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