@rjsf/validator-ajv6
Advanced tools
Comparing version 5.11.2 to 5.12.0
@@ -0,8 +1,195 @@ | ||
"use strict"; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
'use strict' | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
customizeValidator: () => customizeValidator, | ||
default: () => src_default | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./validator-ajv6.cjs.production.min.js') | ||
} else { | ||
module.exports = require('./validator-ajv6.cjs.development.js') | ||
// src/validator.ts | ||
var import_utils = require("@rjsf/utils"); | ||
// src/createAjvInstance.ts | ||
var import_ajv = __toESM(require("ajv")); | ||
var import_isObject = __toESM(require("lodash/isObject")); | ||
var AJV_CONFIG = { | ||
errorDataPath: "property", | ||
allErrors: true, | ||
multipleOfPrecision: 8, | ||
schemaId: "auto", | ||
unknownFormats: "ignore" | ||
}; | ||
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*\)))$/; | ||
var DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/; | ||
function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}) { | ||
const ajv = new import_ajv.default({ ...AJV_CONFIG, ...ajvOptionsOverrides }); | ||
ajv.addFormat("data-url", DATA_URL_FORMAT_REGEX); | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); | ||
if (Array.isArray(additionalMetaSchemas)) { | ||
ajv.addMetaSchema(additionalMetaSchemas); | ||
} | ||
if ((0, import_isObject.default)(customFormats)) { | ||
Object.keys(customFormats).forEach((formatName) => { | ||
ajv.addFormat(formatName, customFormats[formatName]); | ||
}); | ||
} | ||
return ajv; | ||
} | ||
// src/validator.ts | ||
var AJV6Validator = class { | ||
/** Constructs an `AJV6Validator` instance using the `options` | ||
* | ||
* @param options - The `CustomValidatorOptionsType` options that are used to create the AJV instance | ||
*/ | ||
constructor(options) { | ||
const { additionalMetaSchemas, customFormats, ajvOptionsOverrides } = options; | ||
this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides); | ||
} | ||
/** Converts an `errorSchema` into a list of `RJSFValidationErrors` | ||
* | ||
* @param errorSchema - The `ErrorSchema` instance to convert | ||
* @param [fieldPath=[]] - The current field path, defaults to [] if not specified | ||
* @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in | ||
* the next major release. | ||
*/ | ||
toErrorList(errorSchema, fieldPath = []) { | ||
return (0, import_utils.toErrorList)(errorSchema, fieldPath); | ||
} | ||
/** Transforming the error output from ajv to format used by @rjsf/utils. | ||
* At some point, components should be updated to support ajv. | ||
* | ||
* @param errors - The list of AJV errors to convert to `RJSFValidationErrors` | ||
* @private | ||
*/ | ||
transformRJSFValidationErrors(errors = []) { | ||
return errors.map((e) => { | ||
const { dataPath, keyword, message, params, schemaPath } = e; | ||
const property = `${dataPath}`; | ||
return { | ||
name: keyword, | ||
property, | ||
message, | ||
params, | ||
// specific to ajv | ||
stack: `${property} ${message}`.trim(), | ||
schemaPath | ||
}; | ||
}); | ||
} | ||
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use | ||
* by the playground. Returns the `errors` from the validation | ||
* | ||
* @param schema - The schema against which to validate the form data * @param schema | ||
* @param formData - The form data to validate | ||
*/ | ||
rawValidation(schema, formData) { | ||
let validationError = void 0; | ||
try { | ||
this.ajv.validate(schema, formData); | ||
} catch (err) { | ||
validationError = err; | ||
} | ||
const errors = this.ajv.errors || void 0; | ||
this.ajv.errors = null; | ||
return { errors, validationError }; | ||
} | ||
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives | ||
* the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also | ||
* supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and | ||
* transform them in what ever way it chooses. | ||
* | ||
* @param formData - The form data to validate | ||
* @param schema - The schema against which to validate the form data | ||
* @param [customValidate] - An optional function that is used to perform custom validation | ||
* @param [transformErrors] - An optional function that is used to transform errors after AJV validation | ||
* @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate` | ||
*/ | ||
validateFormData(formData, schema, customValidate, transformErrors, uiSchema) { | ||
const rootSchema = schema; | ||
const rawErrors = this.rawValidation(schema, formData); | ||
const { validationError } = rawErrors; | ||
let errors = this.transformRJSFValidationErrors(rawErrors.errors); | ||
const noProperMetaSchema = validationError && validationError.message && validationError.message.includes("no schema with key or ref "); | ||
if (noProperMetaSchema) { | ||
errors = [...errors, { stack: validationError.message }]; | ||
} | ||
if (typeof transformErrors === "function") { | ||
errors = transformErrors(errors, uiSchema); | ||
} | ||
let errorSchema = (0, import_utils.toErrorSchema)(errors); | ||
if (noProperMetaSchema) { | ||
errorSchema = { | ||
...errorSchema, | ||
...{ | ||
$schema: { | ||
__errors: [validationError.message] | ||
} | ||
} | ||
}; | ||
} | ||
if (typeof customValidate !== "function") { | ||
return { errors, errorSchema }; | ||
} | ||
const newFormData = (0, import_utils.getDefaultFormState)(this, schema, formData, rootSchema, true); | ||
const errorHandler = customValidate(newFormData, (0, import_utils.createErrorHandler)(newFormData), uiSchema); | ||
const userErrorSchema = (0, import_utils.unwrapErrorHandler)(errorHandler); | ||
return (0, import_utils.validationDataMerge)({ errors, errorSchema }, userErrorSchema); | ||
} | ||
/** Validates data against a schema, returning true if the data is valid, or | ||
* false otherwise. If the schema is invalid, then this function will return | ||
* false. | ||
* | ||
* @param schema - The schema against which to validate the form data * @param schema | ||
* @param formData- - The form data to validate | ||
* @param rootSchema - The root schema used to provide $ref resolutions | ||
*/ | ||
isValid(schema, formData, rootSchema) { | ||
try { | ||
const result = this.ajv.addSchema(rootSchema, import_utils.ROOT_SCHEMA_PREFIX).validate((0, import_utils.withIdRefPrefix)(schema), formData); | ||
return result; | ||
} catch (e) { | ||
return false; | ||
} finally { | ||
this.ajv.removeSchema(import_utils.ROOT_SCHEMA_PREFIX); | ||
} | ||
} | ||
}; | ||
// src/customizeValidator.ts | ||
function customizeValidator(options = {}) { | ||
return new AJV6Validator(options); | ||
} | ||
// src/index.ts | ||
var src_default = customizeValidator(); | ||
//# sourceMappingURL=index.js.map |
@@ -1,40 +0,34 @@ | ||
import { toErrorList, toErrorSchema, getDefaultFormState, createErrorHandler, unwrapErrorHandler, validationDataMerge, ROOT_SCHEMA_PREFIX, withIdRefPrefix } from '@rjsf/utils'; | ||
import Ajv from 'ajv'; | ||
import isObject from 'lodash-es/isObject'; | ||
// src/validator.ts | ||
import { | ||
createErrorHandler, | ||
getDefaultFormState, | ||
ROOT_SCHEMA_PREFIX, | ||
toErrorList, | ||
toErrorSchema, | ||
unwrapErrorHandler, | ||
validationDataMerge, | ||
withIdRefPrefix | ||
} from "@rjsf/utils"; | ||
const AJV_CONFIG = { | ||
errorDataPath: 'property', | ||
// src/createAjvInstance.ts | ||
import Ajv from "ajv"; | ||
import isObject from "lodash/isObject"; | ||
var AJV_CONFIG = { | ||
errorDataPath: "property", | ||
allErrors: true, | ||
multipleOfPrecision: 8, | ||
schemaId: 'auto', | ||
unknownFormats: 'ignore' | ||
schemaId: "auto", | ||
unknownFormats: "ignore" | ||
}; | ||
const 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*\)))$/; | ||
const DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/; | ||
/** Creates an Ajv version 6 implementation object with standard support for the 'color` and `data-url` custom formats. | ||
* If `additionalMetaSchemas` are provided then the Ajv instance is modified to add each of the meta schemas in the | ||
* list. If `customFormats` are provided then those additional formats are added to the list of supported formats. If | ||
* `ajvOptionsOverrides` are provided then they are spread on top of the default `AJV_CONFIG` options when constructing | ||
* the `Ajv` instance. | ||
* | ||
* @param [additionalMetaSchemas] - The list of additional meta schemas that the validator can access | ||
* @param [customFormats] - The set of additional custom formats that the validator will support | ||
* @param [ajvOptionsOverrides={}] - The set of validator config override options | ||
* @deprecated in favor of the `@rjsf/validator-ajv8 | ||
*/ | ||
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*\)))$/; | ||
var DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/; | ||
function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides = {}) { | ||
const ajv = new Ajv({ | ||
...AJV_CONFIG, | ||
...ajvOptionsOverrides | ||
}); | ||
// add custom formats | ||
ajv.addFormat('data-url', DATA_URL_FORMAT_REGEX); | ||
ajv.addFormat('color', COLOR_FORMAT_REGEX); | ||
// add more schemas to validate against | ||
const ajv = new Ajv({ ...AJV_CONFIG, ...ajvOptionsOverrides }); | ||
ajv.addFormat("data-url", DATA_URL_FORMAT_REGEX); | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); | ||
if (Array.isArray(additionalMetaSchemas)) { | ||
ajv.addMetaSchema(additionalMetaSchemas); | ||
} | ||
// add more custom formats to validate against | ||
if (isObject(customFormats)) { | ||
Object.keys(customFormats).forEach(formatName => { | ||
Object.keys(customFormats).forEach((formatName) => { | ||
ajv.addFormat(formatName, customFormats[formatName]); | ||
@@ -46,7 +40,4 @@ }); | ||
/** `ValidatorType` implementation that uses the AJV 6 validation mechanism. | ||
* | ||
* @deprecated in favor of the `@rjsf/validator-ajv8 | ||
*/ | ||
class AJV6Validator { | ||
// src/validator.ts | ||
var AJV6Validator = class { | ||
/** Constructs an `AJV6Validator` instance using the `options` | ||
@@ -57,12 +48,3 @@ * | ||
constructor(options) { | ||
/** The AJV instance to use for all validations | ||
* | ||
* @private | ||
*/ | ||
this.ajv = void 0; | ||
const { | ||
additionalMetaSchemas, | ||
customFormats, | ||
ajvOptionsOverrides | ||
} = options; | ||
const { additionalMetaSchemas, customFormats, ajvOptionsOverrides } = options; | ||
this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides); | ||
@@ -87,12 +69,5 @@ } | ||
transformRJSFValidationErrors(errors = []) { | ||
return errors.map(e => { | ||
const { | ||
dataPath, | ||
keyword, | ||
message, | ||
params, | ||
schemaPath | ||
} = e; | ||
return errors.map((e) => { | ||
const { dataPath, keyword, message, params, schemaPath } = e; | ||
const property = `${dataPath}`; | ||
// put data in expected format | ||
return { | ||
@@ -103,2 +78,3 @@ name: keyword, | ||
params, | ||
// specific to ajv | ||
stack: `${property} ${message}`.trim(), | ||
@@ -116,3 +92,3 @@ schemaPath | ||
rawValidation(schema, formData) { | ||
let validationError = undefined; | ||
let validationError = void 0; | ||
try { | ||
@@ -123,9 +99,5 @@ this.ajv.validate(schema, formData); | ||
} | ||
const errors = this.ajv.errors || undefined; | ||
// Clear errors to prevent persistent errors, see #1104 | ||
const errors = this.ajv.errors || void 0; | ||
this.ajv.errors = null; | ||
return { | ||
errors: errors, | ||
validationError | ||
}; | ||
return { errors, validationError }; | ||
} | ||
@@ -146,13 +118,9 @@ /** This function processes the `formData` with an optional user contributed `customValidate` function, which receives | ||
const rawErrors = this.rawValidation(schema, formData); | ||
const { | ||
validationError | ||
} = rawErrors; | ||
const { validationError } = rawErrors; | ||
let errors = this.transformRJSFValidationErrors(rawErrors.errors); | ||
const noProperMetaSchema = validationError && validationError.message && validationError.message.includes('no schema with key or ref '); | ||
const noProperMetaSchema = validationError && validationError.message && validationError.message.includes("no schema with key or ref "); | ||
if (noProperMetaSchema) { | ||
errors = [...errors, { | ||
stack: validationError.message | ||
}]; | ||
errors = [...errors, { stack: validationError.message }]; | ||
} | ||
if (typeof transformErrors === 'function') { | ||
if (typeof transformErrors === "function") { | ||
errors = transformErrors(errors, uiSchema); | ||
@@ -171,16 +139,9 @@ } | ||
} | ||
if (typeof customValidate !== 'function') { | ||
return { | ||
errors, | ||
errorSchema | ||
}; | ||
if (typeof customValidate !== "function") { | ||
return { errors, errorSchema }; | ||
} | ||
// Include form data with undefined values, which is required for custom validation. | ||
const newFormData = getDefaultFormState(this, schema, formData, rootSchema, true); | ||
const errorHandler = customValidate(newFormData, createErrorHandler(newFormData), uiSchema); | ||
const userErrorSchema = unwrapErrorHandler(errorHandler); | ||
return validationDataMerge({ | ||
errors, | ||
errorSchema | ||
}, userErrorSchema); | ||
return validationDataMerge({ errors, errorSchema }, userErrorSchema); | ||
} | ||
@@ -197,6 +158,2 @@ /** Validates data against a schema, returning true if the data is valid, or | ||
try { | ||
// add the rootSchema ROOT_SCHEMA_PREFIX as id. | ||
// then rewrite the schema ref's to point to the rootSchema | ||
// this accounts for the case where schema have references to models | ||
// that lives in the rootSchema but not in the schema in question. | ||
const result = this.ajv.addSchema(rootSchema, ROOT_SCHEMA_PREFIX).validate(withIdRefPrefix(schema), formData); | ||
@@ -207,14 +164,8 @@ return result; | ||
} finally { | ||
// make sure we remove the rootSchema from the global ajv instance | ||
this.ajv.removeSchema(ROOT_SCHEMA_PREFIX); | ||
} | ||
} | ||
} | ||
}; | ||
/** Creates and returns a customized implementation of the `ValidatorType` with the given customization `options` if | ||
* provided. | ||
* | ||
* @param [options={}] - The `CustomValidatorOptionsType` options that are used to create the `ValidatorType` instance | ||
* @deprecated in favor of the `@rjsf/validator-ajv8 | ||
*/ | ||
// src/customizeValidator.ts | ||
function customizeValidator(options = {}) { | ||
@@ -224,7 +175,8 @@ return new AJV6Validator(options); | ||
/** @deprecated in favor of the `@rjsf/validator-ajv8 | ||
*/ | ||
var index = /*#__PURE__*/customizeValidator(); | ||
export { customizeValidator, index as default }; | ||
// src/index.ts | ||
var src_default = customizeValidator(); | ||
export { | ||
customizeValidator, | ||
src_default as default | ||
}; | ||
//# sourceMappingURL=validator-ajv6.esm.js.map |
{ | ||
"name": "@rjsf/validator-ajv6", | ||
"version": "5.11.2", | ||
"version": "5.12.0", | ||
"main": "dist/index.js", | ||
"module": "dist/validator-ajv6.esm.js", | ||
"typings": "dist/index.d.ts", | ||
"module": "lib/index.js", | ||
"typings": "lib/index.d.ts", | ||
"description": "The ajv-6 based validator for @rjsf/core", | ||
"files": [ | ||
"dist" | ||
"dist", | ||
"lib", | ||
"src" | ||
], | ||
@@ -16,3 +18,7 @@ "engineStrict": false, | ||
"scripts": { | ||
"build": "rimraf dist && dts build --rollupTypes --format cjs,esm,umd", | ||
"build:ts": "rimraf lib && tsc", | ||
"build:cjs": "esbuild ./src/index.ts --bundle --outfile=dist/index.js --sourcemap --packages=external --format=cjs", | ||
"build:esm": "esbuild ./src/index.ts --bundle --outfile=dist/validator-ajv6.esm.js --sourcemap --packages=external --format=esm", | ||
"build:umd": "rollup dist/validator-ajv6.esm.js --format=umd --file=dist/validator-ajv6.umd.js --name=@rjsf/validator-ajv6", | ||
"build": "rimraf dist && npm run build:ts && npm run build:cjs && npm run build:esm && npm run build:umd", | ||
"cs-check": "prettier -l \"{src,test}/**/*.ts?(x)\"", | ||
@@ -45,13 +51,13 @@ "cs-format": "prettier \"{src,test}/**/*.ts?(x)\" --write", | ||
"@babel/preset-typescript": "^7.22.5", | ||
"@rjsf/utils": "^5.11.2", | ||
"@types/jest-expect-message": "^1.1.0", | ||
"@rjsf/utils": "^5.12.0", | ||
"@types/json-schema": "^7.0.12", | ||
"@types/lodash": "^4.14.195", | ||
"babel-jest": "^29.6.1", | ||
"dts-cli": "^1.6.3", | ||
"eslint": "^8.44.0", | ||
"jest": "^29.6.1", | ||
"@types/lodash": "^4.14.196", | ||
"babel-jest": "^29.6.2", | ||
"esbuild": "^0.18.19", | ||
"eslint": "^8.46.0", | ||
"jest": "^29.6.2", | ||
"jest-environment-jsdom": "^29.6.1", | ||
"jest-expect-message": "^1.1.3", | ||
"rimraf": "^5.0.1" | ||
"rimraf": "^5.0.1", | ||
"rollup": "^3.27.2", | ||
"typescript": "^4.9.5" | ||
}, | ||
@@ -79,3 +85,3 @@ "publishConfig": { | ||
"license": "Apache-2.0", | ||
"gitHead": "2fafced84e18aa2cd487715d11d2730cd23333f3" | ||
"gitHead": "bab797c43a34e5ec68a84b2bdabb3c377e46b092" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
28
1068
1
0
101171
1