@kubernetes-models/validate
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -1,3 +0,3 @@ | ||
import Ajv from "ajv"; | ||
import Ajv, { AnySchema } from "ajv"; | ||
export declare const ajv: Ajv; | ||
export declare function register(id: string, schema: object): void; | ||
export declare function register(id: string, schema: AnySchema): void; |
@@ -19,6 +19,7 @@ "use strict"; | ||
exports.ajv = new ajv_1.default({ | ||
strictTypes: false | ||
strictTypes: false, | ||
allErrors: true, | ||
verbose: true | ||
}); | ||
ajv_formats_1.default(exports.ajv); | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
(0, ajv_formats_1.default)(exports.ajv); | ||
function register(id, schema) { | ||
@@ -25,0 +26,0 @@ if (!exports.ajv.getSchema(id)) { |
@@ -7,6 +7,50 @@ "use strict"; | ||
const ajv_2 = require("./ajv.js"); | ||
function excludeNullableRefErrors(errors) { | ||
const result = []; | ||
const schemaPathsToExclude = new Set(); | ||
for (const err of errors) { | ||
/* | ||
We want to exclude nullable $ref type like this: | ||
{ | ||
instancePath: '', | ||
schemaPath: '#/oneOf', | ||
keyword: 'oneOf', | ||
params: { passingSchemas: null }, | ||
message: 'must match exactly one schema in oneOf', | ||
schema: [ { '$ref': 'str' }, { type: 'null' } ], | ||
parentSchema: { oneOf: [ [Object], [Object] ] }, | ||
data: 3.14 | ||
} | ||
*/ | ||
if (err.keyword === "oneOf") { | ||
const nullTypeIndex = err.schema?.findIndex((x) => x.type === "null"); | ||
if (nullTypeIndex > -1) { | ||
/* | ||
oneOf error usually comes with the error below, and we want to remove | ||
it as well. | ||
{ | ||
instancePath: '', | ||
schemaPath: '#/oneOf/1/type', | ||
keyword: 'type', | ||
params: { type: 'null' }, | ||
message: 'must be null', | ||
schema: 'null', | ||
parentSchema: { type: 'null' }, | ||
data: 3.14 | ||
} | ||
*/ | ||
schemaPathsToExclude.add(`${err.schemaPath}/${nullTypeIndex}/type`); | ||
continue; | ||
} | ||
} | ||
result.push(err); | ||
} | ||
return result.filter((x) => !schemaPathsToExclude.has(x.schemaPath)); | ||
} | ||
function validate(id, data) { | ||
if (!ajv_2.ajv.validate(id, data) && ajv_2.ajv.errors) { | ||
const err = new ajv_1.default.ValidationError(ajv_2.ajv.errors); | ||
err.message = ajv_2.ajv.errorsText(ajv_2.ajv.errors); | ||
const errors = excludeNullableRefErrors(ajv_2.ajv.errors); | ||
const err = new ajv_1.default.ValidationError(errors); | ||
err.message = ajv_2.ajv.errorsText(errors); | ||
throw err; | ||
@@ -13,0 +57,0 @@ } |
{ | ||
"name": "@kubernetes-models/validate", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"main": "dist/index.js", | ||
@@ -17,3 +17,3 @@ "module": "dist/index.mjs", | ||
}, | ||
"homepage": "https://github.com/tommy351/kubernetes-models-ts/tree/master/packages/validate", | ||
"homepage": "https://github.com/tommy351/kubernetes-models-ts/tree/master/core/validate", | ||
"author": "Tommy Chen <tommy351@gmail.com>", | ||
@@ -33,9 +33,9 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=12" | ||
"node": ">=14" | ||
}, | ||
"dependencies": { | ||
"ajv": "^8.6.2", | ||
"ajv-formats": "^2.1.0", | ||
"tslib": "^2.3.0" | ||
"ajv": "^8.11.0", | ||
"ajv-formats": "^2.1.1", | ||
"tslib": "^2.4.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
15143
203
0
Updatedajv@^8.11.0
Updatedajv-formats@^2.1.1
Updatedtslib@^2.4.0