-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathArraySchema.js
More file actions
170 lines (155 loc) · 5.74 KB
/
ArraySchema.js
File metadata and controls
170 lines (155 loc) · 5.74 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict'
const { BaseSchema } = require('./BaseSchema')
const { setAttribute, isFluentSchema, FluentSchemaError } = require('./utils')
const initialState = {
// $schema: 'http://json-schema.org/draft-07/schema#',
type: 'array',
definitions: [],
properties: [],
required: []
}
/**
* Represents a ArraySchema.
* @param {Object} [options] - Options
* @param {StringSchema} [options.schema] - Default schema
* @param {boolean} [options.generateIds = false] - generate the id automatically e.g. #properties.foo
* @returns {ArraySchema}
*/
// https://medium.com/javascript-scene/javascript-factory-functions-with-es6-4d224591a8b1
// Factory Functions for Mixin Composition withBaseSchema
const ArraySchema = ({ schema = initialState, ...options } = {}) => {
options = {
generateIds: false,
factory: ArraySchema,
...options
}
return {
...BaseSchema({ ...options, schema }),
/**
* This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself.
* If "items" is a schema, validation succeeds if all elements in the array successfully validate against that schema.
* If "items" is an array of schemas, validation succeeds if each element of the instance validates against the schema at the same position, if any.
* Omitting this keyword has the same behavior as an empty schema.
*
* {@link https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.4.1|reference}
* @param {FluentSchema|FluentSchema[]} items
* @returns {FluentSchema}
*/
items: items => {
if (
!isFluentSchema(items) &&
!(
Array.isArray(items) &&
items.filter(v => isFluentSchema(v)).length > 0
)
) { throw new FluentSchemaError("'items' must be a S or an array of S") }
if (Array.isArray(items)) {
const values = items.map(v => {
const { $schema, ...rest } = v.valueOf()
return rest
})
return setAttribute({ schema, ...options }, ['items', values, 'array'])
}
const { $schema, ...rest } = items.valueOf()
return setAttribute({ schema, ...options }, [
'items',
{ ...rest },
'array'
])
},
/**
* This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself.
*
* {@link https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.4.2|reference}
* @param {FluentSchema|boolean} items
* @returns {FluentSchema}
*/
additionalItems: items => {
if (typeof items !== 'boolean' && !isFluentSchema(items)) {
throw new FluentSchemaError(
"'additionalItems' must be a boolean or a S"
)
}
if (items === false) {
return setAttribute({ schema, ...options }, [
'additionalItems',
false,
'array'
])
}
const { $schema, ...rest } = items.valueOf()
return setAttribute({ schema, ...options }, [
'additionalItems',
{ ...rest },
'array'
])
},
/**
* An array instance is valid against "contains" if at least one of its elements is valid against the given schema.
*
* {@link https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.4.6|reference}
* @param {FluentSchema} value
* @returns {FluentSchema}
*/
contains: value => {
if (!isFluentSchema(value)) { throw new FluentSchemaError("'contains' must be a S") }
const {
$schema,
definitions,
properties,
required,
...rest
} = value.valueOf()
return setAttribute({ schema, ...options }, [
'contains',
{ ...rest },
'array'
])
},
/**
* If this keyword has boolean value false, the instance validates successfully.
* If it has boolean value true, the instance validates successfully if all of its elements are unique.
* Omitting this keyword has the same behavior as a value of false.
*
* {@link https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.4.5|reference}
* @param {boolean} boolean
* @returns {FluentSchema}
*/
uniqueItems: boolean => {
if (typeof boolean !== 'boolean') { throw new FluentSchemaError("'uniqueItems' must be a boolean") }
return setAttribute({ schema, ...options }, [
'uniqueItems',
boolean,
'array'
])
},
/**
* An array instance is valid against "minItems" if its size is greater than, or equal to, the value of this keyword.
* Omitting this keyword has the same behavior as a value of 0.
*
* {@link https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.4.4|reference}
* @param {number} min
* @returns {FluentSchema}
*/
minItems: min => {
if (!Number.isInteger(min)) { throw new FluentSchemaError("'minItems' must be a integer") }
return setAttribute({ schema, ...options }, ['minItems', min, 'array'])
},
/**
* An array instance is valid against "minItems" if its size is greater than, or equal to, the value of this keyword.
* Omitting this keyword has the same behavior as a value of 0.
*
* {@link https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.4.3|reference}
* @param {number} max
* @returns {FluentSchema}
*/
maxItems: max => {
if (!Number.isInteger(max)) { throw new FluentSchemaError("'maxItems' must be a integer") }
return setAttribute({ schema, ...options }, ['maxItems', max, 'array'])
}
}
}
module.exports = {
ArraySchema,
default: ArraySchema
}