-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathprimitive.ts
30 lines (29 loc) · 1.08 KB
/
primitive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { invariant, isPrimitive, processAdditionalPropArgs } from "../utils/utils"
import { PropSchema, AdditionalPropArgs } from "../api/types"
/**
* Indicates that this field contains a primitive value (or Date) which should be serialized literally to json.
*
* @example
* createModelSchema(Todo, {
* title: primitive(),
* })
*
* serialize(new Todo('test')) // { "title": "test" }
*
* @param additionalArgs optional object that contains beforeDeserialize and/or afterDeserialize handlers
*/
export default function primitive(additionalArgs?: AdditionalPropArgs): PropSchema {
let result: PropSchema = {
serializer: function(value) {
invariant(isPrimitive(value), "this value is not primitive: " + value)
return value
},
deserializer: function(jsonValue, done) {
if (!isPrimitive(jsonValue))
return void done("[serializr] this value is not primitive: " + jsonValue)
return void done(null, jsonValue)
}
}
result = processAdditionalPropArgs(result, additionalArgs)
return result
}