@rjsf/validator-ajv8 5.6.0 → 5.7.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/index.d.ts +47 -4
- package/dist/validator-ajv8.cjs.development.js +301 -98
- package/dist/validator-ajv8.cjs.development.js.map +1 -1
- package/dist/validator-ajv8.cjs.production.min.js +1 -1
- package/dist/validator-ajv8.cjs.production.min.js.map +1 -1
- package/dist/validator-ajv8.esm.js +297 -99
- package/dist/validator-ajv8.esm.js.map +1 -1
- package/dist/validator-ajv8.umd.development.js +301 -101
- package/dist/validator-ajv8.umd.development.js.map +1 -1
- package/dist/validator-ajv8.umd.production.min.js +1 -1
- package/dist/validator-ajv8.umd.production.min.js.map +1 -1
- package/package.json +16 -14
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as _rjsf_utils from '@rjsf/utils';
|
|
2
2
|
import { StrictRJSFSchema, RJSFSchema, FormContextType, ValidatorType } from '@rjsf/utils';
|
|
3
|
-
import Ajv, { Options, ErrorObject } from 'ajv';
|
|
3
|
+
import Ajv, { Options, ErrorObject, ValidateFunction } from 'ajv';
|
|
4
4
|
import { FormatsPluginOptions } from 'ajv-formats';
|
|
5
|
+
import { DataValidationCxt } from 'ajv/lib/types';
|
|
5
6
|
|
|
6
7
|
/** The type describing how to customize the AJV6 validator
|
|
7
8
|
*/
|
|
@@ -21,16 +22,58 @@ interface CustomValidatorOptionsType {
|
|
|
21
22
|
}
|
|
22
23
|
/** The type describing a function that takes a list of Ajv `ErrorObject`s and localizes them
|
|
23
24
|
*/
|
|
24
|
-
type Localizer = (errors?: null | ErrorObject[]) => void;
|
|
25
|
+
type Localizer = (errors?: null | ErrorObject[]) => void;
|
|
26
|
+
/** Extend ValidateFunction to Omit its two required properties, `schema` and `schemaEnv` that are not produced by the
|
|
27
|
+
* AJV schema standalone compilation code
|
|
28
|
+
*/
|
|
29
|
+
interface CompiledValidateFunction<T = any> extends Omit<ValidateFunction<T>, 'schema' | 'schemaEnv'> {
|
|
30
|
+
/** This is almost an exact copy from the `ValidateFunction` type definition from which it extends because it seems to
|
|
31
|
+
* get lost as part of the Omit<> and this one simply returns boolean rather than `data is T`.
|
|
32
|
+
*/
|
|
33
|
+
(this: Ajv | any, data: any, dataCxt?: DataValidationCxt): boolean;
|
|
34
|
+
}
|
|
35
|
+
/** The definition of precompiled validator functions
|
|
36
|
+
*/
|
|
37
|
+
type ValidatorFunctions<T = any> = {
|
|
38
|
+
[key: string]: CompiledValidateFunction<T>;
|
|
39
|
+
};
|
|
25
40
|
|
|
26
41
|
/** Creates and returns a customized implementation of the `ValidatorType` with the given customization `options` if
|
|
27
|
-
* provided.
|
|
42
|
+
* provided. If a `localizer` is provided, it is used to translate the messages generated by the underlying AJV
|
|
43
|
+
* validation.
|
|
28
44
|
*
|
|
29
45
|
* @param [options={}] - The `CustomValidatorOptionsType` options that are used to create the `ValidatorType` instance
|
|
30
46
|
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
47
|
+
* @returns - The custom validator implementation resulting from the set of parameters provided
|
|
31
48
|
*/
|
|
32
49
|
declare function customizeValidator<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(options?: CustomValidatorOptionsType, localizer?: Localizer): ValidatorType<T, S, F>;
|
|
33
50
|
|
|
51
|
+
/** The function used to compile a schema into an output file in the form that allows it to be used as a precompiled
|
|
52
|
+
* validator. The main reasons for using a precompiled validator is reducing code size, improving validation speed and,
|
|
53
|
+
* most importantly, avoiding dynamic code compilation when prohibited by a browser's Content Security Policy. For more
|
|
54
|
+
* information about AJV code compilation see: https://ajv.js.org/standalone.html
|
|
55
|
+
*
|
|
56
|
+
* @param schema - The schema to be compiled into a set of precompiled validators functions
|
|
57
|
+
* @param output - The name of the file into which the precompiled validator functions will be generated
|
|
58
|
+
* @param [options={}] - The set of `CustomValidatorOptionsType` information used to alter the AJV validator used for
|
|
59
|
+
* compiling the schema. They are the same options that are passed to the `customizeValidator()` function in
|
|
60
|
+
* order to modify the behavior of the regular AJV-based validator.
|
|
61
|
+
*/
|
|
62
|
+
declare function compileSchemaValidators<S extends StrictRJSFSchema = RJSFSchema>(schema: S, output: string, options?: CustomValidatorOptionsType): void;
|
|
63
|
+
|
|
64
|
+
/** Creates and returns a `ValidatorType` interface that is implemented with a precompiled validator. If a `localizer`
|
|
65
|
+
* is provided, it is used to translate the messages generated by the underlying AJV validation.
|
|
66
|
+
*
|
|
67
|
+
* NOTE: The `validateFns` parameter is an object obtained by importing from a precompiled validation file created via
|
|
68
|
+
* the `compileSchemaValidators()` function.
|
|
69
|
+
*
|
|
70
|
+
* @param validateFns - The map of the validation functions that are created by the `compileSchemaValidators()` function
|
|
71
|
+
* @param rootSchema - The root schema that was used with the `compileSchemaValidators()` function
|
|
72
|
+
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
73
|
+
* @returns - The precompiled validator implementation resulting from the set of parameters provided
|
|
74
|
+
*/
|
|
75
|
+
declare function createPrecompiledValidator<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validateFns: ValidatorFunctions<T>, rootSchema: S, localizer?: Localizer): ValidatorType<T, S, F>;
|
|
76
|
+
|
|
34
77
|
declare const _default: _rjsf_utils.ValidatorType<any, _rjsf_utils.RJSFSchema, any>;
|
|
35
78
|
|
|
36
|
-
export { CustomValidatorOptionsType, Localizer, customizeValidator, _default as default };
|
|
79
|
+
export { CompiledValidateFunction, CustomValidatorOptionsType, Localizer, ValidatorFunctions, compileSchemaValidators, createPrecompiledValidator, customizeValidator, _default as default };
|
|
@@ -2,18 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var get = require('lodash/get');
|
|
6
5
|
var utils = require('@rjsf/utils');
|
|
7
6
|
var Ajv = require('ajv');
|
|
8
7
|
var addFormats = require('ajv-formats');
|
|
9
8
|
var isObject = require('lodash/isObject');
|
|
9
|
+
var get = require('lodash/get');
|
|
10
|
+
var fs = require('fs');
|
|
11
|
+
var standaloneCode = require('ajv/dist/standalone');
|
|
12
|
+
var isEqual = require('lodash/isEqual');
|
|
10
13
|
|
|
11
14
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
15
|
|
|
13
|
-
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
|
|
14
16
|
var Ajv__default = /*#__PURE__*/_interopDefaultLegacy(Ajv);
|
|
15
17
|
var addFormats__default = /*#__PURE__*/_interopDefaultLegacy(addFormats);
|
|
16
18
|
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
|
|
19
|
+
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
|
|
20
|
+
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
21
|
+
var standaloneCode__default = /*#__PURE__*/_interopDefaultLegacy(standaloneCode);
|
|
22
|
+
var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
|
|
17
23
|
|
|
18
24
|
function _extends() {
|
|
19
25
|
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
@@ -98,6 +104,110 @@ function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverr
|
|
|
98
104
|
}
|
|
99
105
|
|
|
100
106
|
var _excluded = ["instancePath", "keyword", "params", "schemaPath", "parentSchema"];
|
|
107
|
+
/** Transforming the error output from ajv to format used by @rjsf/utils.
|
|
108
|
+
* At some point, components should be updated to support ajv.
|
|
109
|
+
*
|
|
110
|
+
* @param errors - The list of AJV errors to convert to `RJSFValidationErrors`
|
|
111
|
+
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
112
|
+
*/
|
|
113
|
+
function transformRJSFValidationErrors(errors, uiSchema) {
|
|
114
|
+
if (errors === void 0) {
|
|
115
|
+
errors = [];
|
|
116
|
+
}
|
|
117
|
+
return errors.map(function (e) {
|
|
118
|
+
var instancePath = e.instancePath,
|
|
119
|
+
keyword = e.keyword,
|
|
120
|
+
params = e.params,
|
|
121
|
+
schemaPath = e.schemaPath,
|
|
122
|
+
parentSchema = e.parentSchema,
|
|
123
|
+
rest = _objectWithoutPropertiesLoose(e, _excluded);
|
|
124
|
+
var _rest$message = rest.message,
|
|
125
|
+
message = _rest$message === void 0 ? '' : _rest$message;
|
|
126
|
+
var property = instancePath.replace(/\//g, '.');
|
|
127
|
+
var stack = (property + " " + message).trim();
|
|
128
|
+
if ('missingProperty' in params) {
|
|
129
|
+
property = property ? property + "." + params.missingProperty : params.missingProperty;
|
|
130
|
+
var currentProperty = params.missingProperty;
|
|
131
|
+
var uiSchemaTitle = utils.getUiOptions(get__default["default"](uiSchema, "" + property.replace(/^\./, ''))).title;
|
|
132
|
+
if (uiSchemaTitle) {
|
|
133
|
+
message = message.replace(currentProperty, uiSchemaTitle);
|
|
134
|
+
} else {
|
|
135
|
+
var parentSchemaTitle = get__default["default"](parentSchema, [utils.PROPERTIES_KEY, currentProperty, 'title']);
|
|
136
|
+
if (parentSchemaTitle) {
|
|
137
|
+
message = message.replace(currentProperty, parentSchemaTitle);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
stack = message;
|
|
141
|
+
} else {
|
|
142
|
+
var _uiSchemaTitle = utils.getUiOptions(get__default["default"](uiSchema, "" + property.replace(/^\./, ''))).title;
|
|
143
|
+
if (_uiSchemaTitle) {
|
|
144
|
+
stack = ("'" + _uiSchemaTitle + "' " + message).trim();
|
|
145
|
+
} else {
|
|
146
|
+
var _parentSchemaTitle = parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.title;
|
|
147
|
+
if (_parentSchemaTitle) {
|
|
148
|
+
stack = ("'" + _parentSchemaTitle + "' " + message).trim();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// put data in expected format
|
|
153
|
+
return {
|
|
154
|
+
name: keyword,
|
|
155
|
+
property: property,
|
|
156
|
+
message: message,
|
|
157
|
+
params: params,
|
|
158
|
+
stack: stack,
|
|
159
|
+
schemaPath: schemaPath
|
|
160
|
+
};
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
|
|
164
|
+
* the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
|
|
165
|
+
* supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
|
|
166
|
+
* transform them in what ever way it chooses.
|
|
167
|
+
*
|
|
168
|
+
* @param validator - The `ValidatorType` implementation used for the `getDefaultFormState()` call
|
|
169
|
+
* @param rawErrors - The list of raw `ErrorObject`s to process
|
|
170
|
+
* @param formData - The form data to validate
|
|
171
|
+
* @param schema - The schema against which to validate the form data
|
|
172
|
+
* @param [customValidate] - An optional function that is used to perform custom validation
|
|
173
|
+
* @param [transformErrors] - An optional function that is used to transform errors after AJV validation
|
|
174
|
+
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
175
|
+
*/
|
|
176
|
+
function processRawValidationErrors(validator, rawErrors, formData, schema, customValidate, transformErrors, uiSchema) {
|
|
177
|
+
var invalidSchemaError = rawErrors.validationError;
|
|
178
|
+
var errors = transformRJSFValidationErrors(rawErrors.errors, uiSchema);
|
|
179
|
+
if (invalidSchemaError) {
|
|
180
|
+
errors = [].concat(errors, [{
|
|
181
|
+
stack: invalidSchemaError.message
|
|
182
|
+
}]);
|
|
183
|
+
}
|
|
184
|
+
if (typeof transformErrors === 'function') {
|
|
185
|
+
errors = transformErrors(errors, uiSchema);
|
|
186
|
+
}
|
|
187
|
+
var errorSchema = utils.toErrorSchema(errors);
|
|
188
|
+
if (invalidSchemaError) {
|
|
189
|
+
errorSchema = _extends({}, errorSchema, {
|
|
190
|
+
$schema: {
|
|
191
|
+
__errors: [invalidSchemaError.message]
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
if (typeof customValidate !== 'function') {
|
|
196
|
+
return {
|
|
197
|
+
errors: errors,
|
|
198
|
+
errorSchema: errorSchema
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
// Include form data with undefined values, which is required for custom validation.
|
|
202
|
+
var newFormData = utils.getDefaultFormState(validator, schema, formData, schema, true);
|
|
203
|
+
var errorHandler = customValidate(newFormData, utils.createErrorHandler(newFormData), uiSchema);
|
|
204
|
+
var userErrorSchema = utils.unwrapErrorHandler(errorHandler);
|
|
205
|
+
return utils.validationDataMerge({
|
|
206
|
+
errors: errors,
|
|
207
|
+
errorSchema: errorSchema
|
|
208
|
+
}, userErrorSchema);
|
|
209
|
+
}
|
|
210
|
+
|
|
101
211
|
/** `ValidatorType` implementation that uses the AJV 8 validation mechanism.
|
|
102
212
|
*/
|
|
103
213
|
var AJV8Validator = /*#__PURE__*/function () {
|
|
@@ -139,63 +249,6 @@ var AJV8Validator = /*#__PURE__*/function () {
|
|
|
139
249
|
}
|
|
140
250
|
return utils.toErrorList(errorSchema, fieldPath);
|
|
141
251
|
}
|
|
142
|
-
/** Transforming the error output from ajv to format used by @rjsf/utils.
|
|
143
|
-
* At some point, components should be updated to support ajv.
|
|
144
|
-
*
|
|
145
|
-
* @param errors - The list of AJV errors to convert to `RJSFValidationErrors`
|
|
146
|
-
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
147
|
-
* @protected
|
|
148
|
-
*/;
|
|
149
|
-
_proto.transformRJSFValidationErrors = function transformRJSFValidationErrors(errors, uiSchema) {
|
|
150
|
-
if (errors === void 0) {
|
|
151
|
-
errors = [];
|
|
152
|
-
}
|
|
153
|
-
return errors.map(function (e) {
|
|
154
|
-
var instancePath = e.instancePath,
|
|
155
|
-
keyword = e.keyword,
|
|
156
|
-
params = e.params,
|
|
157
|
-
schemaPath = e.schemaPath,
|
|
158
|
-
parentSchema = e.parentSchema,
|
|
159
|
-
rest = _objectWithoutPropertiesLoose(e, _excluded);
|
|
160
|
-
var _rest$message = rest.message,
|
|
161
|
-
message = _rest$message === void 0 ? '' : _rest$message;
|
|
162
|
-
var property = instancePath.replace(/\//g, '.');
|
|
163
|
-
var stack = (property + " " + message).trim();
|
|
164
|
-
if ('missingProperty' in params) {
|
|
165
|
-
property = property ? property + "." + params.missingProperty : params.missingProperty;
|
|
166
|
-
var currentProperty = params.missingProperty;
|
|
167
|
-
var uiSchemaTitle = utils.getUiOptions(get__default["default"](uiSchema, "" + property.replace(/^\./, ''))).title;
|
|
168
|
-
if (uiSchemaTitle) {
|
|
169
|
-
message = message.replace(currentProperty, uiSchemaTitle);
|
|
170
|
-
} else {
|
|
171
|
-
var parentSchemaTitle = get__default["default"](parentSchema, [utils.PROPERTIES_KEY, currentProperty, 'title']);
|
|
172
|
-
if (parentSchemaTitle) {
|
|
173
|
-
message = message.replace(currentProperty, parentSchemaTitle);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
stack = message;
|
|
177
|
-
} else {
|
|
178
|
-
var _uiSchemaTitle = utils.getUiOptions(get__default["default"](uiSchema, "" + property.replace(/^\./, ''))).title;
|
|
179
|
-
if (_uiSchemaTitle) {
|
|
180
|
-
stack = ("'" + _uiSchemaTitle + "' " + message).trim();
|
|
181
|
-
} else {
|
|
182
|
-
var _parentSchemaTitle = parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.title;
|
|
183
|
-
if (_parentSchemaTitle) {
|
|
184
|
-
stack = ("'" + _parentSchemaTitle + "' " + message).trim();
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
// put data in expected format
|
|
189
|
-
return {
|
|
190
|
-
name: keyword,
|
|
191
|
-
property: property,
|
|
192
|
-
message: message,
|
|
193
|
-
params: params,
|
|
194
|
-
stack: stack,
|
|
195
|
-
schemaPath: schemaPath
|
|
196
|
-
};
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
252
|
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
|
|
200
253
|
* by the playground. Returns the `errors` from the validation
|
|
201
254
|
*
|
|
@@ -205,8 +258,8 @@ var AJV8Validator = /*#__PURE__*/function () {
|
|
|
205
258
|
_proto.rawValidation = function rawValidation(schema, formData) {
|
|
206
259
|
var compilationError = undefined;
|
|
207
260
|
var compiledValidator;
|
|
208
|
-
if (schema[
|
|
209
|
-
compiledValidator = this.ajv.getSchema(schema[
|
|
261
|
+
if (schema[utils.ID_KEY]) {
|
|
262
|
+
compiledValidator = this.ajv.getSchema(schema[utils.ID_KEY]);
|
|
210
263
|
}
|
|
211
264
|
try {
|
|
212
265
|
if (compiledValidator === undefined) {
|
|
@@ -243,38 +296,7 @@ var AJV8Validator = /*#__PURE__*/function () {
|
|
|
243
296
|
*/;
|
|
244
297
|
_proto.validateFormData = function validateFormData(formData, schema, customValidate, transformErrors, uiSchema) {
|
|
245
298
|
var rawErrors = this.rawValidation(schema, formData);
|
|
246
|
-
|
|
247
|
-
var errors = this.transformRJSFValidationErrors(rawErrors.errors, uiSchema);
|
|
248
|
-
if (invalidSchemaError) {
|
|
249
|
-
errors = [].concat(errors, [{
|
|
250
|
-
stack: invalidSchemaError.message
|
|
251
|
-
}]);
|
|
252
|
-
}
|
|
253
|
-
if (typeof transformErrors === 'function') {
|
|
254
|
-
errors = transformErrors(errors, uiSchema);
|
|
255
|
-
}
|
|
256
|
-
var errorSchema = utils.toErrorSchema(errors);
|
|
257
|
-
if (invalidSchemaError) {
|
|
258
|
-
errorSchema = _extends({}, errorSchema, {
|
|
259
|
-
$schema: {
|
|
260
|
-
__errors: [invalidSchemaError.message]
|
|
261
|
-
}
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
if (typeof customValidate !== 'function') {
|
|
265
|
-
return {
|
|
266
|
-
errors: errors,
|
|
267
|
-
errorSchema: errorSchema
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
// Include form data with undefined values, which is required for custom validation.
|
|
271
|
-
var newFormData = utils.getDefaultFormState(this, schema, formData, schema, true);
|
|
272
|
-
var errorHandler = customValidate(newFormData, utils.createErrorHandler(newFormData), uiSchema);
|
|
273
|
-
var userErrorSchema = utils.unwrapErrorHandler(errorHandler);
|
|
274
|
-
return utils.validationDataMerge({
|
|
275
|
-
errors: errors,
|
|
276
|
-
errorSchema: errorSchema
|
|
277
|
-
}, userErrorSchema);
|
|
299
|
+
return processRawValidationErrors(this, rawErrors, formData, schema, customValidate, transformErrors, uiSchema);
|
|
278
300
|
}
|
|
279
301
|
/** Validates data against a schema, returning true if the data is valid, or
|
|
280
302
|
* false otherwise. If the schema is invalid, then this function will return
|
|
@@ -285,8 +307,8 @@ var AJV8Validator = /*#__PURE__*/function () {
|
|
|
285
307
|
* @param rootSchema - The root schema used to provide $ref resolutions
|
|
286
308
|
*/;
|
|
287
309
|
_proto.isValid = function isValid(schema, formData, rootSchema) {
|
|
288
|
-
var _rootSchema
|
|
289
|
-
var rootSchemaId = (_rootSchema
|
|
310
|
+
var _rootSchema$ID_KEY;
|
|
311
|
+
var rootSchemaId = (_rootSchema$ID_KEY = rootSchema[utils.ID_KEY]) != null ? _rootSchema$ID_KEY : utils.ROOT_SCHEMA_PREFIX;
|
|
290
312
|
try {
|
|
291
313
|
// add the rootSchema ROOT_SCHEMA_PREFIX as id.
|
|
292
314
|
// then rewrite the schema ref's to point to the rootSchema
|
|
@@ -297,8 +319,8 @@ var AJV8Validator = /*#__PURE__*/function () {
|
|
|
297
319
|
}
|
|
298
320
|
var schemaWithIdRefPrefix = utils.withIdRefPrefix(schema);
|
|
299
321
|
var compiledValidator;
|
|
300
|
-
if (schemaWithIdRefPrefix[
|
|
301
|
-
compiledValidator = this.ajv.getSchema(schemaWithIdRefPrefix[
|
|
322
|
+
if (schemaWithIdRefPrefix[utils.ID_KEY]) {
|
|
323
|
+
compiledValidator = this.ajv.getSchema(schemaWithIdRefPrefix[utils.ID_KEY]);
|
|
302
324
|
}
|
|
303
325
|
if (compiledValidator === undefined) {
|
|
304
326
|
compiledValidator = this.ajv.compile(schemaWithIdRefPrefix);
|
|
@@ -318,10 +340,12 @@ var AJV8Validator = /*#__PURE__*/function () {
|
|
|
318
340
|
}();
|
|
319
341
|
|
|
320
342
|
/** Creates and returns a customized implementation of the `ValidatorType` with the given customization `options` if
|
|
321
|
-
* provided.
|
|
343
|
+
* provided. If a `localizer` is provided, it is used to translate the messages generated by the underlying AJV
|
|
344
|
+
* validation.
|
|
322
345
|
*
|
|
323
346
|
* @param [options={}] - The `CustomValidatorOptionsType` options that are used to create the `ValidatorType` instance
|
|
324
347
|
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
348
|
+
* @returns - The custom validator implementation resulting from the set of parameters provided
|
|
325
349
|
*/
|
|
326
350
|
function customizeValidator(options, localizer) {
|
|
327
351
|
if (options === void 0) {
|
|
@@ -330,8 +354,187 @@ function customizeValidator(options, localizer) {
|
|
|
330
354
|
return new AJV8Validator(options, localizer);
|
|
331
355
|
}
|
|
332
356
|
|
|
357
|
+
/** The function used to compile a schema into an output file in the form that allows it to be used as a precompiled
|
|
358
|
+
* validator. The main reasons for using a precompiled validator is reducing code size, improving validation speed and,
|
|
359
|
+
* most importantly, avoiding dynamic code compilation when prohibited by a browser's Content Security Policy. For more
|
|
360
|
+
* information about AJV code compilation see: https://ajv.js.org/standalone.html
|
|
361
|
+
*
|
|
362
|
+
* @param schema - The schema to be compiled into a set of precompiled validators functions
|
|
363
|
+
* @param output - The name of the file into which the precompiled validator functions will be generated
|
|
364
|
+
* @param [options={}] - The set of `CustomValidatorOptionsType` information used to alter the AJV validator used for
|
|
365
|
+
* compiling the schema. They are the same options that are passed to the `customizeValidator()` function in
|
|
366
|
+
* order to modify the behavior of the regular AJV-based validator.
|
|
367
|
+
*/
|
|
368
|
+
function compileSchemaValidators(schema, output, options) {
|
|
369
|
+
if (options === void 0) {
|
|
370
|
+
options = {};
|
|
371
|
+
}
|
|
372
|
+
console.log('parsing the schema');
|
|
373
|
+
var schemaMaps = utils.schemaParser(schema);
|
|
374
|
+
var schemas = Object.values(schemaMaps);
|
|
375
|
+
var _options = options,
|
|
376
|
+
additionalMetaSchemas = _options.additionalMetaSchemas,
|
|
377
|
+
customFormats = _options.customFormats,
|
|
378
|
+
_options$ajvOptionsOv = _options.ajvOptionsOverrides,
|
|
379
|
+
ajvOptionsOverrides = _options$ajvOptionsOv === void 0 ? {} : _options$ajvOptionsOv,
|
|
380
|
+
ajvFormatOptions = _options.ajvFormatOptions,
|
|
381
|
+
AjvClass = _options.AjvClass;
|
|
382
|
+
// Allow users to turn off the `lines: true` feature in their own overrides, but NOT the `source: true`
|
|
383
|
+
var compileOptions = _extends({}, ajvOptionsOverrides, {
|
|
384
|
+
code: _extends({
|
|
385
|
+
lines: true
|
|
386
|
+
}, ajvOptionsOverrides.code, {
|
|
387
|
+
source: true
|
|
388
|
+
}),
|
|
389
|
+
schemas: schemas
|
|
390
|
+
});
|
|
391
|
+
var ajv = createAjvInstance(additionalMetaSchemas, customFormats, compileOptions, ajvFormatOptions, AjvClass);
|
|
392
|
+
var moduleCode = standaloneCode__default["default"](ajv);
|
|
393
|
+
console.log("writing " + output);
|
|
394
|
+
fs__default["default"].writeFileSync(output, moduleCode);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** `ValidatorType` implementation that uses an AJV 8 precompiled validator as created by the
|
|
398
|
+
* `compileSchemaValidators()` function provided by the `@rjsf/validator-ajv8` library.
|
|
399
|
+
*/
|
|
400
|
+
var AJV8PrecompiledValidator = /*#__PURE__*/function () {
|
|
401
|
+
/** Constructs an `AJV8PrecompiledValidator` instance using the `validateFns` and `rootSchema`
|
|
402
|
+
*
|
|
403
|
+
* @param validateFns - The map of the validation functions that are generated by the `schemaCompile()` function
|
|
404
|
+
* @param rootSchema - The root schema that was used with the `compileSchema()` function
|
|
405
|
+
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
406
|
+
* @throws - Error when the base schema of the precompiled validator does not have a matching validator function
|
|
407
|
+
*/
|
|
408
|
+
function AJV8PrecompiledValidator(validateFns, rootSchema, localizer) {
|
|
409
|
+
/** The root schema object used to construct this validator
|
|
410
|
+
*
|
|
411
|
+
* @private
|
|
412
|
+
*/
|
|
413
|
+
this.rootSchema = void 0;
|
|
414
|
+
/** The `ValidatorFunctions` map used to construct this validator
|
|
415
|
+
*
|
|
416
|
+
* @private
|
|
417
|
+
*/
|
|
418
|
+
this.validateFns = void 0;
|
|
419
|
+
/** The main validator function associated with the base schema in the `precompiledValidator`
|
|
420
|
+
*
|
|
421
|
+
* @private
|
|
422
|
+
*/
|
|
423
|
+
this.mainValidator = void 0;
|
|
424
|
+
/** The Localizer function to use for localizing Ajv errors
|
|
425
|
+
*
|
|
426
|
+
* @private
|
|
427
|
+
*/
|
|
428
|
+
this.localizer = void 0;
|
|
429
|
+
this.rootSchema = rootSchema;
|
|
430
|
+
this.validateFns = validateFns;
|
|
431
|
+
this.localizer = localizer;
|
|
432
|
+
this.mainValidator = this.getValidator(rootSchema);
|
|
433
|
+
}
|
|
434
|
+
/** Returns the precompiled validator associated with the given `schema` from the map of precompiled validator
|
|
435
|
+
* functions.
|
|
436
|
+
*
|
|
437
|
+
* @param schema - The schema for which a precompiled validator function is desired
|
|
438
|
+
* @returns - The precompiled validator function associated with this schema
|
|
439
|
+
*/
|
|
440
|
+
var _proto = AJV8PrecompiledValidator.prototype;
|
|
441
|
+
_proto.getValidator = function getValidator(schema) {
|
|
442
|
+
var key = get__default["default"](schema, utils.ID_KEY) || utils.hashForSchema(schema);
|
|
443
|
+
var validator = this.validateFns[key];
|
|
444
|
+
if (!validator) {
|
|
445
|
+
throw new Error("No precompiled validator function was found for the given schema for \"" + key + "\"");
|
|
446
|
+
}
|
|
447
|
+
return validator;
|
|
448
|
+
}
|
|
449
|
+
/** Converts an `errorSchema` into a list of `RJSFValidationErrors`
|
|
450
|
+
*
|
|
451
|
+
* @param errorSchema - The `ErrorSchema` instance to convert
|
|
452
|
+
* @param [fieldPath=[]] - The current field path, defaults to [] if not specified
|
|
453
|
+
* @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in
|
|
454
|
+
* the next major release.
|
|
455
|
+
*/;
|
|
456
|
+
_proto.toErrorList = function toErrorList(errorSchema, fieldPath) {
|
|
457
|
+
if (fieldPath === void 0) {
|
|
458
|
+
fieldPath = [];
|
|
459
|
+
}
|
|
460
|
+
return utils.toErrorList(errorSchema, fieldPath);
|
|
461
|
+
}
|
|
462
|
+
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
|
|
463
|
+
* by the playground. Returns the `errors` from the validation
|
|
464
|
+
*
|
|
465
|
+
* @param schema - The schema against which to validate the form data * @param schema
|
|
466
|
+
* @param formData - The form data to validate
|
|
467
|
+
* @throws - Error when the schema provided does not match the base schema of the precompiled validator
|
|
468
|
+
*/;
|
|
469
|
+
_proto.rawValidation = function rawValidation(schema, formData) {
|
|
470
|
+
if (!isEqual__default["default"](schema, this.rootSchema)) {
|
|
471
|
+
throw new Error('The schema associated with the precompiled schema differs from the schema provided for validation');
|
|
472
|
+
}
|
|
473
|
+
this.mainValidator(formData);
|
|
474
|
+
if (typeof this.localizer === 'function') {
|
|
475
|
+
this.localizer(this.mainValidator.errors);
|
|
476
|
+
}
|
|
477
|
+
var errors = this.mainValidator.errors || undefined;
|
|
478
|
+
// Clear errors to prevent persistent errors, see #1104
|
|
479
|
+
this.mainValidator.errors = null;
|
|
480
|
+
return {
|
|
481
|
+
errors: errors
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
|
|
485
|
+
* the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
|
|
486
|
+
* supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
|
|
487
|
+
* transform them in what ever way it chooses.
|
|
488
|
+
*
|
|
489
|
+
* @param formData - The form data to validate
|
|
490
|
+
* @param schema - The schema against which to validate the form data
|
|
491
|
+
* @param [customValidate] - An optional function that is used to perform custom validation
|
|
492
|
+
* @param [transformErrors] - An optional function that is used to transform errors after AJV validation
|
|
493
|
+
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
494
|
+
*/;
|
|
495
|
+
_proto.validateFormData = function validateFormData(formData, schema, customValidate, transformErrors, uiSchema) {
|
|
496
|
+
var rawErrors = this.rawValidation(schema, formData);
|
|
497
|
+
return processRawValidationErrors(this, rawErrors, formData, schema, customValidate, transformErrors, uiSchema);
|
|
498
|
+
}
|
|
499
|
+
/** Validates data against a schema, returning true if the data is valid, or false otherwise. If the schema is
|
|
500
|
+
* invalid, then this function will return false.
|
|
501
|
+
*
|
|
502
|
+
* @param schema - The schema against which to validate the form data
|
|
503
|
+
* @param formData - The form data to validate
|
|
504
|
+
* @param rootSchema - The root schema used to provide $ref resolutions
|
|
505
|
+
* @returns - true if the formData validates against the schema, false otherwise
|
|
506
|
+
* @throws - Error when the schema provided does not match the base schema of the precompiled validator OR if there
|
|
507
|
+
* isn't a precompiled validator function associated with the schema
|
|
508
|
+
*/;
|
|
509
|
+
_proto.isValid = function isValid(schema, formData, rootSchema) {
|
|
510
|
+
if (!isEqual__default["default"](rootSchema, this.rootSchema)) {
|
|
511
|
+
throw new Error('The schema associated with the precompiled validator differs from the rootSchema provided for validation');
|
|
512
|
+
}
|
|
513
|
+
var validator = this.getValidator(schema);
|
|
514
|
+
return validator(formData);
|
|
515
|
+
};
|
|
516
|
+
return AJV8PrecompiledValidator;
|
|
517
|
+
}();
|
|
518
|
+
|
|
519
|
+
/** Creates and returns a `ValidatorType` interface that is implemented with a precompiled validator. If a `localizer`
|
|
520
|
+
* is provided, it is used to translate the messages generated by the underlying AJV validation.
|
|
521
|
+
*
|
|
522
|
+
* NOTE: The `validateFns` parameter is an object obtained by importing from a precompiled validation file created via
|
|
523
|
+
* the `compileSchemaValidators()` function.
|
|
524
|
+
*
|
|
525
|
+
* @param validateFns - The map of the validation functions that are created by the `compileSchemaValidators()` function
|
|
526
|
+
* @param rootSchema - The root schema that was used with the `compileSchemaValidators()` function
|
|
527
|
+
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
528
|
+
* @returns - The precompiled validator implementation resulting from the set of parameters provided
|
|
529
|
+
*/
|
|
530
|
+
function createPrecompiledValidator(validateFns, rootSchema, localizer) {
|
|
531
|
+
return new AJV8PrecompiledValidator(validateFns, rootSchema, localizer);
|
|
532
|
+
}
|
|
533
|
+
|
|
333
534
|
var index = /*#__PURE__*/customizeValidator();
|
|
334
535
|
|
|
536
|
+
exports.compileSchemaValidators = compileSchemaValidators;
|
|
537
|
+
exports.createPrecompiledValidator = createPrecompiledValidator;
|
|
335
538
|
exports.customizeValidator = customizeValidator;
|
|
336
539
|
exports["default"] = index;
|
|
337
540
|
//# sourceMappingURL=validator-ajv8.cjs.development.js.map
|