-
Notifications
You must be signed in to change notification settings - Fork 1
/
Type.js
138 lines (122 loc) · 3.33 KB
/
Type.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict'
/**
* @class
* @param {string} jsonType
* @param {checkCallback} checkFn
* @param {toJSONCallback|string} [toJSON]
* @param {toJSONSchemaCallback} [toJSONSchema]
*/
function Type(jsonType, checkFn, toJSON, toJSONSchema) {
let types = ['number', 'string', 'boolean', 'object', 'array', '*', 'raw']
if (typeof jsonType !== 'string') {
throw new Error('jsType must be a string')
} else if (types.indexOf(jsonType) === -1) {
throw new Error('jsType must be one of: ' + types.join(', '))
} else if (typeof checkFn !== 'function') {
throw new Error('checkFn must be a function')
}
/** @member {string} */
this.jsonType = jsonType
/** @member {Function} */
this.checkFn = checkFn
/** @member {?(toJSONCallback|string)} */
this._toJSON = toJSON
/** @member {?toJSONSchemaCallback} */
this._toJSONSchema = toJSONSchema
}
module.exports = Type
/**
* Check if a given value has the right type
* @param {*} value
* @param {string} path
* @param {*} extra
* @param {Object} options
* @returns {*} the validated value
* @throws if invalid
*/
Type.prototype.validate = function (value, path, extra, options) {
// Call toJSON() if present
if (this.jsonType !== 'raw' &&
value !== null &&
value !== undefined &&
typeof value.toJSON === 'function') {
value = value.toJSON()
}
let type, ret
if (Array.isArray(value)) {
type = 'array'
} else if (value === null) {
type = 'null'
} else if (typeof value === 'number' && isNaN(value)) {
type = 'NaN'
} else if (value === Infinity || value === -Infinity) {
type = 'infinity'
} else {
type = typeof value
}
if (this.jsonType !== '*' &&
this.jsonType !== 'raw' &&
this.jsonType !== type) {
throw 'I was expecting ' + this.jsonType + ' and you gave me ' + type
}
ret = this.checkFn(value, extra, options, path)
return ret === undefined ? value : ret
}
/**
* @param {*} value
* @returns {boolean} whether the given value is considered empty
*/
Type.prototype.isEmpty = function (value) {
return value === undefined ||
value === null ||
(this.jsonType === 'string' && value === '')
}
/**
* Function called when stringifying a Field instance
* Only core types can be precisely stringified
* Custom types are represented by their parent JSON-type
* @param {*} extra
* @returns {*}
*/
Type.prototype.convertToJSON = function (extra) {
if (this._toJSON) {
return typeof this._toJSON === 'function' ? this._toJSON(extra) : this._toJSON
}
// Aproximated convertion
return {
number: '$Number',
string: '$String',
boolean: '$Boolean',
object: '$Object',
array: '$Array',
'*': '*',
raw: '*'
}[this.jsonType]
}
/**
* Function called when coverting to JSON Schema
* Only core types can be precisely represented
* Custom types are represented by their parent JSON-type
* @param {*} extra
* @param {string} [componentsPath] - if given, internal typedefs are returned as references
* @returns {Object}
*/
Type.prototype.convertToJSONSchema = function (extra, componentsPath) {
if (this._toJSONSchema) {
return this._toJSONSchema(extra, componentsPath)
}
// Aproximated convertion
let jsonSchemaType = {
number: 'number',
string: 'string',
boolean: 'boolean',
object: 'object',
array: 'array'
}[this.jsonType]
if (!jsonSchemaType) {
throw new Error('This type cannot be converted to JSON Schema')
}
return {
type: jsonSchemaType
}
}