@rjsf/validator-ajv8 5.11.2 → 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
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { ErrorObject } from 'ajv';
|
|
2
|
+
import get from 'lodash/get';
|
|
3
|
+
import {
|
|
4
|
+
createErrorHandler,
|
|
5
|
+
CustomValidator,
|
|
6
|
+
ErrorTransformer,
|
|
7
|
+
FormContextType,
|
|
8
|
+
getDefaultFormState,
|
|
9
|
+
getUiOptions,
|
|
10
|
+
PROPERTIES_KEY,
|
|
11
|
+
RJSFSchema,
|
|
12
|
+
RJSFValidationError,
|
|
13
|
+
StrictRJSFSchema,
|
|
14
|
+
toErrorSchema,
|
|
15
|
+
UiSchema,
|
|
16
|
+
unwrapErrorHandler,
|
|
17
|
+
validationDataMerge,
|
|
18
|
+
ValidatorType,
|
|
19
|
+
} from '@rjsf/utils';
|
|
20
|
+
|
|
21
|
+
export type RawValidationErrorsType<Result = any> = { errors?: Result[]; validationError?: Error };
|
|
22
|
+
|
|
23
|
+
/** Transforming the error output from ajv to format used by @rjsf/utils.
|
|
24
|
+
* At some point, components should be updated to support ajv.
|
|
25
|
+
*
|
|
26
|
+
* @param errors - The list of AJV errors to convert to `RJSFValidationErrors`
|
|
27
|
+
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
28
|
+
*/
|
|
29
|
+
export function transformRJSFValidationErrors<
|
|
30
|
+
T = any,
|
|
31
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
32
|
+
F extends FormContextType = any
|
|
33
|
+
>(errors: ErrorObject[] = [], uiSchema?: UiSchema<T, S, F>): RJSFValidationError[] {
|
|
34
|
+
return errors.map((e: ErrorObject) => {
|
|
35
|
+
const { instancePath, keyword, params, schemaPath, parentSchema, ...rest } = e;
|
|
36
|
+
let { message = '' } = rest;
|
|
37
|
+
let property = instancePath.replace(/\//g, '.');
|
|
38
|
+
let stack = `${property} ${message}`.trim();
|
|
39
|
+
|
|
40
|
+
if ('missingProperty' in params) {
|
|
41
|
+
property = property ? `${property}.${params.missingProperty}` : params.missingProperty;
|
|
42
|
+
const currentProperty: string = params.missingProperty;
|
|
43
|
+
const uiSchemaTitle = getUiOptions(get(uiSchema, `${property.replace(/^\./, '')}`)).title;
|
|
44
|
+
|
|
45
|
+
if (uiSchemaTitle) {
|
|
46
|
+
message = message.replace(currentProperty, uiSchemaTitle);
|
|
47
|
+
} else {
|
|
48
|
+
const parentSchemaTitle = get(parentSchema, [PROPERTIES_KEY, currentProperty, 'title']);
|
|
49
|
+
|
|
50
|
+
if (parentSchemaTitle) {
|
|
51
|
+
message = message.replace(currentProperty, parentSchemaTitle);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
stack = message;
|
|
56
|
+
} else {
|
|
57
|
+
const uiSchemaTitle = getUiOptions<T, S, F>(get(uiSchema, `${property.replace(/^\./, '')}`)).title;
|
|
58
|
+
|
|
59
|
+
if (uiSchemaTitle) {
|
|
60
|
+
stack = `'${uiSchemaTitle}' ${message}`.trim();
|
|
61
|
+
} else {
|
|
62
|
+
const parentSchemaTitle = parentSchema?.title;
|
|
63
|
+
|
|
64
|
+
if (parentSchemaTitle) {
|
|
65
|
+
stack = `'${parentSchemaTitle}' ${message}`.trim();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// put data in expected format
|
|
71
|
+
return {
|
|
72
|
+
name: keyword,
|
|
73
|
+
property,
|
|
74
|
+
message,
|
|
75
|
+
params, // specific to ajv
|
|
76
|
+
stack,
|
|
77
|
+
schemaPath,
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
|
|
83
|
+
* the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
|
|
84
|
+
* supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
|
|
85
|
+
* transform them in what ever way it chooses.
|
|
86
|
+
*
|
|
87
|
+
* @param validator - The `ValidatorType` implementation used for the `getDefaultFormState()` call
|
|
88
|
+
* @param rawErrors - The list of raw `ErrorObject`s to process
|
|
89
|
+
* @param formData - The form data to validate
|
|
90
|
+
* @param schema - The schema against which to validate the form data
|
|
91
|
+
* @param [customValidate] - An optional function that is used to perform custom validation
|
|
92
|
+
* @param [transformErrors] - An optional function that is used to transform errors after AJV validation
|
|
93
|
+
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
94
|
+
*/
|
|
95
|
+
export default function processRawValidationErrors<
|
|
96
|
+
T = any,
|
|
97
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
98
|
+
F extends FormContextType = any
|
|
99
|
+
>(
|
|
100
|
+
validator: ValidatorType<T, S, F>,
|
|
101
|
+
rawErrors: RawValidationErrorsType<ErrorObject>,
|
|
102
|
+
formData: T | undefined,
|
|
103
|
+
schema: S,
|
|
104
|
+
customValidate?: CustomValidator<T, S, F>,
|
|
105
|
+
transformErrors?: ErrorTransformer<T, S, F>,
|
|
106
|
+
uiSchema?: UiSchema<T, S, F>
|
|
107
|
+
) {
|
|
108
|
+
const { validationError: invalidSchemaError } = rawErrors;
|
|
109
|
+
let errors = transformRJSFValidationErrors<T, S, F>(rawErrors.errors, uiSchema);
|
|
110
|
+
|
|
111
|
+
if (invalidSchemaError) {
|
|
112
|
+
errors = [...errors, { stack: invalidSchemaError!.message }];
|
|
113
|
+
}
|
|
114
|
+
if (typeof transformErrors === 'function') {
|
|
115
|
+
errors = transformErrors(errors, uiSchema);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let errorSchema = toErrorSchema<T>(errors);
|
|
119
|
+
|
|
120
|
+
if (invalidSchemaError) {
|
|
121
|
+
errorSchema = {
|
|
122
|
+
...errorSchema,
|
|
123
|
+
$schema: {
|
|
124
|
+
__errors: [invalidSchemaError!.message],
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (typeof customValidate !== 'function') {
|
|
130
|
+
return { errors, errorSchema };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Include form data with undefined values, which is required for custom validation.
|
|
134
|
+
const newFormData = getDefaultFormState<T, S, F>(validator, schema, formData, schema, true) as T;
|
|
135
|
+
|
|
136
|
+
const errorHandler = customValidate(newFormData, createErrorHandler<T>(newFormData), uiSchema);
|
|
137
|
+
const userErrorSchema = unwrapErrorHandler<T>(errorHandler);
|
|
138
|
+
return validationDataMerge<T>({ errors, errorSchema }, userErrorSchema);
|
|
139
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import Ajv, { Options, ErrorObject } from 'ajv';
|
|
2
|
+
import { FormatsPluginOptions } from 'ajv-formats';
|
|
3
|
+
|
|
4
|
+
/** The type describing how to customize the AJV6 validator
|
|
5
|
+
*/
|
|
6
|
+
export interface CustomValidatorOptionsType {
|
|
7
|
+
/** The list of additional meta schemas that the validator can access */
|
|
8
|
+
additionalMetaSchemas?: ReadonlyArray<object>;
|
|
9
|
+
/** The set of additional custom formats that the validator will support */
|
|
10
|
+
customFormats?: {
|
|
11
|
+
[k: string]: string | RegExp | ((data: string) => boolean);
|
|
12
|
+
};
|
|
13
|
+
/** The set of config overrides that will be passed to the AJV validator constructor on top of the defaults */
|
|
14
|
+
ajvOptionsOverrides?: Options;
|
|
15
|
+
/** The `ajv-format` options to use when adding formats to `ajv`; pass `false` to disable it */
|
|
16
|
+
ajvFormatOptions?: FormatsPluginOptions | false;
|
|
17
|
+
/** The AJV class to construct */
|
|
18
|
+
AjvClass?: typeof Ajv;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The type describing a function that takes a list of Ajv `ErrorObject`s and localizes them
|
|
22
|
+
*/
|
|
23
|
+
export type Localizer = (errors?: null | ErrorObject[]) => void;
|
|
24
|
+
|
|
25
|
+
/** This is a simplification of the `ValidateFunction` type provided by the AJV validator down to its minimal form so
|
|
26
|
+
* that the code still works properly with precompiled validator functions generated by the AJV standalone code
|
|
27
|
+
* compilation but does not rely on any of the internal AJV types which causes Typescript issue for library consumers
|
|
28
|
+
*/
|
|
29
|
+
export interface CompiledValidateFunction {
|
|
30
|
+
/** The errors produced by the precompiled validator */
|
|
31
|
+
errors?: null | ErrorObject[];
|
|
32
|
+
/** This is simplified version of a `ValidateFunction` type definition which describes the interface that our
|
|
33
|
+
* precompiled validator will call.
|
|
34
|
+
*/
|
|
35
|
+
(this: Ajv | any, data: any): boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The definition of precompiled validator functions
|
|
39
|
+
*/
|
|
40
|
+
export type ValidatorFunctions = { [key: string]: CompiledValidateFunction };
|
package/src/validator.ts
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import Ajv, { ErrorObject, ValidateFunction } from 'ajv';
|
|
2
|
+
import {
|
|
3
|
+
CustomValidator,
|
|
4
|
+
ErrorSchema,
|
|
5
|
+
ErrorTransformer,
|
|
6
|
+
FormContextType,
|
|
7
|
+
ID_KEY,
|
|
8
|
+
RJSFSchema,
|
|
9
|
+
ROOT_SCHEMA_PREFIX,
|
|
10
|
+
StrictRJSFSchema,
|
|
11
|
+
toErrorList,
|
|
12
|
+
UiSchema,
|
|
13
|
+
ValidationData,
|
|
14
|
+
ValidatorType,
|
|
15
|
+
withIdRefPrefix,
|
|
16
|
+
hashForSchema,
|
|
17
|
+
} from '@rjsf/utils';
|
|
18
|
+
|
|
19
|
+
import { CustomValidatorOptionsType, Localizer } from './types';
|
|
20
|
+
import createAjvInstance from './createAjvInstance';
|
|
21
|
+
import processRawValidationErrors, { RawValidationErrorsType } from './processRawValidationErrors';
|
|
22
|
+
|
|
23
|
+
/** `ValidatorType` implementation that uses the AJV 8 validation mechanism.
|
|
24
|
+
*/
|
|
25
|
+
export default class AJV8Validator<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>
|
|
26
|
+
implements ValidatorType<T, S, F>
|
|
27
|
+
{
|
|
28
|
+
/** The AJV instance to use for all validations
|
|
29
|
+
*
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
private ajv: Ajv;
|
|
33
|
+
|
|
34
|
+
/** The Localizer function to use for localizing Ajv errors
|
|
35
|
+
*
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
38
|
+
readonly localizer?: Localizer;
|
|
39
|
+
|
|
40
|
+
/** Constructs an `AJV8Validator` instance using the `options`
|
|
41
|
+
*
|
|
42
|
+
* @param options - The `CustomValidatorOptionsType` options that are used to create the AJV instance
|
|
43
|
+
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
|
|
44
|
+
*/
|
|
45
|
+
constructor(options: CustomValidatorOptionsType, localizer?: Localizer) {
|
|
46
|
+
const { additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass } = options;
|
|
47
|
+
this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass);
|
|
48
|
+
this.localizer = localizer;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Converts an `errorSchema` into a list of `RJSFValidationErrors`
|
|
52
|
+
*
|
|
53
|
+
* @param errorSchema - The `ErrorSchema` instance to convert
|
|
54
|
+
* @param [fieldPath=[]] - The current field path, defaults to [] if not specified
|
|
55
|
+
* @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in
|
|
56
|
+
* the next major release.
|
|
57
|
+
*/
|
|
58
|
+
toErrorList(errorSchema?: ErrorSchema<T>, fieldPath: string[] = []) {
|
|
59
|
+
return toErrorList(errorSchema, fieldPath);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
|
|
63
|
+
* by the playground. Returns the `errors` from the validation
|
|
64
|
+
*
|
|
65
|
+
* @param schema - The schema against which to validate the form data * @param schema
|
|
66
|
+
* @param formData - The form data to validate
|
|
67
|
+
*/
|
|
68
|
+
rawValidation<Result = any>(schema: S, formData?: T): RawValidationErrorsType<Result> {
|
|
69
|
+
let compilationError: Error | undefined = undefined;
|
|
70
|
+
let compiledValidator: ValidateFunction | undefined;
|
|
71
|
+
if (schema[ID_KEY]) {
|
|
72
|
+
compiledValidator = this.ajv.getSchema(schema[ID_KEY]);
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
if (compiledValidator === undefined) {
|
|
76
|
+
compiledValidator = this.ajv.compile(schema);
|
|
77
|
+
}
|
|
78
|
+
compiledValidator(formData);
|
|
79
|
+
} catch (err) {
|
|
80
|
+
compilationError = err as Error;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let errors;
|
|
84
|
+
if (compiledValidator) {
|
|
85
|
+
if (typeof this.localizer === 'function') {
|
|
86
|
+
this.localizer(compiledValidator.errors);
|
|
87
|
+
}
|
|
88
|
+
errors = compiledValidator.errors || undefined;
|
|
89
|
+
|
|
90
|
+
// Clear errors to prevent persistent errors, see #1104
|
|
91
|
+
compiledValidator.errors = null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
errors: errors as unknown as Result[],
|
|
96
|
+
validationError: compilationError,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
|
|
101
|
+
* the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
|
|
102
|
+
* supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
|
|
103
|
+
* transform them in what ever way it chooses.
|
|
104
|
+
*
|
|
105
|
+
* @param formData - The form data to validate
|
|
106
|
+
* @param schema - The schema against which to validate the form data
|
|
107
|
+
* @param [customValidate] - An optional function that is used to perform custom validation
|
|
108
|
+
* @param [transformErrors] - An optional function that is used to transform errors after AJV validation
|
|
109
|
+
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
|
|
110
|
+
*/
|
|
111
|
+
validateFormData(
|
|
112
|
+
formData: T | undefined,
|
|
113
|
+
schema: S,
|
|
114
|
+
customValidate?: CustomValidator<T, S, F>,
|
|
115
|
+
transformErrors?: ErrorTransformer<T, S, F>,
|
|
116
|
+
uiSchema?: UiSchema<T, S, F>
|
|
117
|
+
): ValidationData<T> {
|
|
118
|
+
const rawErrors = this.rawValidation<ErrorObject>(schema, formData);
|
|
119
|
+
return processRawValidationErrors(this, rawErrors, formData, schema, customValidate, transformErrors, uiSchema);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Validates data against a schema, returning true if the data is valid, or
|
|
123
|
+
* false otherwise. If the schema is invalid, then this function will return
|
|
124
|
+
* false.
|
|
125
|
+
*
|
|
126
|
+
* @param schema - The schema against which to validate the form data
|
|
127
|
+
* @param formData - The form data to validate
|
|
128
|
+
* @param rootSchema - The root schema used to provide $ref resolutions
|
|
129
|
+
*/
|
|
130
|
+
isValid(schema: S, formData: T | undefined, rootSchema: S) {
|
|
131
|
+
const rootSchemaId = rootSchema[ID_KEY] ?? ROOT_SCHEMA_PREFIX;
|
|
132
|
+
try {
|
|
133
|
+
// add the rootSchema ROOT_SCHEMA_PREFIX as id.
|
|
134
|
+
// then rewrite the schema ref's to point to the rootSchema
|
|
135
|
+
// this accounts for the case where schema have references to models
|
|
136
|
+
// that lives in the rootSchema but not in the schema in question.
|
|
137
|
+
if (this.ajv.getSchema(rootSchemaId) === undefined) {
|
|
138
|
+
this.ajv.addSchema(rootSchema, rootSchemaId);
|
|
139
|
+
}
|
|
140
|
+
const schemaWithIdRefPrefix = withIdRefPrefix<S>(schema) as S;
|
|
141
|
+
const schemaId = schemaWithIdRefPrefix[ID_KEY] ?? hashForSchema(schemaWithIdRefPrefix);
|
|
142
|
+
let compiledValidator: ValidateFunction | undefined;
|
|
143
|
+
compiledValidator = this.ajv.getSchema(schemaId);
|
|
144
|
+
if (compiledValidator === undefined) {
|
|
145
|
+
// Add schema by an explicit ID so it can be fetched later
|
|
146
|
+
// Fall back to using compile if necessary
|
|
147
|
+
// https://ajv.js.org/guide/managing-schemas.html#pre-adding-all-schemas-vs-adding-on-demand
|
|
148
|
+
compiledValidator =
|
|
149
|
+
this.ajv.addSchema(schemaWithIdRefPrefix, schemaId).getSchema(schemaId) ||
|
|
150
|
+
this.ajv.compile(schemaWithIdRefPrefix);
|
|
151
|
+
}
|
|
152
|
+
const result = compiledValidator(formData);
|
|
153
|
+
return result as boolean;
|
|
154
|
+
} catch (e) {
|
|
155
|
+
console.warn('Error encountered compiling schema:', e);
|
|
156
|
+
return false;
|
|
157
|
+
} finally {
|
|
158
|
+
// TODO: A function should be called if the root schema changes so we don't have to remove and recompile the schema every run.
|
|
159
|
+
// make sure we remove the rootSchema from the global ajv instance
|
|
160
|
+
this.ajv.removeSchema(rootSchemaId);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var fs = require('fs');
|
|
6
|
-
var standaloneCode = require('ajv/dist/standalone');
|
|
7
|
-
var utils = require('@rjsf/utils');
|
|
8
|
-
var createAjvInstance = require('./createAjvInstance-33d73c95.js');
|
|
9
|
-
require('ajv');
|
|
10
|
-
require('ajv-formats');
|
|
11
|
-
require('lodash/isObject');
|
|
12
|
-
|
|
13
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
|
-
|
|
15
|
-
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
16
|
-
var standaloneCode__default = /*#__PURE__*/_interopDefaultLegacy(standaloneCode);
|
|
17
|
-
|
|
18
|
-
/** The function used to compile a schema into an output file in the form that allows it to be used as a precompiled
|
|
19
|
-
* validator. The main reasons for using a precompiled validator is reducing code size, improving validation speed and,
|
|
20
|
-
* most importantly, avoiding dynamic code compilation when prohibited by a browser's Content Security Policy. For more
|
|
21
|
-
* information about AJV code compilation see: https://ajv.js.org/standalone.html
|
|
22
|
-
*
|
|
23
|
-
* @param schema - The schema to be compiled into a set of precompiled validators functions
|
|
24
|
-
* @param output - The name of the file into which the precompiled validator functions will be generated
|
|
25
|
-
* @param [options={}] - The set of `CustomValidatorOptionsType` information used to alter the AJV validator used for
|
|
26
|
-
* compiling the schema. They are the same options that are passed to the `customizeValidator()` function in
|
|
27
|
-
* order to modify the behavior of the regular AJV-based validator.
|
|
28
|
-
*/
|
|
29
|
-
function compileSchemaValidators(schema, output, options = {}) {
|
|
30
|
-
console.log('parsing the schema');
|
|
31
|
-
const schemaMaps = utils.schemaParser(schema);
|
|
32
|
-
const schemas = Object.values(schemaMaps);
|
|
33
|
-
const {
|
|
34
|
-
additionalMetaSchemas,
|
|
35
|
-
customFormats,
|
|
36
|
-
ajvOptionsOverrides = {},
|
|
37
|
-
ajvFormatOptions,
|
|
38
|
-
AjvClass
|
|
39
|
-
} = options;
|
|
40
|
-
// Allow users to turn off the `lines: true` feature in their own overrides, but NOT the `source: true`
|
|
41
|
-
const compileOptions = {
|
|
42
|
-
...ajvOptionsOverrides,
|
|
43
|
-
code: {
|
|
44
|
-
lines: true,
|
|
45
|
-
...ajvOptionsOverrides.code,
|
|
46
|
-
source: true
|
|
47
|
-
},
|
|
48
|
-
schemas
|
|
49
|
-
};
|
|
50
|
-
const ajv = createAjvInstance.createAjvInstance(additionalMetaSchemas, customFormats, compileOptions, ajvFormatOptions, AjvClass);
|
|
51
|
-
const moduleCode = standaloneCode__default["default"](ajv);
|
|
52
|
-
console.log(`writing ${output}`);
|
|
53
|
-
fs__default["default"].writeFileSync(output, moduleCode);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
exports["default"] = compileSchemaValidators;
|
|
57
|
-
//# sourceMappingURL=compileSchemaValidators.cjs.development.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compileSchemaValidators.cjs.development.js","sources":["../src/compileSchemaValidators.ts"],"sourcesContent":["import fs from 'fs';\nimport standaloneCode from 'ajv/dist/standalone';\nimport { RJSFSchema, StrictRJSFSchema, schemaParser } from '@rjsf/utils';\n\nimport createAjvInstance from './createAjvInstance';\nimport { CustomValidatorOptionsType } from './types';\n\n/** The function used to compile a schema into an output file in the form that allows it to be used as a precompiled\n * validator. The main reasons for using a precompiled validator is reducing code size, improving validation speed and,\n * most importantly, avoiding dynamic code compilation when prohibited by a browser's Content Security Policy. For more\n * information about AJV code compilation see: https://ajv.js.org/standalone.html\n *\n * @param schema - The schema to be compiled into a set of precompiled validators functions\n * @param output - The name of the file into which the precompiled validator functions will be generated\n * @param [options={}] - The set of `CustomValidatorOptionsType` information used to alter the AJV validator used for\n * compiling the schema. They are the same options that are passed to the `customizeValidator()` function in\n * order to modify the behavior of the regular AJV-based validator.\n */\nexport default function compileSchemaValidators<S extends StrictRJSFSchema = RJSFSchema>(\n schema: S,\n output: string,\n options: CustomValidatorOptionsType = {}\n) {\n console.log('parsing the schema');\n const schemaMaps = schemaParser(schema);\n const schemas = Object.values(schemaMaps);\n\n const { additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}, ajvFormatOptions, AjvClass } = options;\n // Allow users to turn off the `lines: true` feature in their own overrides, but NOT the `source: true`\n const compileOptions = {\n ...ajvOptionsOverrides,\n code: { lines: true, ...ajvOptionsOverrides.code, source: true },\n schemas,\n };\n const ajv = createAjvInstance(additionalMetaSchemas, customFormats, compileOptions, ajvFormatOptions, AjvClass);\n\n const moduleCode = standaloneCode(ajv);\n console.log(`writing ${output}`);\n fs.writeFileSync(output, moduleCode);\n}\n"],"names":["compileSchemaValidators","schema","output","options","console","log","schemaMaps","schemaParser","schemas","Object","values","additionalMetaSchemas","customFormats","ajvOptionsOverrides","ajvFormatOptions","AjvClass","compileOptions","code","lines","source","ajv","createAjvInstance","moduleCode","standaloneCode","fs","writeFileSync"],"mappings":";;;;;;;;;;;;;;;;;AAOA;;;;;;;;;;AAUG;AACW,SAAUA,uBAAuBA,CAC7CC,MAAS,EACTC,MAAc,EACdC,OAAA,GAAsC,EAAE,EAAA;AAExCC,EAAAA,OAAO,CAACC,GAAG,CAAC,oBAAoB,CAAC,CAAA;AACjC,EAAA,MAAMC,UAAU,GAAGC,kBAAY,CAACN,MAAM,CAAC,CAAA;AACvC,EAAA,MAAMO,OAAO,GAAGC,MAAM,CAACC,MAAM,CAACJ,UAAU,CAAC,CAAA;EAEzC,MAAM;IAAEK,qBAAqB;IAAEC,aAAa;IAAEC,mBAAmB,GAAG,EAAE;IAAEC,gBAAgB;AAAEC,IAAAA,QAAAA;AAAU,GAAA,GAAGZ,OAAO,CAAA;AAC9G;AACA,EAAA,MAAMa,cAAc,GAAG;AACrB,IAAA,GAAGH,mBAAmB;AACtBI,IAAAA,IAAI,EAAE;AAAEC,MAAAA,KAAK,EAAE,IAAI;MAAE,GAAGL,mBAAmB,CAACI,IAAI;AAAEE,MAAAA,MAAM,EAAE,IAAA;KAAM;AAChEX,IAAAA,OAAAA;GACD,CAAA;AACD,EAAA,MAAMY,GAAG,GAAGC,mCAAiB,CAACV,qBAAqB,EAAEC,aAAa,EAAEI,cAAc,EAAEF,gBAAgB,EAAEC,QAAQ,CAAC,CAAA;AAE/G,EAAA,MAAMO,UAAU,GAAGC,kCAAc,CAACH,GAAG,CAAC,CAAA;AACtChB,EAAAA,OAAO,CAACC,GAAG,EAAYH,QAAAA,EAAAA,MAAM,EAAE,CAAC,CAAA;AAChCsB,EAAAA,sBAAE,CAACC,aAAa,CAACvB,MAAM,EAAEoB,UAAU,CAAC,CAAA;AACtC;;;;"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),r=require("ajv/dist/standalone"),t=require("@rjsf/utils"),s=require("./createAjvInstance-33d73c95.js");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("ajv"),require("ajv-formats"),require("lodash/isObject");var o=a(e),i=a(r);exports.default=function(e,r,a={}){console.log("parsing the schema");const c=t.schemaParser(e),n=Object.values(c),{additionalMetaSchemas:u,customFormats:l,ajvOptionsOverrides:d={},ajvFormatOptions:j,AjvClass:v}=a,f={...d,code:{lines:!0,...d.code,source:!0},schemas:n},m=s.createAjvInstance(u,l,f,j,v),p=i.default(m);console.log(`writing ${r}`),o.default.writeFileSync(r,p)};
|
|
2
|
-
//# sourceMappingURL=compileSchemaValidators.cjs.production.min.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compileSchemaValidators.cjs.production.min.js","sources":["../src/compileSchemaValidators.ts"],"sourcesContent":["import fs from 'fs';\nimport standaloneCode from 'ajv/dist/standalone';\nimport { RJSFSchema, StrictRJSFSchema, schemaParser } from '@rjsf/utils';\n\nimport createAjvInstance from './createAjvInstance';\nimport { CustomValidatorOptionsType } from './types';\n\n/** The function used to compile a schema into an output file in the form that allows it to be used as a precompiled\n * validator. The main reasons for using a precompiled validator is reducing code size, improving validation speed and,\n * most importantly, avoiding dynamic code compilation when prohibited by a browser's Content Security Policy. For more\n * information about AJV code compilation see: https://ajv.js.org/standalone.html\n *\n * @param schema - The schema to be compiled into a set of precompiled validators functions\n * @param output - The name of the file into which the precompiled validator functions will be generated\n * @param [options={}] - The set of `CustomValidatorOptionsType` information used to alter the AJV validator used for\n * compiling the schema. They are the same options that are passed to the `customizeValidator()` function in\n * order to modify the behavior of the regular AJV-based validator.\n */\nexport default function compileSchemaValidators<S extends StrictRJSFSchema = RJSFSchema>(\n schema: S,\n output: string,\n options: CustomValidatorOptionsType = {}\n) {\n console.log('parsing the schema');\n const schemaMaps = schemaParser(schema);\n const schemas = Object.values(schemaMaps);\n\n const { additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}, ajvFormatOptions, AjvClass } = options;\n // Allow users to turn off the `lines: true` feature in their own overrides, but NOT the `source: true`\n const compileOptions = {\n ...ajvOptionsOverrides,\n code: { lines: true, ...ajvOptionsOverrides.code, source: true },\n schemas,\n };\n const ajv = createAjvInstance(additionalMetaSchemas, customFormats, compileOptions, ajvFormatOptions, AjvClass);\n\n const moduleCode = standaloneCode(ajv);\n console.log(`writing ${output}`);\n fs.writeFileSync(output, moduleCode);\n}\n"],"names":["schema","output","options","console","log","schemaMaps","schemaParser","schemas","Object","values","additionalMetaSchemas","customFormats","ajvOptionsOverrides","ajvFormatOptions","AjvClass","compileOptions","code","lines","source","ajv","createAjvInstance","moduleCode","standaloneCode","fs","writeFileSync"],"mappings":"0WAkBc,SACZA,EACAC,EACAC,EAAsC,CAAA,GAEtCC,QAAQC,IAAI,sBACZ,MAAMC,EAAaC,eAAaN,GAC1BO,EAAUC,OAAOC,OAAOJ,IAExBK,sBAAEA,EAAqBC,cAAEA,EAAaC,oBAAEA,EAAsB,CAAE,EAAAC,iBAAEA,EAAgBC,SAAEA,GAAaZ,EAEjGa,EAAiB,IAClBH,EACHI,KAAM,CAAEC,OAAO,KAASL,EAAoBI,KAAME,QAAQ,GAC1DX,WAEIY,EAAMC,EAAAA,kBAAkBV,EAAuBC,EAAeI,EAAgBF,EAAkBC,GAEhGO,EAAaC,UAAeH,GAClChB,QAAQC,eAAeH,KACvBsB,EAAAA,QAAGC,cAAcvB,EAAQoB,EAC3B"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";var e=require("ajv"),a=require("ajv-formats"),r=require("lodash/isObject"),t=require("@rjsf/utils");function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var d=s(e),o=s(a),l=s(r);const u={allErrors:!0,multipleOfPrecision:8,strict:!1,verbose:!0},i=/^(#?([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*\)))$/,n=/^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/;exports.createAjvInstance=function(e,a,r={},s,c=d.default){const b=new c({...u,...r});return s?o.default(b,s):!1!==s&&o.default(b),b.addFormat("data-url",n),b.addFormat("color",i),b.addKeyword(t.ADDITIONAL_PROPERTY_FLAG),b.addKeyword(t.RJSF_ADDITONAL_PROPERTIES_FLAG),Array.isArray(e)&&b.addMetaSchema(e),l.default(a)&&Object.keys(a).forEach((e=>{b.addFormat(e,a[e])})),b};
|
|
2
|
-
//# sourceMappingURL=createAjvInstance-33d73c95.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createAjvInstance-33d73c95.js","sources":["../src/createAjvInstance.ts"],"sourcesContent":["import Ajv, { Options } from 'ajv';\nimport addFormats, { FormatsPluginOptions } from 'ajv-formats';\nimport isObject from 'lodash/isObject';\n\nimport { CustomValidatorOptionsType } from './types';\nimport { ADDITIONAL_PROPERTY_FLAG, RJSF_ADDITONAL_PROPERTIES_FLAG } from '@rjsf/utils';\n\nexport const AJV_CONFIG: Options = {\n allErrors: true,\n multipleOfPrecision: 8,\n strict: false,\n verbose: true,\n} as const;\nexport const COLOR_FORMAT_REGEX =\n /^(#?([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*\\)))$/;\nexport const DATA_URL_FORMAT_REGEX = /^data:([a-z]+\\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/;\n\n/** Creates an Ajv version 8 implementation object with standard support for the 'color` and `data-url` custom formats.\n * If `additionalMetaSchemas` are provided then the Ajv instance is modified to add each of the meta schemas in the\n * list. If `customFormats` are provided then those additional formats are added to the list of supported formats. If\n * `ajvOptionsOverrides` are provided then they are spread on top of the default `AJV_CONFIG` options when constructing\n * the `Ajv` instance. With Ajv v8, the JSON Schema formats are not provided by default, but can be plugged in. By\n * default, all formats from the `ajv-formats` library are added. To disable this capability, set the `ajvFormatOptions`\n * parameter to `false`. Additionally, you can configure the `ajv-formats` by providing a custom set of\n * [format options](https://github.com/ajv-validator/ajv-formats) to the `ajvFormatOptions` parameter.\n *\n * @param [additionalMetaSchemas] - The list of additional meta schemas that the validator can access\n * @param [customFormats] - The set of additional custom formats that the validator will support\n * @param [ajvOptionsOverrides={}] - The set of validator config override options\n * @param [ajvFormatOptions] - The `ajv-format` options to use when adding formats to `ajv`; pass `false` to disable it\n * @param [AjvClass] - The `Ajv` class to use when creating the validator instance\n */\nexport default function createAjvInstance(\n additionalMetaSchemas?: CustomValidatorOptionsType['additionalMetaSchemas'],\n customFormats?: CustomValidatorOptionsType['customFormats'],\n ajvOptionsOverrides: CustomValidatorOptionsType['ajvOptionsOverrides'] = {},\n ajvFormatOptions?: FormatsPluginOptions | false,\n AjvClass: typeof Ajv = Ajv\n) {\n const ajv = new AjvClass({ ...AJV_CONFIG, ...ajvOptionsOverrides });\n if (ajvFormatOptions) {\n addFormats(ajv, ajvFormatOptions);\n } else if (ajvFormatOptions !== false) {\n addFormats(ajv);\n }\n\n // add custom formats\n ajv.addFormat('data-url', DATA_URL_FORMAT_REGEX);\n ajv.addFormat('color', COLOR_FORMAT_REGEX);\n\n // Add RJSF-specific additional properties keywords so Ajv doesn't report errors if strict is enabled.\n ajv.addKeyword(ADDITIONAL_PROPERTY_FLAG);\n ajv.addKeyword(RJSF_ADDITONAL_PROPERTIES_FLAG);\n\n // add more schemas to validate against\n if (Array.isArray(additionalMetaSchemas)) {\n ajv.addMetaSchema(additionalMetaSchemas);\n }\n\n // add more custom formats to validate against\n if (isObject(customFormats)) {\n Object.keys(customFormats).forEach((formatName) => {\n ajv.addFormat(formatName, customFormats[formatName]);\n });\n }\n\n return ajv;\n}\n"],"names":["AJV_CONFIG","allErrors","multipleOfPrecision","strict","verbose","COLOR_FORMAT_REGEX","DATA_URL_FORMAT_REGEX","additionalMetaSchemas","customFormats","ajvOptionsOverrides","ajvFormatOptions","AjvClass","Ajv","ajv","addFormats","addFormat","addKeyword","ADDITIONAL_PROPERTY_FLAG","RJSF_ADDITONAL_PROPERTIES_FLAG","Array","isArray","addMetaSchema","isObject","Object","keys","forEach","formatName"],"mappings":"kNAOO,MAAMA,EAAsB,CACjCC,WAAW,EACXC,oBAAqB,EACrBC,QAAQ,EACRC,SAAS,GAEEC,EACX,6YACWC,EAAwB,sFAiBb,SACtBC,EACAC,EACAC,EAAyE,CAAA,EACzEC,EACAC,EAAuBC,WAEvB,MAAMC,EAAM,IAAIF,EAAS,IAAKX,KAAeS,IA2B7C,OA1BIC,EACFI,UAAWD,EAAKH,IACc,IAArBA,GACTI,EAAU,QAACD,GAIbA,EAAIE,UAAU,WAAYT,GAC1BO,EAAIE,UAAU,QAASV,GAGvBQ,EAAIG,WAAWC,EAAAA,0BACfJ,EAAIG,WAAWE,EAAAA,gCAGXC,MAAMC,QAAQb,IAChBM,EAAIQ,cAAcd,GAIhBe,EAAAA,QAASd,IACXe,OAAOC,KAAKhB,GAAeiB,SAASC,IAClCb,EAAIE,UAAUW,EAAYlB,EAAckB,GAAY,IAIjDb,CACT"}
|