|
|
|
@ -1,8 +1,8 @@
@@ -1,8 +1,8 @@
|
|
|
|
|
import { primitive, PropSchema } from "serializr"; |
|
|
|
|
import {primitive, PropSchema} from 'serializr'; |
|
|
|
|
|
|
|
|
|
function invariant(cond: boolean, message?: string) { |
|
|
|
|
if (!cond) { |
|
|
|
|
throw new Error("[serializr] " + (message || "Illegal ServerState")); |
|
|
|
|
throw new Error('[serializr] ' + (message || 'Illegal ServerState')); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -11,14 +11,11 @@ function isPropSchema(thing: any) {
@@ -11,14 +11,11 @@ function isPropSchema(thing: any) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function isAliasedPropSchema(propSchema: any) { |
|
|
|
|
return typeof propSchema === "object" && !!propSchema.jsonname; |
|
|
|
|
return typeof propSchema === 'object' && !!propSchema.jsonname; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function parallel( |
|
|
|
|
ar: any[], |
|
|
|
|
processor: (item: any, done: any) => void, |
|
|
|
|
cb: any |
|
|
|
|
) { |
|
|
|
|
ar: any[], processor: (item: any, done: any) => void, cb: any) { |
|
|
|
|
if (ar.length === 0) { |
|
|
|
|
return void cb(null, []); |
|
|
|
|
} |
|
|
|
@ -43,17 +40,15 @@ function parallel(
@@ -43,17 +40,15 @@ function parallel(
|
|
|
|
|
|
|
|
|
|
export default function list(propSchema: PropSchema): PropSchema { |
|
|
|
|
propSchema = propSchema || primitive(); |
|
|
|
|
invariant(isPropSchema(propSchema), "expected prop schema as first argument"); |
|
|
|
|
invariant(isPropSchema(propSchema), 'expected prop schema as first argument'); |
|
|
|
|
invariant( |
|
|
|
|
!isAliasedPropSchema(propSchema), |
|
|
|
|
"provided prop is aliased, please put aliases first" |
|
|
|
|
); |
|
|
|
|
!isAliasedPropSchema(propSchema), |
|
|
|
|
'provided prop is aliased, please put aliases first'); |
|
|
|
|
return { |
|
|
|
|
serializer(ar) { |
|
|
|
|
invariant( |
|
|
|
|
ar && typeof ar.length === "number" && typeof ar.map === "function", |
|
|
|
|
"expected array (like) object" |
|
|
|
|
); |
|
|
|
|
ar && typeof ar.length === 'number' && typeof ar.map === 'function', |
|
|
|
|
'expected array (like) object'); |
|
|
|
|
return ar.map(propSchema.serializer); |
|
|
|
|
}, |
|
|
|
|
deserializer(jsonArray, done, context) { |
|
|
|
@ -62,14 +57,15 @@ export default function list(propSchema: PropSchema): PropSchema {
@@ -62,14 +57,15 @@ export default function list(propSchema: PropSchema): PropSchema {
|
|
|
|
|
return void done(null, []); |
|
|
|
|
} |
|
|
|
|
if (!Array.isArray(jsonArray)) { |
|
|
|
|
return void done("[serializr] expected JSON array", null); |
|
|
|
|
return void done('[serializr] expected JSON array', null); |
|
|
|
|
} |
|
|
|
|
parallel( |
|
|
|
|
jsonArray, |
|
|
|
|
(item: any, itemDone: (err: any, targetPropertyValue: any) => void) => |
|
|
|
|
propSchema.deserializer(item, itemDone, context, undefined), |
|
|
|
|
done |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
jsonArray, |
|
|
|
|
(item: any, itemDone: (err: any, targetPropertyValue: any) => void) => |
|
|
|
|
propSchema.deserializer(item, itemDone, context, undefined), |
|
|
|
|
done); |
|
|
|
|
}, |
|
|
|
|
beforeDeserialize: () => {}, |
|
|
|
|
afterDeserialize: () => {}, |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|