@rjsf/validator-ajv8 5.11.1 → 5.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compileSchemaValidators.esm.js +68 -0
- package/dist/compileSchemaValidators.esm.js.map +7 -0
- package/dist/compileSchemaValidators.js +96 -5
- package/dist/compileSchemaValidators.js.map +7 -0
- package/dist/index.js +362 -5
- package/dist/index.js.map +7 -0
- package/dist/validator-ajv8.esm.js +166 -266
- package/dist/validator-ajv8.esm.js.map +7 -1
- package/dist/{validator-ajv8.umd.development.js → validator-ajv8.umd.js} +136 -277
- package/lib/compileSchemaValidators.d.ts +16 -0
- package/lib/compileSchemaValidators.js +21 -0
- package/lib/compileSchemaValidators.js.map +1 -0
- package/lib/compileSchemaValidatorsCode.d.ts +13 -0
- package/lib/compileSchemaValidatorsCode.js +23 -0
- package/lib/compileSchemaValidatorsCode.js.map +1 -0
- package/lib/createAjvInstance.d.ts +22 -0
- package/lib/createAjvInstance.js +54 -0
- package/lib/createAjvInstance.js.map +1 -0
- package/lib/createPrecompiledValidator.d.ts +14 -0
- package/lib/createPrecompiledValidator.js +16 -0
- package/lib/createPrecompiledValidator.js.map +1 -0
- package/lib/customizeValidator.d.ts +11 -0
- package/lib/customizeValidator.js +13 -0
- package/lib/customizeValidator.js.map +1 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +6 -0
- package/lib/index.js.map +1 -0
- package/lib/precompiledValidator.d.ts +87 -0
- package/lib/precompiledValidator.js +103 -0
- package/lib/precompiledValidator.js.map +1 -0
- package/lib/processRawValidationErrors.d.ts +30 -0
- package/lib/processRawValidationErrors.js +91 -0
- package/lib/processRawValidationErrors.js.map +1 -0
- package/lib/types.d.ts +38 -0
- package/lib/types.js +2 -0
- package/lib/types.js.map +1 -0
- package/lib/validator.d.ts +59 -0
- package/lib/validator.js +122 -0
- package/lib/validator.js.map +1 -0
- package/package.json +21 -16
- package/src/compileSchemaValidators.ts +29 -0
- package/src/compileSchemaValidatorsCode.ts +34 -0
- package/src/createAjvInstance.ts +68 -0
- package/src/createPrecompiledValidator.ts +23 -0
- package/src/customizeValidator.ts +20 -0
- package/src/index.ts +7 -0
- package/src/precompiledValidator.ts +174 -0
- package/src/processRawValidationErrors.ts +139 -0
- package/src/types.ts +40 -0
- package/src/validator.ts +163 -0
- package/dist/compileSchemaValidators.cjs.development.js +0 -57
- package/dist/compileSchemaValidators.cjs.development.js.map +0 -1
- package/dist/compileSchemaValidators.cjs.production.min.js +0 -2
- package/dist/compileSchemaValidators.cjs.production.min.js.map +0 -1
- package/dist/createAjvInstance-33d73c95.js +0 -2
- package/dist/createAjvInstance-33d73c95.js.map +0 -1
- package/dist/index.cjs.development.js +0 -412
- package/dist/index.cjs.development.js.map +0 -1
- package/dist/index.cjs.production.min.js +0 -2
- package/dist/index.cjs.production.min.js.map +0 -1
- package/dist/index.d.ts +0 -68
- package/dist/validator-ajv8.umd.development.js.map +0 -1
- package/dist/validator-ajv8.umd.production.min.js +0 -2
- package/dist/validator-ajv8.umd.production.min.js.map +0 -1
|
@@ -1,149 +1,87 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@rjsf/utils'), require('ajv'), require('ajv-formats'), require('lodash
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', '@rjsf/utils', 'ajv', 'ajv-formats', 'lodash
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@rjsf/utils'), require('ajv'), require('ajv-formats'), require('lodash/isObject'), require('lodash/get'), require('lodash/isEqual')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', '@rjsf/utils', 'ajv', 'ajv-formats', 'lodash/isObject', 'lodash/get', 'lodash/isEqual'], factory) :
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@rjsf/validator-ajv8"] = {}, global.utils, global.Ajv, global.addFormats, global.isObject, global.get, global.isEqual));
|
|
5
5
|
})(this, (function (exports, utils, Ajv, addFormats, isObject, get, isEqual) { 'use strict';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var Ajv__default = /*#__PURE__*/_interopDefaultLegacy(Ajv);
|
|
10
|
-
var addFormats__default = /*#__PURE__*/_interopDefaultLegacy(addFormats);
|
|
11
|
-
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
|
|
12
|
-
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
|
|
13
|
-
var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
|
|
14
|
-
|
|
15
|
-
const AJV_CONFIG = {
|
|
7
|
+
// src/validator.ts
|
|
8
|
+
var AJV_CONFIG = {
|
|
16
9
|
allErrors: true,
|
|
17
10
|
multipleOfPrecision: 8,
|
|
18
11
|
strict: false,
|
|
19
12
|
verbose: true
|
|
20
13
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
* list. If `customFormats` are provided then those additional formats are added to the list of supported formats. If
|
|
26
|
-
* `ajvOptionsOverrides` are provided then they are spread on top of the default `AJV_CONFIG` options when constructing
|
|
27
|
-
* the `Ajv` instance. With Ajv v8, the JSON Schema formats are not provided by default, but can be plugged in. By
|
|
28
|
-
* default, all formats from the `ajv-formats` library are added. To disable this capability, set the `ajvFormatOptions`
|
|
29
|
-
* parameter to `false`. Additionally, you can configure the `ajv-formats` by providing a custom set of
|
|
30
|
-
* [format options](https://github.com/ajv-validator/ajv-formats) to the `ajvFormatOptions` parameter.
|
|
31
|
-
*
|
|
32
|
-
* @param [additionalMetaSchemas] - The list of additional meta schemas that the validator can access
|
|
33
|
-
* @param [customFormats] - The set of additional custom formats that the validator will support
|
|
34
|
-
* @param [ajvOptionsOverrides={}] - The set of validator config override options
|
|
35
|
-
* @param [ajvFormatOptions] - The `ajv-format` options to use when adding formats to `ajv`; pass `false` to disable it
|
|
36
|
-
* @param [AjvClass] - The `Ajv` class to use when creating the validator instance
|
|
37
|
-
*/
|
|
38
|
-
function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}, ajvFormatOptions, AjvClass = Ajv__default["default"]) {
|
|
39
|
-
const ajv = new AjvClass({
|
|
40
|
-
...AJV_CONFIG,
|
|
41
|
-
...ajvOptionsOverrides
|
|
42
|
-
});
|
|
14
|
+
var COLOR_FORMAT_REGEX = /^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/;
|
|
15
|
+
var DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/;
|
|
16
|
+
function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}, ajvFormatOptions, AjvClass = Ajv) {
|
|
17
|
+
const ajv = new AjvClass({ ...AJV_CONFIG, ...ajvOptionsOverrides });
|
|
43
18
|
if (ajvFormatOptions) {
|
|
44
|
-
|
|
19
|
+
addFormats(ajv, ajvFormatOptions);
|
|
45
20
|
} else if (ajvFormatOptions !== false) {
|
|
46
|
-
|
|
21
|
+
addFormats(ajv);
|
|
47
22
|
}
|
|
48
|
-
|
|
49
|
-
ajv.addFormat(
|
|
50
|
-
ajv.addFormat('color', COLOR_FORMAT_REGEX);
|
|
51
|
-
// Add RJSF-specific additional properties keywords so Ajv doesn't report errors if strict is enabled.
|
|
23
|
+
ajv.addFormat("data-url", DATA_URL_FORMAT_REGEX);
|
|
24
|
+
ajv.addFormat("color", COLOR_FORMAT_REGEX);
|
|
52
25
|
ajv.addKeyword(utils.ADDITIONAL_PROPERTY_FLAG);
|
|
53
26
|
ajv.addKeyword(utils.RJSF_ADDITONAL_PROPERTIES_FLAG);
|
|
54
|
-
// add more schemas to validate against
|
|
55
27
|
if (Array.isArray(additionalMetaSchemas)) {
|
|
56
28
|
ajv.addMetaSchema(additionalMetaSchemas);
|
|
57
29
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
Object.keys(customFormats).forEach(formatName => {
|
|
30
|
+
if (isObject(customFormats)) {
|
|
31
|
+
Object.keys(customFormats).forEach((formatName) => {
|
|
61
32
|
ajv.addFormat(formatName, customFormats[formatName]);
|
|
62
33
|
});
|
|
63
34
|
}
|
|
64
35
|
return ajv;
|
|
65
36
|
}
|
|
66
|
-
|
|
67
|
-
/** Transforming the error output from ajv to format used by @rjsf/utils.
|
|
68
|
-
* At some point, components should be updated to support ajv.
|
|
69
|
-
*
|
|
70
|
-
* @param errors - The list of AJV errors to convert to `RJSFValidationErrors`
|
|
71
|
-
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
72
|
-
*/
|
|
73
37
|
function transformRJSFValidationErrors(errors = [], uiSchema) {
|
|
74
|
-
return errors.map(e => {
|
|
75
|
-
const {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
params,
|
|
79
|
-
schemaPath,
|
|
80
|
-
parentSchema,
|
|
81
|
-
...rest
|
|
82
|
-
} = e;
|
|
83
|
-
let {
|
|
84
|
-
message = ''
|
|
85
|
-
} = rest;
|
|
86
|
-
let property = instancePath.replace(/\//g, '.');
|
|
38
|
+
return errors.map((e) => {
|
|
39
|
+
const { instancePath, keyword, params, schemaPath, parentSchema, ...rest } = e;
|
|
40
|
+
let { message = "" } = rest;
|
|
41
|
+
let property = instancePath.replace(/\//g, ".");
|
|
87
42
|
let stack = `${property} ${message}`.trim();
|
|
88
|
-
if (
|
|
43
|
+
if ("missingProperty" in params) {
|
|
89
44
|
property = property ? `${property}.${params.missingProperty}` : params.missingProperty;
|
|
90
45
|
const currentProperty = params.missingProperty;
|
|
91
|
-
const uiSchemaTitle = utils.getUiOptions(
|
|
46
|
+
const uiSchemaTitle = utils.getUiOptions(get(uiSchema, `${property.replace(/^\./, "")}`)).title;
|
|
92
47
|
if (uiSchemaTitle) {
|
|
93
48
|
message = message.replace(currentProperty, uiSchemaTitle);
|
|
94
49
|
} else {
|
|
95
|
-
const parentSchemaTitle =
|
|
50
|
+
const parentSchemaTitle = get(parentSchema, [utils.PROPERTIES_KEY, currentProperty, "title"]);
|
|
96
51
|
if (parentSchemaTitle) {
|
|
97
52
|
message = message.replace(currentProperty, parentSchemaTitle);
|
|
98
53
|
}
|
|
99
54
|
}
|
|
100
55
|
stack = message;
|
|
101
56
|
} else {
|
|
102
|
-
const uiSchemaTitle = utils.getUiOptions(
|
|
57
|
+
const uiSchemaTitle = utils.getUiOptions(get(uiSchema, `${property.replace(/^\./, "")}`)).title;
|
|
103
58
|
if (uiSchemaTitle) {
|
|
104
59
|
stack = `'${uiSchemaTitle}' ${message}`.trim();
|
|
105
60
|
} else {
|
|
106
|
-
const parentSchemaTitle = parentSchema
|
|
61
|
+
const parentSchemaTitle = parentSchema?.title;
|
|
107
62
|
if (parentSchemaTitle) {
|
|
108
63
|
stack = `'${parentSchemaTitle}' ${message}`.trim();
|
|
109
64
|
}
|
|
110
65
|
}
|
|
111
66
|
}
|
|
112
|
-
// put data in expected format
|
|
113
67
|
return {
|
|
114
68
|
name: keyword,
|
|
115
69
|
property,
|
|
116
70
|
message,
|
|
117
71
|
params,
|
|
72
|
+
// specific to ajv
|
|
118
73
|
stack,
|
|
119
74
|
schemaPath
|
|
120
75
|
};
|
|
121
76
|
});
|
|
122
77
|
}
|
|
123
|
-
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
|
|
124
|
-
* the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
|
|
125
|
-
* supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
|
|
126
|
-
* transform them in what ever way it chooses.
|
|
127
|
-
*
|
|
128
|
-
* @param validator - The `ValidatorType` implementation used for the `getDefaultFormState()` call
|
|
129
|
-
* @param rawErrors - The list of raw `ErrorObject`s to process
|
|
130
|
-
* @param formData - The form data to validate
|
|
131
|
-
* @param schema - The schema against which to validate the form data
|
|
132
|
-
* @param [customValidate] - An optional function that is used to perform custom validation
|
|
133
|
-
* @param [transformErrors] - An optional function that is used to transform errors after AJV validation
|
|
134
|
-
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
135
|
-
*/
|
|
136
78
|
function processRawValidationErrors(validator, rawErrors, formData, schema, customValidate, transformErrors, uiSchema) {
|
|
137
|
-
const {
|
|
138
|
-
validationError: invalidSchemaError
|
|
139
|
-
} = rawErrors;
|
|
79
|
+
const { validationError: invalidSchemaError } = rawErrors;
|
|
140
80
|
let errors = transformRJSFValidationErrors(rawErrors.errors, uiSchema);
|
|
141
81
|
if (invalidSchemaError) {
|
|
142
|
-
errors = [...errors, {
|
|
143
|
-
stack: invalidSchemaError.message
|
|
144
|
-
}];
|
|
82
|
+
errors = [...errors, { stack: invalidSchemaError.message }];
|
|
145
83
|
}
|
|
146
|
-
if (typeof transformErrors ===
|
|
84
|
+
if (typeof transformErrors === "function") {
|
|
147
85
|
errors = transformErrors(errors, uiSchema);
|
|
148
86
|
}
|
|
149
87
|
let errorSchema = utils.toErrorSchema(errors);
|
|
@@ -155,75 +93,51 @@
|
|
|
155
93
|
}
|
|
156
94
|
};
|
|
157
95
|
}
|
|
158
|
-
if (typeof customValidate !==
|
|
159
|
-
return {
|
|
160
|
-
errors,
|
|
161
|
-
errorSchema
|
|
162
|
-
};
|
|
96
|
+
if (typeof customValidate !== "function") {
|
|
97
|
+
return { errors, errorSchema };
|
|
163
98
|
}
|
|
164
|
-
// Include form data with undefined values, which is required for custom validation.
|
|
165
99
|
const newFormData = utils.getDefaultFormState(validator, schema, formData, schema, true);
|
|
166
100
|
const errorHandler = customValidate(newFormData, utils.createErrorHandler(newFormData), uiSchema);
|
|
167
101
|
const userErrorSchema = utils.unwrapErrorHandler(errorHandler);
|
|
168
|
-
return utils.validationDataMerge({
|
|
169
|
-
errors,
|
|
170
|
-
errorSchema
|
|
171
|
-
}, userErrorSchema);
|
|
102
|
+
return utils.validationDataMerge({ errors, errorSchema }, userErrorSchema);
|
|
172
103
|
}
|
|
173
104
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
*
|
|
179
|
-
* @param
|
|
180
|
-
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
105
|
+
// src/validator.ts
|
|
106
|
+
var AJV8Validator = class {
|
|
107
|
+
/** Constructs an `AJV8Validator` instance using the `options`
|
|
108
|
+
*
|
|
109
|
+
* @param options - The `CustomValidatorOptionsType` options that are used to create the AJV instance
|
|
110
|
+
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
181
111
|
*/
|
|
182
112
|
constructor(options, localizer) {
|
|
183
|
-
|
|
184
|
-
*
|
|
185
|
-
* @private
|
|
186
|
-
*/
|
|
187
|
-
this.ajv = void 0;
|
|
188
|
-
/** The Localizer function to use for localizing Ajv errors
|
|
189
|
-
*
|
|
190
|
-
* @private
|
|
191
|
-
*/
|
|
192
|
-
this.localizer = void 0;
|
|
193
|
-
const {
|
|
194
|
-
additionalMetaSchemas,
|
|
195
|
-
customFormats,
|
|
196
|
-
ajvOptionsOverrides,
|
|
197
|
-
ajvFormatOptions,
|
|
198
|
-
AjvClass
|
|
199
|
-
} = options;
|
|
113
|
+
const { additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass } = options;
|
|
200
114
|
this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass);
|
|
201
115
|
this.localizer = localizer;
|
|
202
116
|
}
|
|
203
|
-
/** Converts an `errorSchema` into a list of `RJSFValidationErrors`
|
|
204
|
-
*
|
|
205
|
-
* @param errorSchema - The `ErrorSchema` instance to convert
|
|
206
|
-
* @param [fieldPath=[]] - The current field path, defaults to [] if not specified
|
|
207
|
-
* @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in
|
|
208
|
-
* the next major release.
|
|
117
|
+
/** Converts an `errorSchema` into a list of `RJSFValidationErrors`
|
|
118
|
+
*
|
|
119
|
+
* @param errorSchema - The `ErrorSchema` instance to convert
|
|
120
|
+
* @param [fieldPath=[]] - The current field path, defaults to [] if not specified
|
|
121
|
+
* @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in
|
|
122
|
+
* the next major release.
|
|
209
123
|
*/
|
|
210
124
|
toErrorList(errorSchema, fieldPath = []) {
|
|
211
125
|
return utils.toErrorList(errorSchema, fieldPath);
|
|
212
126
|
}
|
|
213
|
-
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
|
|
214
|
-
* by the playground. Returns the `errors` from the validation
|
|
215
|
-
*
|
|
216
|
-
* @param schema - The schema against which to validate the form data * @param schema
|
|
217
|
-
* @param formData - The form data to validate
|
|
127
|
+
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
|
|
128
|
+
* by the playground. Returns the `errors` from the validation
|
|
129
|
+
*
|
|
130
|
+
* @param schema - The schema against which to validate the form data * @param schema
|
|
131
|
+
* @param formData - The form data to validate
|
|
218
132
|
*/
|
|
219
133
|
rawValidation(schema, formData) {
|
|
220
|
-
let compilationError =
|
|
134
|
+
let compilationError = void 0;
|
|
221
135
|
let compiledValidator;
|
|
222
136
|
if (schema[utils.ID_KEY]) {
|
|
223
137
|
compiledValidator = this.ajv.getSchema(schema[utils.ID_KEY]);
|
|
224
138
|
}
|
|
225
139
|
try {
|
|
226
|
-
if (compiledValidator ===
|
|
140
|
+
if (compiledValidator === void 0) {
|
|
227
141
|
compiledValidator = this.ajv.compile(schema);
|
|
228
142
|
}
|
|
229
143
|
compiledValidator(formData);
|
|
@@ -232,234 +146,179 @@
|
|
|
232
146
|
}
|
|
233
147
|
let errors;
|
|
234
148
|
if (compiledValidator) {
|
|
235
|
-
if (typeof this.localizer ===
|
|
149
|
+
if (typeof this.localizer === "function") {
|
|
236
150
|
this.localizer(compiledValidator.errors);
|
|
237
151
|
}
|
|
238
|
-
errors = compiledValidator.errors ||
|
|
239
|
-
// Clear errors to prevent persistent errors, see #1104
|
|
152
|
+
errors = compiledValidator.errors || void 0;
|
|
240
153
|
compiledValidator.errors = null;
|
|
241
154
|
}
|
|
242
155
|
return {
|
|
243
|
-
errors
|
|
156
|
+
errors,
|
|
244
157
|
validationError: compilationError
|
|
245
158
|
};
|
|
246
159
|
}
|
|
247
|
-
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
|
|
248
|
-
* the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
|
|
249
|
-
* supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
|
|
250
|
-
* transform them in what ever way it chooses.
|
|
251
|
-
*
|
|
252
|
-
* @param formData - The form data to validate
|
|
253
|
-
* @param schema - The schema against which to validate the form data
|
|
254
|
-
* @param [customValidate] - An optional function that is used to perform custom validation
|
|
255
|
-
* @param [transformErrors] - An optional function that is used to transform errors after AJV validation
|
|
256
|
-
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
160
|
+
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
|
|
161
|
+
* the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
|
|
162
|
+
* supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
|
|
163
|
+
* transform them in what ever way it chooses.
|
|
164
|
+
*
|
|
165
|
+
* @param formData - The form data to validate
|
|
166
|
+
* @param schema - The schema against which to validate the form data
|
|
167
|
+
* @param [customValidate] - An optional function that is used to perform custom validation
|
|
168
|
+
* @param [transformErrors] - An optional function that is used to transform errors after AJV validation
|
|
169
|
+
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
257
170
|
*/
|
|
258
171
|
validateFormData(formData, schema, customValidate, transformErrors, uiSchema) {
|
|
259
172
|
const rawErrors = this.rawValidation(schema, formData);
|
|
260
173
|
return processRawValidationErrors(this, rawErrors, formData, schema, customValidate, transformErrors, uiSchema);
|
|
261
174
|
}
|
|
262
|
-
/** Validates data against a schema, returning true if the data is valid, or
|
|
263
|
-
* false otherwise. If the schema is invalid, then this function will return
|
|
264
|
-
* false.
|
|
265
|
-
*
|
|
266
|
-
* @param schema - The schema against which to validate the form data
|
|
267
|
-
* @param formData - The form data to validate
|
|
268
|
-
* @param rootSchema - The root schema used to provide $ref resolutions
|
|
175
|
+
/** Validates data against a schema, returning true if the data is valid, or
|
|
176
|
+
* false otherwise. If the schema is invalid, then this function will return
|
|
177
|
+
* false.
|
|
178
|
+
*
|
|
179
|
+
* @param schema - The schema against which to validate the form data
|
|
180
|
+
* @param formData - The form data to validate
|
|
181
|
+
* @param rootSchema - The root schema used to provide $ref resolutions
|
|
269
182
|
*/
|
|
270
183
|
isValid(schema, formData, rootSchema) {
|
|
271
184
|
const rootSchemaId = rootSchema[utils.ID_KEY] ?? utils.ROOT_SCHEMA_PREFIX;
|
|
272
185
|
try {
|
|
273
|
-
|
|
274
|
-
// then rewrite the schema ref's to point to the rootSchema
|
|
275
|
-
// this accounts for the case where schema have references to models
|
|
276
|
-
// that lives in the rootSchema but not in the schema in question.
|
|
277
|
-
if (this.ajv.getSchema(rootSchemaId) === undefined) {
|
|
186
|
+
if (this.ajv.getSchema(rootSchemaId) === void 0) {
|
|
278
187
|
this.ajv.addSchema(rootSchema, rootSchemaId);
|
|
279
188
|
}
|
|
280
189
|
const schemaWithIdRefPrefix = utils.withIdRefPrefix(schema);
|
|
281
190
|
const schemaId = schemaWithIdRefPrefix[utils.ID_KEY] ?? utils.hashForSchema(schemaWithIdRefPrefix);
|
|
282
191
|
let compiledValidator;
|
|
283
192
|
compiledValidator = this.ajv.getSchema(schemaId);
|
|
284
|
-
if (compiledValidator ===
|
|
285
|
-
// Add schema by an explicit ID so it can be fetched later
|
|
286
|
-
// Fall back to using compile if necessary
|
|
287
|
-
// https://ajv.js.org/guide/managing-schemas.html#pre-adding-all-schemas-vs-adding-on-demand
|
|
193
|
+
if (compiledValidator === void 0) {
|
|
288
194
|
compiledValidator = this.ajv.addSchema(schemaWithIdRefPrefix, schemaId).getSchema(schemaId) || this.ajv.compile(schemaWithIdRefPrefix);
|
|
289
195
|
}
|
|
290
196
|
const result = compiledValidator(formData);
|
|
291
197
|
return result;
|
|
292
198
|
} catch (e) {
|
|
293
|
-
console.warn(
|
|
199
|
+
console.warn("Error encountered compiling schema:", e);
|
|
294
200
|
return false;
|
|
295
201
|
} finally {
|
|
296
|
-
// TODO: A function should be called if the root schema changes so we don't have to remove and recompile the schema every run.
|
|
297
|
-
// make sure we remove the rootSchema from the global ajv instance
|
|
298
202
|
this.ajv.removeSchema(rootSchemaId);
|
|
299
203
|
}
|
|
300
204
|
}
|
|
301
|
-
}
|
|
205
|
+
};
|
|
302
206
|
|
|
303
|
-
|
|
304
|
-
* provided. If a `localizer` is provided, it is used to translate the messages generated by the underlying AJV
|
|
305
|
-
* validation.
|
|
306
|
-
*
|
|
307
|
-
* @param [options={}] - The `CustomValidatorOptionsType` options that are used to create the `ValidatorType` instance
|
|
308
|
-
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
309
|
-
* @returns - The custom validator implementation resulting from the set of parameters provided
|
|
310
|
-
*/
|
|
207
|
+
// src/customizeValidator.ts
|
|
311
208
|
function customizeValidator(options = {}, localizer) {
|
|
312
209
|
return new AJV8Validator(options, localizer);
|
|
313
210
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
*
|
|
321
|
-
* @param validateFns - The map of the validation functions that are generated by the `schemaCompile()` function
|
|
322
|
-
* @param rootSchema - The root schema that was used with the `compileSchema()` function
|
|
323
|
-
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
324
|
-
* @throws - Error when the base schema of the precompiled validator does not have a matching validator function
|
|
211
|
+
var AJV8PrecompiledValidator = class {
|
|
212
|
+
/** Constructs an `AJV8PrecompiledValidator` instance using the `validateFns` and `rootSchema`
|
|
213
|
+
*
|
|
214
|
+
* @param validateFns - The map of the validation functions that are generated by the `schemaCompile()` function
|
|
215
|
+
* @param rootSchema - The root schema that was used with the `compileSchema()` function
|
|
216
|
+
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
217
|
+
* @throws - Error when the base schema of the precompiled validator does not have a matching validator function
|
|
325
218
|
*/
|
|
326
219
|
constructor(validateFns, rootSchema, localizer) {
|
|
327
|
-
/** The root schema object used to construct this validator
|
|
328
|
-
*
|
|
329
|
-
* @private
|
|
330
|
-
*/
|
|
331
|
-
this.rootSchema = void 0;
|
|
332
|
-
/** The root schema resolved top level refs
|
|
333
|
-
*
|
|
334
|
-
* @private
|
|
335
|
-
*/
|
|
336
|
-
this.resolvedRootSchema = void 0;
|
|
337
|
-
/** The `ValidatorFunctions` map used to construct this validator
|
|
338
|
-
*
|
|
339
|
-
* @private
|
|
340
|
-
*/
|
|
341
|
-
this.validateFns = void 0;
|
|
342
|
-
/** The main validator function associated with the base schema in the `precompiledValidator`
|
|
343
|
-
*
|
|
344
|
-
* @private
|
|
345
|
-
*/
|
|
346
|
-
this.mainValidator = void 0;
|
|
347
|
-
/** The Localizer function to use for localizing Ajv errors
|
|
348
|
-
*
|
|
349
|
-
* @private
|
|
350
|
-
*/
|
|
351
|
-
this.localizer = void 0;
|
|
352
220
|
this.rootSchema = rootSchema;
|
|
353
221
|
this.validateFns = validateFns;
|
|
354
222
|
this.localizer = localizer;
|
|
355
223
|
this.mainValidator = this.getValidator(rootSchema);
|
|
356
224
|
this.resolvedRootSchema = utils.retrieveSchema(this, rootSchema, rootSchema);
|
|
357
225
|
}
|
|
358
|
-
/** Returns the precompiled validator associated with the given `schema` from the map of precompiled validator
|
|
359
|
-
* functions.
|
|
360
|
-
*
|
|
361
|
-
* @param schema - The schema for which a precompiled validator function is desired
|
|
362
|
-
* @returns - The precompiled validator function associated with this schema
|
|
226
|
+
/** Returns the precompiled validator associated with the given `schema` from the map of precompiled validator
|
|
227
|
+
* functions.
|
|
228
|
+
*
|
|
229
|
+
* @param schema - The schema for which a precompiled validator function is desired
|
|
230
|
+
* @returns - The precompiled validator function associated with this schema
|
|
363
231
|
*/
|
|
364
232
|
getValidator(schema) {
|
|
365
|
-
const key =
|
|
233
|
+
const key = get(schema, utils.ID_KEY) || utils.hashForSchema(schema);
|
|
366
234
|
const validator = this.validateFns[key];
|
|
367
235
|
if (!validator) {
|
|
368
236
|
throw new Error(`No precompiled validator function was found for the given schema for "${key}"`);
|
|
369
237
|
}
|
|
370
238
|
return validator;
|
|
371
239
|
}
|
|
372
|
-
/** Converts an `errorSchema` into a list of `RJSFValidationErrors`
|
|
373
|
-
*
|
|
374
|
-
* @param errorSchema - The `ErrorSchema` instance to convert
|
|
375
|
-
* @param [fieldPath=[]] - The current field path, defaults to [] if not specified
|
|
376
|
-
* @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in
|
|
377
|
-
* the next major release.
|
|
240
|
+
/** Converts an `errorSchema` into a list of `RJSFValidationErrors`
|
|
241
|
+
*
|
|
242
|
+
* @param errorSchema - The `ErrorSchema` instance to convert
|
|
243
|
+
* @param [fieldPath=[]] - The current field path, defaults to [] if not specified
|
|
244
|
+
* @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in
|
|
245
|
+
* the next major release.
|
|
378
246
|
*/
|
|
379
247
|
toErrorList(errorSchema, fieldPath = []) {
|
|
380
248
|
return utils.toErrorList(errorSchema, fieldPath);
|
|
381
249
|
}
|
|
382
|
-
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
|
|
383
|
-
* by the playground. Returns the `errors` from the validation
|
|
384
|
-
*
|
|
385
|
-
* @param schema - The schema against which to validate the form data * @param schema
|
|
386
|
-
* @param formData - The form data to validate
|
|
387
|
-
* @throws - Error when the schema provided does not match the base schema of the precompiled validator
|
|
250
|
+
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
|
|
251
|
+
* by the playground. Returns the `errors` from the validation
|
|
252
|
+
*
|
|
253
|
+
* @param schema - The schema against which to validate the form data * @param schema
|
|
254
|
+
* @param formData - The form data to validate
|
|
255
|
+
* @throws - Error when the schema provided does not match the base schema of the precompiled validator
|
|
388
256
|
*/
|
|
389
257
|
rawValidation(schema, formData) {
|
|
390
|
-
if (!
|
|
391
|
-
throw new Error(
|
|
258
|
+
if (!isEqual(schema, this.resolvedRootSchema)) {
|
|
259
|
+
throw new Error(
|
|
260
|
+
"The schema associated with the precompiled schema differs from the schema provided for validation"
|
|
261
|
+
);
|
|
392
262
|
}
|
|
393
263
|
this.mainValidator(formData);
|
|
394
|
-
if (typeof this.localizer ===
|
|
264
|
+
if (typeof this.localizer === "function") {
|
|
395
265
|
this.localizer(this.mainValidator.errors);
|
|
396
266
|
}
|
|
397
|
-
const errors = this.mainValidator.errors ||
|
|
398
|
-
// Clear errors to prevent persistent errors, see #1104
|
|
267
|
+
const errors = this.mainValidator.errors || void 0;
|
|
399
268
|
this.mainValidator.errors = null;
|
|
400
|
-
return {
|
|
401
|
-
errors: errors
|
|
402
|
-
};
|
|
269
|
+
return { errors };
|
|
403
270
|
}
|
|
404
|
-
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
|
|
405
|
-
* the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
|
|
406
|
-
* supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
|
|
407
|
-
* transform them in what ever way it chooses.
|
|
408
|
-
*
|
|
409
|
-
* @param formData - The form data to validate
|
|
410
|
-
* @param schema - The schema against which to validate the form data
|
|
411
|
-
* @param [customValidate] - An optional function that is used to perform custom validation
|
|
412
|
-
* @param [transformErrors] - An optional function that is used to transform errors after AJV validation
|
|
413
|
-
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
271
|
+
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
|
|
272
|
+
* the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
|
|
273
|
+
* supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
|
|
274
|
+
* transform them in what ever way it chooses.
|
|
275
|
+
*
|
|
276
|
+
* @param formData - The form data to validate
|
|
277
|
+
* @param schema - The schema against which to validate the form data
|
|
278
|
+
* @param [customValidate] - An optional function that is used to perform custom validation
|
|
279
|
+
* @param [transformErrors] - An optional function that is used to transform errors after AJV validation
|
|
280
|
+
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
414
281
|
*/
|
|
415
282
|
validateFormData(formData, schema, customValidate, transformErrors, uiSchema) {
|
|
416
283
|
const rawErrors = this.rawValidation(schema, formData);
|
|
417
284
|
return processRawValidationErrors(this, rawErrors, formData, schema, customValidate, transformErrors, uiSchema);
|
|
418
285
|
}
|
|
419
|
-
/** Validates data against a schema, returning true if the data is valid, or false otherwise. If the schema is
|
|
420
|
-
* invalid, then this function will return false.
|
|
421
|
-
*
|
|
422
|
-
* @param schema - The schema against which to validate the form data
|
|
423
|
-
* @param formData - The form data to validate
|
|
424
|
-
* @param rootSchema - The root schema used to provide $ref resolutions
|
|
425
|
-
* @returns - true if the formData validates against the schema, false otherwise
|
|
426
|
-
* @throws - Error when the schema provided does not match the base schema of the precompiled validator OR if there
|
|
427
|
-
* isn't a precompiled validator function associated with the schema
|
|
286
|
+
/** Validates data against a schema, returning true if the data is valid, or false otherwise. If the schema is
|
|
287
|
+
* invalid, then this function will return false.
|
|
288
|
+
*
|
|
289
|
+
* @param schema - The schema against which to validate the form data
|
|
290
|
+
* @param formData - The form data to validate
|
|
291
|
+
* @param rootSchema - The root schema used to provide $ref resolutions
|
|
292
|
+
* @returns - true if the formData validates against the schema, false otherwise
|
|
293
|
+
* @throws - Error when the schema provided does not match the base schema of the precompiled validator OR if there
|
|
294
|
+
* isn't a precompiled validator function associated with the schema
|
|
428
295
|
*/
|
|
429
296
|
isValid(schema, formData, rootSchema) {
|
|
430
|
-
if (!
|
|
431
|
-
throw new Error(
|
|
297
|
+
if (!isEqual(rootSchema, this.rootSchema)) {
|
|
298
|
+
throw new Error(
|
|
299
|
+
"The schema associated with the precompiled validator differs from the rootSchema provided for validation"
|
|
300
|
+
);
|
|
432
301
|
}
|
|
433
|
-
if (
|
|
302
|
+
if (get(schema, utils.ID_KEY) === utils.JUNK_OPTION_ID) {
|
|
434
303
|
return false;
|
|
435
304
|
}
|
|
436
305
|
const validator = this.getValidator(schema);
|
|
437
306
|
return validator(formData);
|
|
438
307
|
}
|
|
439
|
-
}
|
|
308
|
+
};
|
|
440
309
|
|
|
441
|
-
|
|
442
|
-
* is provided, it is used to translate the messages generated by the underlying AJV validation.
|
|
443
|
-
*
|
|
444
|
-
* NOTE: The `validateFns` parameter is an object obtained by importing from a precompiled validation file created via
|
|
445
|
-
* the `compileSchemaValidators()` function.
|
|
446
|
-
*
|
|
447
|
-
* @param validateFns - The map of the validation functions that are created by the `compileSchemaValidators()` function
|
|
448
|
-
* @param rootSchema - The root schema that was used with the `compileSchemaValidators()` function
|
|
449
|
-
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
450
|
-
* @returns - The precompiled validator implementation resulting from the set of parameters provided
|
|
451
|
-
*/
|
|
310
|
+
// src/createPrecompiledValidator.ts
|
|
452
311
|
function createPrecompiledValidator(validateFns, rootSchema, localizer) {
|
|
453
312
|
return new AJV8PrecompiledValidator(validateFns, rootSchema, localizer);
|
|
454
313
|
}
|
|
455
314
|
|
|
456
|
-
|
|
315
|
+
// src/index.ts
|
|
316
|
+
var src_default = customizeValidator();
|
|
457
317
|
|
|
458
318
|
exports.createPrecompiledValidator = createPrecompiledValidator;
|
|
459
319
|
exports.customizeValidator = customizeValidator;
|
|
460
|
-
exports
|
|
320
|
+
exports.default = src_default;
|
|
461
321
|
|
|
462
322
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
463
323
|
|
|
464
324
|
}));
|
|
465
|
-
//# sourceMappingURL=validator-ajv8.umd.development.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
|
|
2
|
+
import { CustomValidatorOptionsType } from './types';
|
|
3
|
+
import { compileSchemaValidatorsCode } from './compileSchemaValidatorsCode';
|
|
4
|
+
export { compileSchemaValidatorsCode };
|
|
5
|
+
/** The function used to compile a schema into an output file in the form that allows it to be used as a precompiled
|
|
6
|
+
* validator. The main reasons for using a precompiled validator is reducing code size, improving validation speed and,
|
|
7
|
+
* most importantly, avoiding dynamic code compilation when prohibited by a browser's Content Security Policy. For more
|
|
8
|
+
* information about AJV code compilation see: https://ajv.js.org/standalone.html
|
|
9
|
+
*
|
|
10
|
+
* @param schema - The schema to be compiled into a set of precompiled validators functions
|
|
11
|
+
* @param output - The name of the file into which the precompiled validator functions will be generated
|
|
12
|
+
* @param [options={}] - The set of `CustomValidatorOptionsType` information used to alter the AJV validator used for
|
|
13
|
+
* compiling the schema. They are the same options that are passed to the `customizeValidator()` function in
|
|
14
|
+
* order to modify the behavior of the regular AJV-based validator.
|
|
15
|
+
*/
|
|
16
|
+
export default function compileSchemaValidators<S extends StrictRJSFSchema = RJSFSchema>(schema: S, output: string, options?: CustomValidatorOptionsType): void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { compileSchemaValidatorsCode } from './compileSchemaValidatorsCode';
|
|
3
|
+
export { compileSchemaValidatorsCode };
|
|
4
|
+
/** The function used to compile a schema into an output file in the form that allows it to be used as a precompiled
|
|
5
|
+
* validator. The main reasons for using a precompiled validator is reducing code size, improving validation speed and,
|
|
6
|
+
* most importantly, avoiding dynamic code compilation when prohibited by a browser's Content Security Policy. For more
|
|
7
|
+
* information about AJV code compilation see: https://ajv.js.org/standalone.html
|
|
8
|
+
*
|
|
9
|
+
* @param schema - The schema to be compiled into a set of precompiled validators functions
|
|
10
|
+
* @param output - The name of the file into which the precompiled validator functions will be generated
|
|
11
|
+
* @param [options={}] - The set of `CustomValidatorOptionsType` information used to alter the AJV validator used for
|
|
12
|
+
* compiling the schema. They are the same options that are passed to the `customizeValidator()` function in
|
|
13
|
+
* order to modify the behavior of the regular AJV-based validator.
|
|
14
|
+
*/
|
|
15
|
+
export default function compileSchemaValidators(schema, output, options = {}) {
|
|
16
|
+
console.log('parsing the schema');
|
|
17
|
+
const moduleCode = compileSchemaValidatorsCode(schema, options);
|
|
18
|
+
console.log(`writing ${output}`);
|
|
19
|
+
fs.writeFileSync(output, moduleCode);
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=compileSchemaValidators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compileSchemaValidators.js","sourceRoot":"","sources":["../src/compileSchemaValidators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AAGpB,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAC;AAE5E,OAAO,EAAE,2BAA2B,EAAE,CAAC;AAEvC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,uBAAuB,CAC7C,MAAS,EACT,MAAc,EACd,UAAsC,EAAE;IAExC,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;IAElC,MAAM,UAAU,GAAG,2BAA2B,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChE,OAAO,CAAC,GAAG,CAAC,WAAW,MAAM,EAAE,CAAC,CAAC;IACjC,EAAE,CAAC,aAAa,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AACvC,CAAC"}
|