-
Notifications
You must be signed in to change notification settings - Fork 18
/
step.ts
199 lines (177 loc) · 6.85 KB
/
step.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import getTypeOf from "./getTypeOf";
import createSchemaOf from "./createSchemaOf";
import { JsonSchema, JsonError, isJsonError, SchemaNode } from "./types";
import { reduceSchema } from "./reduceSchema";
type StepFunction = (
node: SchemaNode,
key: string,
data: any
) => SchemaNode | JsonError | undefined;
const stepType: Record<string, StepFunction> = {
array: (node, key, data) => {
const { draft, schema, pointer } = node;
const itemValue = data?.[key];
const itemsType = getTypeOf(schema.items);
if (itemsType === "object") {
// @spec: ignore additionalItems, when items is schema-object
return reduceSchema(node.next(schema.items as JsonSchema, key), itemValue);
}
if (itemsType === "array") {
// @draft >= 7 bool schema, items:[true, false]
if (schema.items[key] === true) {
return node.next(createSchemaOf(itemValue), key);
}
// @draft >= 7 bool schema, items:[true, false]
if (schema.items[key] === false) {
return draft.errors.invalidDataError({
key,
value: itemValue,
pointer,
schema
});
}
if (schema.items[key]) {
return draft.resolveRef(node.next(schema.items[key] as JsonSchema, key));
}
if (schema.additionalItems === false) {
return draft.errors.additionalItemsError({
key,
value: itemValue,
pointer,
schema
});
}
if (schema.additionalItems === true || schema.additionalItems === undefined) {
return node.next(createSchemaOf(itemValue), key);
}
if (getTypeOf(schema.additionalItems) === "object") {
return node.next(schema.additionalItems, key);
}
throw new Error(
`Invalid schema ${JSON.stringify(schema, null, 2)} for ${JSON.stringify(
data,
null,
2
)}`
);
}
if (schema.additionalItems !== false && itemValue) {
// @todo reevaluate: incomplete schema is created here
// @todo support additionalItems: {schema}
return node.next(createSchemaOf(itemValue), key);
}
return new Error(`Invalid array schema for ${key} at ${pointer}`) as JsonError;
},
object: (node, key, data) => {
const { draft, pointer } = node;
const reduction = reduceSchema(node, data);
const schema = (reduction.schema ?? reduction) as JsonSchema;
// @feature properties
const property = schema?.properties?.[key];
if (property !== undefined) {
// @todo patternProperties also validate properties
// @feature boolean schema
if (property === false) {
return draft.errors.forbiddenPropertyError({
property: key,
value: data,
pointer,
schema
});
} else if (property === true) {
return node.next(createSchemaOf(data?.[key]), key);
}
const nextPropertyNode = draft.resolveRef(node.next(property as JsonSchema, key));
if (isJsonError(nextPropertyNode)) {
return nextPropertyNode;
}
// check if there is a oneOf selection, which must be resolved
if (nextPropertyNode && Array.isArray(nextPropertyNode.schema.oneOf)) {
// @special case: this is a mix of a schema and optional definitions
// we resolve the schema here and add the original schema to `oneOfSchema`
return draft.resolveOneOf(data[key], nextPropertyNode.schema, nextPropertyNode.pointer);
}
if (nextPropertyNode) {
return nextPropertyNode;
}
}
// @feature patternProperties
const { patternProperties } = schema;
if (getTypeOf(patternProperties) === "object") {
// find matching property key
let regex;
const patterns = Object.keys(patternProperties);
for (let i = 0, l = patterns.length; i < l; i += 1) {
regex = new RegExp(patterns[i]);
if (regex.test(key)) {
return node.next(patternProperties[patterns[i]], key);
}
}
}
// @feature additionalProperties
const { additionalProperties } = schema;
if (getTypeOf(additionalProperties) === "object") {
return node.next(schema.additionalProperties, key);
}
if (data && (additionalProperties === undefined || additionalProperties === true)) {
const generatedSchema = createSchemaOf(data[key]);
return generatedSchema ? node.next(generatedSchema, key) : undefined;
}
return draft.errors.unknownPropertyError({
property: key,
value: data,
pointer: `${pointer}`,
schema
});
}
};
/**
* Returns the json-schema of the given object property or array item.
* e.g. it steps by one key into the data
*
* This helper determines the location of the property within the schema (additional properties, oneOf, ...) and
* returns the correct schema.
*
* @param draft - validator
* @param key - property-name or array-index
* @param schema - json schema of current data
* @param data - parent of key
* @param [pointer] - pointer to schema and data (parent of key)
* @return Schema or Error if failed resolving key
*/
export default function step(
node: SchemaNode,
key: string | number,
data?: any
): SchemaNode | JsonError {
const { draft, schema, pointer } = node;
const typeOfData = getTypeOf(data);
let schemaType = schema.type ?? typeOfData;
// @draft >= 4 ?
if (Array.isArray(schemaType)) {
if (!schemaType.includes(typeOfData)) {
return draft.errors.typeError({
value: data,
pointer,
expected: schema.type,
received: typeOfData,
schema
});
}
schemaType = typeOfData;
}
const stepFunction = stepType[schemaType];
if (stepFunction) {
const schemaResult = stepFunction(node, `${key}`, data);
if (schemaResult === undefined) {
return draft.errors.schemaWarning({
pointer,
value: data,
schema,
key
});
}
return schemaResult;
}
return new Error(`Unsupported schema type ${schema.type} for key ${key}`) as JsonError;
}