-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
162 lines (140 loc) · 4.99 KB
/
index.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* eslint-disable no-loop-func */
import * as yup from 'yup';
import mergeWith from 'lodash/mergeWith';
import {getRequiredFields, getPath, mergeWithArray, omitField} from './utils';
import {defaultValidationTypes} from './constants';
/**
* Options
* @param {array} blackList
* @param {object} validationTypes
* @param {object} customValidationFields
*/
const DEFAULT_OPTIONS = {
validationTypes: defaultValidationTypes,
customValidationFields: {},
blackList: []
};
/**
* Update Field Level Validation
* @param {*} schema
*/
export const buildYupSchemaValidation = (jsonSchema, opts = {}) => {
let schema = JSON.parse(JSON.stringify(jsonSchema));
const options = {...DEFAULT_OPTIONS, ...opts};
const blackListFields = new Set([...options.blackList]);
const validationTypes = {...options.validationTypes};
const customValidationFields = {...options.customValidationFields};
const stack = [];
let requiredFields = new Set();
// Consolidate 'allOf' field definitions
if (Object.prototype.hasOwnProperty.call(schema, 'allOf')) {
const {allOf, ...mergedSchema} = mergeWith(
schema,
mergeWith(...schema.allOf, mergeWithArray),
mergeWithArray
);
schema = mergedSchema;
}
// Consolidate required fields by path
if (Object.prototype.hasOwnProperty.call(schema, 'required')) {
requiredFields = new Set(
getRequiredFields(schema.properties, schema.properties, schema.required)
);
}
if (Object.prototype.hasOwnProperty.call(schema, 'properties')) {
stack.push(omitField(schema.properties, blackListFields));
}
while (stack.length) {
const fields = stack.pop();
Object.keys(fields).forEach(field => {
// Consolidate 'allOf' field definitions
if (Object.prototype.hasOwnProperty.call(fields[field], 'allOf')) {
const allOf = mergeWith(
fields[field],
mergeWith(...fields[field].allOf, mergeWithArray),
mergeWithArray
);
const yupObj = yup
.object()
.shape(omitField(allOf.properties, blackListFields));
const {required = []} = allOf;
fields[field] = yupObj;
stack.push(yupObj.fields);
requiredFields = new Set([
...requiredFields,
...getRequiredFields(yupObj.fields, schema.properties, required)
]);
// Manage 'array' field definitions
} else if (
fields[field].type === 'array' &&
Object.prototype.hasOwnProperty.call(fields[field], 'items') &&
!customValidationFields[field]
) {
const {minItems, maxItems} = fields[field];
const {required = []} = fields[field].items;
const yupObj = yup
.object()
.shape(omitField(fields[field].items.properties, blackListFields));
fields[field] = yup.array().of(yupObj);
stack.push(yupObj.fields);
if (minItems) {
fields[field] = fields[field].min(minItems);
}
if (maxItems) {
fields[field] = fields[field].max(maxItems);
}
if (requiredFields.has(getPath(schema.properties, fields[field]))) {
fields[field] = fields[field].required();
}
requiredFields = new Set([
...requiredFields,
...getRequiredFields(yupObj.fields, schema.properties, required)
]);
// Manage 'object' field definitions
} else if (
fields[field].type === 'object' &&
Object.prototype.hasOwnProperty.call(fields[field], 'properties') &&
!customValidationFields[field]
) {
const {required = []} = fields[field];
const yupObj = yup
.object()
.shape(omitField(fields[field].properties, blackListFields));
fields[field] = yupObj;
stack.push(yupObj.fields);
requiredFields = new Set([
...requiredFields,
...getRequiredFields(yupObj.fields, schema.properties, required)
]);
// Manage 'object' field definitions
} else if (!blackListFields.has(field)) {
const isRequired = requiredFields.has(
getPath(schema.properties, fields[field])
);
const enumValues = fields[field].enum;
const minimum = fields[field].minimum || fields[field].minLength;
const maximum = fields[field].maximum || fields[field].maxLength;
if (customValidationFields[field]) {
fields[field] = customValidationFields[field];
} else if (validationTypes[fields[field].type]) {
fields[field] = validationTypes[fields[field].type];
} else {
fields[field] = yup.mixed().nullable();
}
if (enumValues) {
fields[field] = fields[field].oneOf([...enumValues]);
}
if (minimum) {
fields[field] = fields[field].min(minimum);
}
if (maximum) {
fields[field] = fields[field].max(maximum);
}
if (isRequired) {
fields[field] = fields[field].required();
}
}
});
}
return yup.object().shape(schema.properties);
};