@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.
Files changed (64) hide show
  1. package/dist/compileSchemaValidators.esm.js +68 -0
  2. package/dist/compileSchemaValidators.esm.js.map +7 -0
  3. package/dist/compileSchemaValidators.js +96 -5
  4. package/dist/compileSchemaValidators.js.map +7 -0
  5. package/dist/index.js +362 -5
  6. package/dist/index.js.map +7 -0
  7. package/dist/validator-ajv8.esm.js +166 -266
  8. package/dist/validator-ajv8.esm.js.map +7 -1
  9. package/dist/{validator-ajv8.umd.development.js → validator-ajv8.umd.js} +136 -277
  10. package/lib/compileSchemaValidators.d.ts +16 -0
  11. package/lib/compileSchemaValidators.js +21 -0
  12. package/lib/compileSchemaValidators.js.map +1 -0
  13. package/lib/compileSchemaValidatorsCode.d.ts +13 -0
  14. package/lib/compileSchemaValidatorsCode.js +23 -0
  15. package/lib/compileSchemaValidatorsCode.js.map +1 -0
  16. package/lib/createAjvInstance.d.ts +22 -0
  17. package/lib/createAjvInstance.js +54 -0
  18. package/lib/createAjvInstance.js.map +1 -0
  19. package/lib/createPrecompiledValidator.d.ts +14 -0
  20. package/lib/createPrecompiledValidator.js +16 -0
  21. package/lib/createPrecompiledValidator.js.map +1 -0
  22. package/lib/customizeValidator.d.ts +11 -0
  23. package/lib/customizeValidator.js +13 -0
  24. package/lib/customizeValidator.js.map +1 -0
  25. package/lib/index.d.ts +6 -0
  26. package/lib/index.js +6 -0
  27. package/lib/index.js.map +1 -0
  28. package/lib/precompiledValidator.d.ts +87 -0
  29. package/lib/precompiledValidator.js +103 -0
  30. package/lib/precompiledValidator.js.map +1 -0
  31. package/lib/processRawValidationErrors.d.ts +30 -0
  32. package/lib/processRawValidationErrors.js +91 -0
  33. package/lib/processRawValidationErrors.js.map +1 -0
  34. package/lib/types.d.ts +38 -0
  35. package/lib/types.js +2 -0
  36. package/lib/types.js.map +1 -0
  37. package/lib/validator.d.ts +59 -0
  38. package/lib/validator.js +122 -0
  39. package/lib/validator.js.map +1 -0
  40. package/package.json +21 -16
  41. package/src/compileSchemaValidators.ts +29 -0
  42. package/src/compileSchemaValidatorsCode.ts +34 -0
  43. package/src/createAjvInstance.ts +68 -0
  44. package/src/createPrecompiledValidator.ts +23 -0
  45. package/src/customizeValidator.ts +20 -0
  46. package/src/index.ts +7 -0
  47. package/src/precompiledValidator.ts +174 -0
  48. package/src/processRawValidationErrors.ts +139 -0
  49. package/src/types.ts +40 -0
  50. package/src/validator.ts +163 -0
  51. package/dist/compileSchemaValidators.cjs.development.js +0 -57
  52. package/dist/compileSchemaValidators.cjs.development.js.map +0 -1
  53. package/dist/compileSchemaValidators.cjs.production.min.js +0 -2
  54. package/dist/compileSchemaValidators.cjs.production.min.js.map +0 -1
  55. package/dist/createAjvInstance-33d73c95.js +0 -2
  56. package/dist/createAjvInstance-33d73c95.js.map +0 -1
  57. package/dist/index.cjs.development.js +0 -412
  58. package/dist/index.cjs.development.js.map +0 -1
  59. package/dist/index.cjs.production.min.js +0 -2
  60. package/dist/index.cjs.production.min.js.map +0 -1
  61. package/dist/index.d.ts +0 -68
  62. package/dist/validator-ajv8.umd.development.js.map +0 -1
  63. package/dist/validator-ajv8.umd.production.min.js +0 -2
  64. package/dist/validator-ajv8.umd.production.min.js.map +0 -1
@@ -0,0 +1,68 @@
1
+ // src/compileSchemaValidators.ts
2
+ import fs from "fs";
3
+
4
+ // src/compileSchemaValidatorsCode.ts
5
+ import standaloneCode from "ajv/dist/standalone";
6
+ import { schemaParser } from "@rjsf/utils";
7
+
8
+ // src/createAjvInstance.ts
9
+ import Ajv from "ajv";
10
+ import addFormats from "ajv-formats";
11
+ import isObject from "lodash/isObject";
12
+ import { ADDITIONAL_PROPERTY_FLAG, RJSF_ADDITONAL_PROPERTIES_FLAG } from "@rjsf/utils";
13
+ var AJV_CONFIG = {
14
+ allErrors: true,
15
+ multipleOfPrecision: 8,
16
+ strict: false,
17
+ verbose: true
18
+ };
19
+ 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*\)))$/;
20
+ var DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/;
21
+ function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}, ajvFormatOptions, AjvClass = Ajv) {
22
+ const ajv = new AjvClass({ ...AJV_CONFIG, ...ajvOptionsOverrides });
23
+ if (ajvFormatOptions) {
24
+ addFormats(ajv, ajvFormatOptions);
25
+ } else if (ajvFormatOptions !== false) {
26
+ addFormats(ajv);
27
+ }
28
+ ajv.addFormat("data-url", DATA_URL_FORMAT_REGEX);
29
+ ajv.addFormat("color", COLOR_FORMAT_REGEX);
30
+ ajv.addKeyword(ADDITIONAL_PROPERTY_FLAG);
31
+ ajv.addKeyword(RJSF_ADDITONAL_PROPERTIES_FLAG);
32
+ if (Array.isArray(additionalMetaSchemas)) {
33
+ ajv.addMetaSchema(additionalMetaSchemas);
34
+ }
35
+ if (isObject(customFormats)) {
36
+ Object.keys(customFormats).forEach((formatName) => {
37
+ ajv.addFormat(formatName, customFormats[formatName]);
38
+ });
39
+ }
40
+ return ajv;
41
+ }
42
+
43
+ // src/compileSchemaValidatorsCode.ts
44
+ function compileSchemaValidatorsCode(schema, options = {}) {
45
+ const schemaMaps = schemaParser(schema);
46
+ const schemas = Object.values(schemaMaps);
47
+ const { additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}, ajvFormatOptions, AjvClass } = options;
48
+ const compileOptions = {
49
+ ...ajvOptionsOverrides,
50
+ code: { lines: true, ...ajvOptionsOverrides.code, source: true },
51
+ schemas
52
+ };
53
+ const ajv = createAjvInstance(additionalMetaSchemas, customFormats, compileOptions, ajvFormatOptions, AjvClass);
54
+ return standaloneCode(ajv);
55
+ }
56
+
57
+ // src/compileSchemaValidators.ts
58
+ function compileSchemaValidators(schema, output, options = {}) {
59
+ console.log("parsing the schema");
60
+ const moduleCode = compileSchemaValidatorsCode(schema, options);
61
+ console.log(`writing ${output}`);
62
+ fs.writeFileSync(output, moduleCode);
63
+ }
64
+ export {
65
+ compileSchemaValidatorsCode,
66
+ compileSchemaValidators as default
67
+ };
68
+ //# sourceMappingURL=compileSchemaValidators.esm.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/compileSchemaValidators.ts", "../src/compileSchemaValidatorsCode.ts", "../src/createAjvInstance.ts"],
4
+ "sourcesContent": ["import fs from 'fs';\nimport { RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';\nimport { CustomValidatorOptionsType } from './types';\nimport { compileSchemaValidatorsCode } from './compileSchemaValidatorsCode';\n\nexport { compileSchemaValidatorsCode };\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\n const moduleCode = compileSchemaValidatorsCode(schema, options);\n console.log(`writing ${output}`);\n fs.writeFileSync(output, moduleCode);\n}\n", "import 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 javascript code 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 [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 function compileSchemaValidatorsCode<S extends StrictRJSFSchema = RJSFSchema>(\n schema: S,\n options: CustomValidatorOptionsType = {}\n) {\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 return standaloneCode(ajv);\n}\n", "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"],
5
+ "mappings": ";AAAA,OAAO,QAAQ;;;ACAf,OAAO,oBAAoB;AAC3B,SAAuC,oBAAoB;;;ACD3D,OAAO,SAAsB;AAC7B,OAAO,gBAA0C;AACjD,OAAO,cAAc;AAGrB,SAAS,0BAA0B,sCAAsC;AAElE,IAAM,aAAsB;AAAA,EACjC,WAAW;AAAA,EACX,qBAAqB;AAAA,EACrB,QAAQ;AAAA,EACR,SAAS;AACX;AACO,IAAM,qBACX;AACK,IAAM,wBAAwB;AAiBtB,SAAR,kBACL,uBACA,eACA,sBAAyE,CAAC,GAC1E,kBACA,WAAuB,KACvB;AACA,QAAM,MAAM,IAAI,SAAS,EAAE,GAAG,YAAY,GAAG,oBAAoB,CAAC;AAClE,MAAI,kBAAkB;AACpB,eAAW,KAAK,gBAAgB;AAAA,EAClC,WAAW,qBAAqB,OAAO;AACrC,eAAW,GAAG;AAAA,EAChB;AAGA,MAAI,UAAU,YAAY,qBAAqB;AAC/C,MAAI,UAAU,SAAS,kBAAkB;AAGzC,MAAI,WAAW,wBAAwB;AACvC,MAAI,WAAW,8BAA8B;AAG7C,MAAI,MAAM,QAAQ,qBAAqB,GAAG;AACxC,QAAI,cAAc,qBAAqB;AAAA,EACzC;AAGA,MAAI,SAAS,aAAa,GAAG;AAC3B,WAAO,KAAK,aAAa,EAAE,QAAQ,CAAC,eAAe;AACjD,UAAI,UAAU,YAAY,cAAc,UAAU,CAAC;AAAA,IACrD,CAAC;AAAA,EACH;AAEA,SAAO;AACT;;;ADnDO,SAAS,4BACd,QACA,UAAsC,CAAC,GACvC;AACA,QAAM,aAAa,aAAa,MAAM;AACtC,QAAM,UAAU,OAAO,OAAO,UAAU;AAExC,QAAM,EAAE,uBAAuB,eAAe,sBAAsB,CAAC,GAAG,kBAAkB,SAAS,IAAI;AAEvG,QAAM,iBAAiB;AAAA,IACrB,GAAG;AAAA,IACH,MAAM,EAAE,OAAO,MAAM,GAAG,oBAAoB,MAAM,QAAQ,KAAK;AAAA,IAC/D;AAAA,EACF;AACA,QAAM,MAAM,kBAAkB,uBAAuB,eAAe,gBAAgB,kBAAkB,QAAQ;AAE9G,SAAO,eAAe,GAAG;AAC3B;;;ADfe,SAAR,wBACL,QACA,QACA,UAAsC,CAAC,GACvC;AACA,UAAQ,IAAI,oBAAoB;AAEhC,QAAM,aAAa,4BAA4B,QAAQ,OAAO;AAC9D,UAAQ,IAAI,WAAW,MAAM,EAAE;AAC/B,KAAG,cAAc,QAAQ,UAAU;AACrC;",
6
+ "names": []
7
+ }
@@ -1,8 +1,99 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
29
 
2
- 'use strict'
30
+ // src/compileSchemaValidators.ts
31
+ var compileSchemaValidators_exports = {};
32
+ __export(compileSchemaValidators_exports, {
33
+ compileSchemaValidatorsCode: () => compileSchemaValidatorsCode,
34
+ default: () => compileSchemaValidators
35
+ });
36
+ module.exports = __toCommonJS(compileSchemaValidators_exports);
37
+ var import_fs = __toESM(require("fs"));
3
38
 
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./compileSchemaValidators.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./compileSchemaValidators.cjs.development.js')
39
+ // src/compileSchemaValidatorsCode.ts
40
+ var import_standalone = __toESM(require("ajv/dist/standalone"));
41
+ var import_utils2 = require("@rjsf/utils");
42
+
43
+ // src/createAjvInstance.ts
44
+ var import_ajv = __toESM(require("ajv"));
45
+ var import_ajv_formats = __toESM(require("ajv-formats"));
46
+ var import_isObject = __toESM(require("lodash/isObject"));
47
+ var import_utils = require("@rjsf/utils");
48
+ var AJV_CONFIG = {
49
+ allErrors: true,
50
+ multipleOfPrecision: 8,
51
+ strict: false,
52
+ verbose: true
53
+ };
54
+ 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*\)))$/;
55
+ var DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/;
56
+ function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}, ajvFormatOptions, AjvClass = import_ajv.default) {
57
+ const ajv = new AjvClass({ ...AJV_CONFIG, ...ajvOptionsOverrides });
58
+ if (ajvFormatOptions) {
59
+ (0, import_ajv_formats.default)(ajv, ajvFormatOptions);
60
+ } else if (ajvFormatOptions !== false) {
61
+ (0, import_ajv_formats.default)(ajv);
62
+ }
63
+ ajv.addFormat("data-url", DATA_URL_FORMAT_REGEX);
64
+ ajv.addFormat("color", COLOR_FORMAT_REGEX);
65
+ ajv.addKeyword(import_utils.ADDITIONAL_PROPERTY_FLAG);
66
+ ajv.addKeyword(import_utils.RJSF_ADDITONAL_PROPERTIES_FLAG);
67
+ if (Array.isArray(additionalMetaSchemas)) {
68
+ ajv.addMetaSchema(additionalMetaSchemas);
69
+ }
70
+ if ((0, import_isObject.default)(customFormats)) {
71
+ Object.keys(customFormats).forEach((formatName) => {
72
+ ajv.addFormat(formatName, customFormats[formatName]);
73
+ });
74
+ }
75
+ return ajv;
76
+ }
77
+
78
+ // src/compileSchemaValidatorsCode.ts
79
+ function compileSchemaValidatorsCode(schema, options = {}) {
80
+ const schemaMaps = (0, import_utils2.schemaParser)(schema);
81
+ const schemas = Object.values(schemaMaps);
82
+ const { additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}, ajvFormatOptions, AjvClass } = options;
83
+ const compileOptions = {
84
+ ...ajvOptionsOverrides,
85
+ code: { lines: true, ...ajvOptionsOverrides.code, source: true },
86
+ schemas
87
+ };
88
+ const ajv = createAjvInstance(additionalMetaSchemas, customFormats, compileOptions, ajvFormatOptions, AjvClass);
89
+ return (0, import_standalone.default)(ajv);
90
+ }
91
+
92
+ // src/compileSchemaValidators.ts
93
+ function compileSchemaValidators(schema, output, options = {}) {
94
+ console.log("parsing the schema");
95
+ const moduleCode = compileSchemaValidatorsCode(schema, options);
96
+ console.log(`writing ${output}`);
97
+ import_fs.default.writeFileSync(output, moduleCode);
8
98
  }
99
+ //# sourceMappingURL=compileSchemaValidators.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/compileSchemaValidators.ts", "../src/compileSchemaValidatorsCode.ts", "../src/createAjvInstance.ts"],
4
+ "sourcesContent": ["import fs from 'fs';\nimport { RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';\nimport { CustomValidatorOptionsType } from './types';\nimport { compileSchemaValidatorsCode } from './compileSchemaValidatorsCode';\n\nexport { compileSchemaValidatorsCode };\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\n const moduleCode = compileSchemaValidatorsCode(schema, options);\n console.log(`writing ${output}`);\n fs.writeFileSync(output, moduleCode);\n}\n", "import 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 javascript code 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 [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 function compileSchemaValidatorsCode<S extends StrictRJSFSchema = RJSFSchema>(\n schema: S,\n options: CustomValidatorOptionsType = {}\n) {\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 return standaloneCode(ajv);\n}\n", "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"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAAe;;;ACAf,wBAA2B;AAC3B,IAAAA,gBAA2D;;;ACD3D,iBAA6B;AAC7B,yBAAiD;AACjD,sBAAqB;AAGrB,mBAAyE;AAElE,IAAM,aAAsB;AAAA,EACjC,WAAW;AAAA,EACX,qBAAqB;AAAA,EACrB,QAAQ;AAAA,EACR,SAAS;AACX;AACO,IAAM,qBACX;AACK,IAAM,wBAAwB;AAiBtB,SAAR,kBACL,uBACA,eACA,sBAAyE,CAAC,GAC1E,kBACA,WAAuB,WAAAC,SACvB;AACA,QAAM,MAAM,IAAI,SAAS,EAAE,GAAG,YAAY,GAAG,oBAAoB,CAAC;AAClE,MAAI,kBAAkB;AACpB,2BAAAC,SAAW,KAAK,gBAAgB;AAAA,EAClC,WAAW,qBAAqB,OAAO;AACrC,2BAAAA,SAAW,GAAG;AAAA,EAChB;AAGA,MAAI,UAAU,YAAY,qBAAqB;AAC/C,MAAI,UAAU,SAAS,kBAAkB;AAGzC,MAAI,WAAW,qCAAwB;AACvC,MAAI,WAAW,2CAA8B;AAG7C,MAAI,MAAM,QAAQ,qBAAqB,GAAG;AACxC,QAAI,cAAc,qBAAqB;AAAA,EACzC;AAGA,UAAI,gBAAAC,SAAS,aAAa,GAAG;AAC3B,WAAO,KAAK,aAAa,EAAE,QAAQ,CAAC,eAAe;AACjD,UAAI,UAAU,YAAY,cAAc,UAAU,CAAC;AAAA,IACrD,CAAC;AAAA,EACH;AAEA,SAAO;AACT;;;ADnDO,SAAS,4BACd,QACA,UAAsC,CAAC,GACvC;AACA,QAAM,iBAAa,4BAAa,MAAM;AACtC,QAAM,UAAU,OAAO,OAAO,UAAU;AAExC,QAAM,EAAE,uBAAuB,eAAe,sBAAsB,CAAC,GAAG,kBAAkB,SAAS,IAAI;AAEvG,QAAM,iBAAiB;AAAA,IACrB,GAAG;AAAA,IACH,MAAM,EAAE,OAAO,MAAM,GAAG,oBAAoB,MAAM,QAAQ,KAAK;AAAA,IAC/D;AAAA,EACF;AACA,QAAM,MAAM,kBAAkB,uBAAuB,eAAe,gBAAgB,kBAAkB,QAAQ;AAE9G,aAAO,kBAAAC,SAAe,GAAG;AAC3B;;;ADfe,SAAR,wBACL,QACA,QACA,UAAsC,CAAC,GACvC;AACA,UAAQ,IAAI,oBAAoB;AAEhC,QAAM,aAAa,4BAA4B,QAAQ,OAAO;AAC9D,UAAQ,IAAI,WAAW,MAAM,EAAE;AAC/B,YAAAC,QAAG,cAAc,QAAQ,UAAU;AACrC;",
6
+ "names": ["import_utils", "Ajv", "addFormats", "isObject", "standaloneCode", "fs"]
7
+ }
package/dist/index.js CHANGED
@@ -1,8 +1,365 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
29
 
2
- 'use strict'
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ createPrecompiledValidator: () => createPrecompiledValidator,
34
+ customizeValidator: () => customizeValidator,
35
+ default: () => src_default
36
+ });
37
+ module.exports = __toCommonJS(src_exports);
3
38
 
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./index.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./index.cjs.development.js')
39
+ // src/validator.ts
40
+ var import_utils3 = require("@rjsf/utils");
41
+
42
+ // src/createAjvInstance.ts
43
+ var import_ajv = __toESM(require("ajv"));
44
+ var import_ajv_formats = __toESM(require("ajv-formats"));
45
+ var import_isObject = __toESM(require("lodash/isObject"));
46
+ var import_utils = require("@rjsf/utils");
47
+ var AJV_CONFIG = {
48
+ allErrors: true,
49
+ multipleOfPrecision: 8,
50
+ strict: false,
51
+ verbose: true
52
+ };
53
+ 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*\)))$/;
54
+ var DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/;
55
+ function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}, ajvFormatOptions, AjvClass = import_ajv.default) {
56
+ const ajv = new AjvClass({ ...AJV_CONFIG, ...ajvOptionsOverrides });
57
+ if (ajvFormatOptions) {
58
+ (0, import_ajv_formats.default)(ajv, ajvFormatOptions);
59
+ } else if (ajvFormatOptions !== false) {
60
+ (0, import_ajv_formats.default)(ajv);
61
+ }
62
+ ajv.addFormat("data-url", DATA_URL_FORMAT_REGEX);
63
+ ajv.addFormat("color", COLOR_FORMAT_REGEX);
64
+ ajv.addKeyword(import_utils.ADDITIONAL_PROPERTY_FLAG);
65
+ ajv.addKeyword(import_utils.RJSF_ADDITONAL_PROPERTIES_FLAG);
66
+ if (Array.isArray(additionalMetaSchemas)) {
67
+ ajv.addMetaSchema(additionalMetaSchemas);
68
+ }
69
+ if ((0, import_isObject.default)(customFormats)) {
70
+ Object.keys(customFormats).forEach((formatName) => {
71
+ ajv.addFormat(formatName, customFormats[formatName]);
72
+ });
73
+ }
74
+ return ajv;
75
+ }
76
+
77
+ // src/processRawValidationErrors.ts
78
+ var import_get = __toESM(require("lodash/get"));
79
+ var import_utils2 = require("@rjsf/utils");
80
+ function transformRJSFValidationErrors(errors = [], uiSchema) {
81
+ return errors.map((e) => {
82
+ const { instancePath, keyword, params, schemaPath, parentSchema, ...rest } = e;
83
+ let { message = "" } = rest;
84
+ let property = instancePath.replace(/\//g, ".");
85
+ let stack = `${property} ${message}`.trim();
86
+ if ("missingProperty" in params) {
87
+ property = property ? `${property}.${params.missingProperty}` : params.missingProperty;
88
+ const currentProperty = params.missingProperty;
89
+ const uiSchemaTitle = (0, import_utils2.getUiOptions)((0, import_get.default)(uiSchema, `${property.replace(/^\./, "")}`)).title;
90
+ if (uiSchemaTitle) {
91
+ message = message.replace(currentProperty, uiSchemaTitle);
92
+ } else {
93
+ const parentSchemaTitle = (0, import_get.default)(parentSchema, [import_utils2.PROPERTIES_KEY, currentProperty, "title"]);
94
+ if (parentSchemaTitle) {
95
+ message = message.replace(currentProperty, parentSchemaTitle);
96
+ }
97
+ }
98
+ stack = message;
99
+ } else {
100
+ const uiSchemaTitle = (0, import_utils2.getUiOptions)((0, import_get.default)(uiSchema, `${property.replace(/^\./, "")}`)).title;
101
+ if (uiSchemaTitle) {
102
+ stack = `'${uiSchemaTitle}' ${message}`.trim();
103
+ } else {
104
+ const parentSchemaTitle = parentSchema?.title;
105
+ if (parentSchemaTitle) {
106
+ stack = `'${parentSchemaTitle}' ${message}`.trim();
107
+ }
108
+ }
109
+ }
110
+ return {
111
+ name: keyword,
112
+ property,
113
+ message,
114
+ params,
115
+ // specific to ajv
116
+ stack,
117
+ schemaPath
118
+ };
119
+ });
8
120
  }
121
+ function processRawValidationErrors(validator, rawErrors, formData, schema, customValidate, transformErrors, uiSchema) {
122
+ const { validationError: invalidSchemaError } = rawErrors;
123
+ let errors = transformRJSFValidationErrors(rawErrors.errors, uiSchema);
124
+ if (invalidSchemaError) {
125
+ errors = [...errors, { stack: invalidSchemaError.message }];
126
+ }
127
+ if (typeof transformErrors === "function") {
128
+ errors = transformErrors(errors, uiSchema);
129
+ }
130
+ let errorSchema = (0, import_utils2.toErrorSchema)(errors);
131
+ if (invalidSchemaError) {
132
+ errorSchema = {
133
+ ...errorSchema,
134
+ $schema: {
135
+ __errors: [invalidSchemaError.message]
136
+ }
137
+ };
138
+ }
139
+ if (typeof customValidate !== "function") {
140
+ return { errors, errorSchema };
141
+ }
142
+ const newFormData = (0, import_utils2.getDefaultFormState)(validator, schema, formData, schema, true);
143
+ const errorHandler = customValidate(newFormData, (0, import_utils2.createErrorHandler)(newFormData), uiSchema);
144
+ const userErrorSchema = (0, import_utils2.unwrapErrorHandler)(errorHandler);
145
+ return (0, import_utils2.validationDataMerge)({ errors, errorSchema }, userErrorSchema);
146
+ }
147
+
148
+ // src/validator.ts
149
+ var AJV8Validator = class {
150
+ /** Constructs an `AJV8Validator` instance using the `options`
151
+ *
152
+ * @param options - The `CustomValidatorOptionsType` options that are used to create the AJV instance
153
+ * @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
154
+ */
155
+ constructor(options, localizer) {
156
+ const { additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass } = options;
157
+ this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass);
158
+ this.localizer = localizer;
159
+ }
160
+ /** Converts an `errorSchema` into a list of `RJSFValidationErrors`
161
+ *
162
+ * @param errorSchema - The `ErrorSchema` instance to convert
163
+ * @param [fieldPath=[]] - The current field path, defaults to [] if not specified
164
+ * @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in
165
+ * the next major release.
166
+ */
167
+ toErrorList(errorSchema, fieldPath = []) {
168
+ return (0, import_utils3.toErrorList)(errorSchema, fieldPath);
169
+ }
170
+ /** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
171
+ * by the playground. Returns the `errors` from the validation
172
+ *
173
+ * @param schema - The schema against which to validate the form data * @param schema
174
+ * @param formData - The form data to validate
175
+ */
176
+ rawValidation(schema, formData) {
177
+ let compilationError = void 0;
178
+ let compiledValidator;
179
+ if (schema[import_utils3.ID_KEY]) {
180
+ compiledValidator = this.ajv.getSchema(schema[import_utils3.ID_KEY]);
181
+ }
182
+ try {
183
+ if (compiledValidator === void 0) {
184
+ compiledValidator = this.ajv.compile(schema);
185
+ }
186
+ compiledValidator(formData);
187
+ } catch (err) {
188
+ compilationError = err;
189
+ }
190
+ let errors;
191
+ if (compiledValidator) {
192
+ if (typeof this.localizer === "function") {
193
+ this.localizer(compiledValidator.errors);
194
+ }
195
+ errors = compiledValidator.errors || void 0;
196
+ compiledValidator.errors = null;
197
+ }
198
+ return {
199
+ errors,
200
+ validationError: compilationError
201
+ };
202
+ }
203
+ /** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
204
+ * the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
205
+ * supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
206
+ * transform them in what ever way it chooses.
207
+ *
208
+ * @param formData - The form data to validate
209
+ * @param schema - The schema against which to validate the form data
210
+ * @param [customValidate] - An optional function that is used to perform custom validation
211
+ * @param [transformErrors] - An optional function that is used to transform errors after AJV validation
212
+ * @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
213
+ */
214
+ validateFormData(formData, schema, customValidate, transformErrors, uiSchema) {
215
+ const rawErrors = this.rawValidation(schema, formData);
216
+ return processRawValidationErrors(this, rawErrors, formData, schema, customValidate, transformErrors, uiSchema);
217
+ }
218
+ /** Validates data against a schema, returning true if the data is valid, or
219
+ * false otherwise. If the schema is invalid, then this function will return
220
+ * false.
221
+ *
222
+ * @param schema - The schema against which to validate the form data
223
+ * @param formData - The form data to validate
224
+ * @param rootSchema - The root schema used to provide $ref resolutions
225
+ */
226
+ isValid(schema, formData, rootSchema) {
227
+ const rootSchemaId = rootSchema[import_utils3.ID_KEY] ?? import_utils3.ROOT_SCHEMA_PREFIX;
228
+ try {
229
+ if (this.ajv.getSchema(rootSchemaId) === void 0) {
230
+ this.ajv.addSchema(rootSchema, rootSchemaId);
231
+ }
232
+ const schemaWithIdRefPrefix = (0, import_utils3.withIdRefPrefix)(schema);
233
+ const schemaId = schemaWithIdRefPrefix[import_utils3.ID_KEY] ?? (0, import_utils3.hashForSchema)(schemaWithIdRefPrefix);
234
+ let compiledValidator;
235
+ compiledValidator = this.ajv.getSchema(schemaId);
236
+ if (compiledValidator === void 0) {
237
+ compiledValidator = this.ajv.addSchema(schemaWithIdRefPrefix, schemaId).getSchema(schemaId) || this.ajv.compile(schemaWithIdRefPrefix);
238
+ }
239
+ const result = compiledValidator(formData);
240
+ return result;
241
+ } catch (e) {
242
+ console.warn("Error encountered compiling schema:", e);
243
+ return false;
244
+ } finally {
245
+ this.ajv.removeSchema(rootSchemaId);
246
+ }
247
+ }
248
+ };
249
+
250
+ // src/customizeValidator.ts
251
+ function customizeValidator(options = {}, localizer) {
252
+ return new AJV8Validator(options, localizer);
253
+ }
254
+
255
+ // src/precompiledValidator.ts
256
+ var import_get2 = __toESM(require("lodash/get"));
257
+ var import_isEqual = __toESM(require("lodash/isEqual"));
258
+ var import_utils4 = require("@rjsf/utils");
259
+ var AJV8PrecompiledValidator = class {
260
+ /** Constructs an `AJV8PrecompiledValidator` instance using the `validateFns` and `rootSchema`
261
+ *
262
+ * @param validateFns - The map of the validation functions that are generated by the `schemaCompile()` function
263
+ * @param rootSchema - The root schema that was used with the `compileSchema()` function
264
+ * @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
265
+ * @throws - Error when the base schema of the precompiled validator does not have a matching validator function
266
+ */
267
+ constructor(validateFns, rootSchema, localizer) {
268
+ this.rootSchema = rootSchema;
269
+ this.validateFns = validateFns;
270
+ this.localizer = localizer;
271
+ this.mainValidator = this.getValidator(rootSchema);
272
+ this.resolvedRootSchema = (0, import_utils4.retrieveSchema)(this, rootSchema, rootSchema);
273
+ }
274
+ /** Returns the precompiled validator associated with the given `schema` from the map of precompiled validator
275
+ * functions.
276
+ *
277
+ * @param schema - The schema for which a precompiled validator function is desired
278
+ * @returns - The precompiled validator function associated with this schema
279
+ */
280
+ getValidator(schema) {
281
+ const key = (0, import_get2.default)(schema, import_utils4.ID_KEY) || (0, import_utils4.hashForSchema)(schema);
282
+ const validator = this.validateFns[key];
283
+ if (!validator) {
284
+ throw new Error(`No precompiled validator function was found for the given schema for "${key}"`);
285
+ }
286
+ return validator;
287
+ }
288
+ /** Converts an `errorSchema` into a list of `RJSFValidationErrors`
289
+ *
290
+ * @param errorSchema - The `ErrorSchema` instance to convert
291
+ * @param [fieldPath=[]] - The current field path, defaults to [] if not specified
292
+ * @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in
293
+ * the next major release.
294
+ */
295
+ toErrorList(errorSchema, fieldPath = []) {
296
+ return (0, import_utils4.toErrorList)(errorSchema, fieldPath);
297
+ }
298
+ /** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
299
+ * by the playground. Returns the `errors` from the validation
300
+ *
301
+ * @param schema - The schema against which to validate the form data * @param schema
302
+ * @param formData - The form data to validate
303
+ * @throws - Error when the schema provided does not match the base schema of the precompiled validator
304
+ */
305
+ rawValidation(schema, formData) {
306
+ if (!(0, import_isEqual.default)(schema, this.resolvedRootSchema)) {
307
+ throw new Error(
308
+ "The schema associated with the precompiled schema differs from the schema provided for validation"
309
+ );
310
+ }
311
+ this.mainValidator(formData);
312
+ if (typeof this.localizer === "function") {
313
+ this.localizer(this.mainValidator.errors);
314
+ }
315
+ const errors = this.mainValidator.errors || void 0;
316
+ this.mainValidator.errors = null;
317
+ return { errors };
318
+ }
319
+ /** This function processes the `formData` with an optional user contributed `customValidate` function, which receives
320
+ * the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also
321
+ * supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and
322
+ * transform them in what ever way it chooses.
323
+ *
324
+ * @param formData - The form data to validate
325
+ * @param schema - The schema against which to validate the form data
326
+ * @param [customValidate] - An optional function that is used to perform custom validation
327
+ * @param [transformErrors] - An optional function that is used to transform errors after AJV validation
328
+ * @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate`
329
+ */
330
+ validateFormData(formData, schema, customValidate, transformErrors, uiSchema) {
331
+ const rawErrors = this.rawValidation(schema, formData);
332
+ return processRawValidationErrors(this, rawErrors, formData, schema, customValidate, transformErrors, uiSchema);
333
+ }
334
+ /** Validates data against a schema, returning true if the data is valid, or false otherwise. If the schema is
335
+ * invalid, then this function will return false.
336
+ *
337
+ * @param schema - The schema against which to validate the form data
338
+ * @param formData - The form data to validate
339
+ * @param rootSchema - The root schema used to provide $ref resolutions
340
+ * @returns - true if the formData validates against the schema, false otherwise
341
+ * @throws - Error when the schema provided does not match the base schema of the precompiled validator OR if there
342
+ * isn't a precompiled validator function associated with the schema
343
+ */
344
+ isValid(schema, formData, rootSchema) {
345
+ if (!(0, import_isEqual.default)(rootSchema, this.rootSchema)) {
346
+ throw new Error(
347
+ "The schema associated with the precompiled validator differs from the rootSchema provided for validation"
348
+ );
349
+ }
350
+ if ((0, import_get2.default)(schema, import_utils4.ID_KEY) === import_utils4.JUNK_OPTION_ID) {
351
+ return false;
352
+ }
353
+ const validator = this.getValidator(schema);
354
+ return validator(formData);
355
+ }
356
+ };
357
+
358
+ // src/createPrecompiledValidator.ts
359
+ function createPrecompiledValidator(validateFns, rootSchema, localizer) {
360
+ return new AJV8PrecompiledValidator(validateFns, rootSchema, localizer);
361
+ }
362
+
363
+ // src/index.ts
364
+ var src_default = customizeValidator();
365
+ //# sourceMappingURL=index.js.map