Comparing version 8.8.2 to 8.9.0
@@ -38,3 +38,4 @@ export declare abstract class _CodeOrName { | ||
export declare function getProperty(key: Code | string | number): Code; | ||
export declare function getEsmExportName(key: Code | string | number): Code; | ||
export declare function regexpCode(rx: RegExp): Code; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.regexpCode = exports.getProperty = exports.safeStringify = exports.stringify = exports.strConcat = exports.addCodeArg = exports.str = exports._ = exports.nil = exports._Code = exports.Name = exports.IDENTIFIER = exports._CodeOrName = void 0; | ||
exports.regexpCode = exports.getEsmExportName = exports.getProperty = exports.safeStringify = exports.stringify = exports.strConcat = exports.addCodeArg = exports.str = exports._ = exports.nil = exports._Code = exports.Name = exports.IDENTIFIER = exports._CodeOrName = void 0; | ||
class _CodeOrName { | ||
@@ -143,2 +143,10 @@ } | ||
exports.getProperty = getProperty; | ||
//Does best effort to format the name properly | ||
function getEsmExportName(key) { | ||
if (typeof key == "string" && exports.IDENTIFIER.test(key)) { | ||
return new _Code(`${key}`); | ||
} | ||
throw new Error(`CodeGen: invalid export name: ${key}, use explicit $id name mapping`); | ||
} | ||
exports.getEsmExportName = getEsmExportName; | ||
function regexpCode(rx) { | ||
@@ -145,0 +153,0 @@ return new _Code(rx.toString()); |
@@ -74,2 +74,3 @@ export { Format, FormatDefinition, AsyncFormatDefinition, KeywordDefinition, KeywordErrorDefinition, CodeKeywordDefinition, MacroKeywordDefinition, FuncKeywordDefinition, Vocabulary, Schema, SchemaObject, AnySchemaObject, AsyncSchema, AnySchema, ValidateFunction, AsyncValidateFunction, AnyValidateFunction, ErrorObject, ErrorNoParams, } from "./types"; | ||
es5?: boolean; | ||
esm?: boolean; | ||
lines?: boolean; | ||
@@ -76,0 +77,0 @@ optimize?: boolean | number; |
@@ -25,2 +25,6 @@ "use strict"; | ||
const vCode = validateCode(usedValues, source); | ||
if (ajv.opts.code.esm) { | ||
// Always do named export as `validate` rather than the variable `n` which is `validateXX` for known export value | ||
return `"use strict";${_n}export const validate = ${n};${_n}export default ${n};${_n}${vCode}`; | ||
} | ||
return `"use strict";${_n}module.exports = ${n};${_n}module.exports.default = ${n};${_n}${vCode}`; | ||
@@ -36,3 +40,6 @@ } | ||
const vCode = validateCode(usedValues, v.source); | ||
code = (0, code_1._) `${code}${_n}exports${(0, code_1.getProperty)(name)} = ${(_a = v.source) === null || _a === void 0 ? void 0 : _a.validateName};${_n}${vCode}`; | ||
const exportSyntax = ajv.opts.code.esm | ||
? (0, code_1._) `export const ${(0, code_1.getEsmExportName)(name)}` | ||
: (0, code_1._) `exports${(0, code_1.getProperty)(name)}`; | ||
code = (0, code_1._) `${code}${_n}${exportSyntax} = ${(_a = v.source) === null || _a === void 0 ? void 0 : _a.validateName};${_n}${vCode}`; | ||
} | ||
@@ -39,0 +46,0 @@ } |
@@ -121,3 +121,4 @@ /** numeric strings */ | ||
}; | ||
declare type JTDDataDef<S, D extends Record<string, unknown>> = (S extends { | ||
declare type JTDDataDef<S, D extends Record<string, unknown>> = // ref | ||
(S extends { | ||
ref: string; | ||
@@ -124,0 +125,0 @@ } ? D extends { |
@@ -5,2 +5,4 @@ "use strict"; | ||
const types_1 = require("../discriminator/types"); | ||
const compile_1 = require("../../compile"); | ||
const util_1 = require("../../compile/util"); | ||
const error = { | ||
@@ -57,6 +59,11 @@ message: ({ params: { discrError, tagName } }) => discrError === types_1.DiscrError.Tag | ||
for (let i = 0; i < oneOf.length; i++) { | ||
const sch = oneOf[i]; | ||
const propSch = (_a = sch.properties) === null || _a === void 0 ? void 0 : _a[tagName]; | ||
let sch = oneOf[i]; | ||
if ((sch === null || sch === void 0 ? void 0 : sch.$ref) && !(0, util_1.schemaHasRulesButRef)(sch, it.self.RULES)) { | ||
sch = compile_1.resolveRef.call(it.self, it.schemaEnv, it.baseId, sch === null || sch === void 0 ? void 0 : sch.$ref); | ||
if (sch instanceof compile_1.SchemaEnv) | ||
sch = sch.schema; | ||
} | ||
const propSch = (_a = sch === null || sch === void 0 ? void 0 : sch.properties) === null || _a === void 0 ? void 0 : _a[tagName]; | ||
if (typeof propSch != "object") { | ||
throw new Error(`discriminator: oneOf schemas must have "properties/${tagName}"`); | ||
throw new Error(`discriminator: oneOf subschemas (or referenced schemas) must have "properties/${tagName}"`); | ||
} | ||
@@ -63,0 +70,0 @@ tagRequired = tagRequired && (topRequired || hasRequired(sch)); |
@@ -158,4 +158,12 @@ export abstract class _CodeOrName { | ||
//Does best effort to format the name properly | ||
export function getEsmExportName(key: Code | string | number): Code { | ||
if (typeof key == "string" && IDENTIFIER.test(key)) { | ||
return new _Code(`${key}`) | ||
} | ||
throw new Error(`CodeGen: invalid export name: ${key}, use explicit $id name mapping`) | ||
} | ||
export function regexpCode(rx: RegExp): Code { | ||
return new _Code(rx.toString()) | ||
} |
@@ -143,2 +143,3 @@ export { | ||
es5?: boolean | ||
esm?: boolean | ||
lines?: boolean | ||
@@ -145,0 +146,0 @@ optimize?: boolean | number |
@@ -5,3 +5,3 @@ import type AjvCore from "../core" | ||
import {UsedScopeValues, UsedValueState, ValueScopeName, varKinds} from "../compile/codegen/scope" | ||
import {_, nil, _Code, Code, getProperty} from "../compile/codegen/code" | ||
import {_, nil, _Code, Code, getProperty, getEsmExportName} from "../compile/codegen/code" | ||
@@ -34,2 +34,6 @@ function standaloneCode( | ||
const vCode = validateCode(usedValues, source) | ||
if (ajv.opts.code.esm) { | ||
// Always do named export as `validate` rather than the variable `n` which is `validateXX` for known export value | ||
return `"use strict";${_n}export const validate = ${n};${_n}export default ${n};${_n}${vCode}` | ||
} | ||
return `"use strict";${_n}module.exports = ${n};${_n}module.exports.default = ${n};${_n}${vCode}` | ||
@@ -48,3 +52,6 @@ } | ||
const vCode = validateCode(usedValues, v.source) | ||
code = _`${code}${_n}exports${getProperty(name)} = ${v.source?.validateName};${_n}${vCode}` | ||
const exportSyntax = ajv.opts.code.esm | ||
? _`export const ${getEsmExportName(name)}` | ||
: _`exports${getProperty(name)}` | ||
code = _`${code}${_n}${exportSyntax} = ${v.source?.validateName};${_n}${vCode}` | ||
} | ||
@@ -51,0 +58,0 @@ } |
@@ -116,6 +116,3 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ | ||
maxProperties?: number | ||
} & (// "required" type does not guarantee that all required properties | ||
// are listed it only asserts that optional cannot be listed. | ||
// "required" is not necessary if it's a non-partial type with no required keys | ||
IsPartial extends true | ||
} & (IsPartial extends true // "required" is not necessary if it's a non-partial type with no required keys // are listed it only asserts that optional cannot be listed. // "required" type does not guarantee that all required properties | ||
? {required: Readonly<(keyof T)[]>} | ||
@@ -122,0 +119,0 @@ : [UncheckedRequiredMembers<T>] extends [never] |
@@ -120,5 +120,4 @@ /** numeric strings */ | ||
| (unknown extends T ? {nullable?: boolean} : never) | ||
// all other types | ||
| ((// numbers - only accepts the type number | ||
true extends NullTypeEquality<T, number> | ||
// all other types // numbers - only accepts the type number | ||
| ((true extends NullTypeEquality<T, number> | ||
? {type: NumberType} | ||
@@ -202,4 +201,4 @@ : // booleans - accepts the type boolean | ||
type JTDDataDef<S, D extends Record<string, unknown>> = | ||
| (// ref | ||
S extends {ref: string} | ||
| // ref | ||
(S extends {ref: string} | ||
? D extends {[K in S["ref"]]: infer V} | ||
@@ -206,0 +205,0 @@ ? JTDDataDef<V, D> |
@@ -5,2 +5,4 @@ import type {CodeKeywordDefinition, AnySchemaObject, KeywordErrorDefinition} from "../../types" | ||
import {DiscrError, DiscrErrorObj} from "../discriminator/types" | ||
import {resolveRef, SchemaEnv} from "../../compile" | ||
import {schemaHasRulesButRef} from "../../compile/util" | ||
@@ -66,6 +68,12 @@ export type DiscriminatorError = DiscrErrorObj<DiscrError.Tag> | DiscrErrorObj<DiscrError.Mapping> | ||
for (let i = 0; i < oneOf.length; i++) { | ||
const sch = oneOf[i] | ||
const propSch = sch.properties?.[tagName] | ||
let sch = oneOf[i] | ||
if (sch?.$ref && !schemaHasRulesButRef(sch, it.self.RULES)) { | ||
sch = resolveRef.call(it.self, it.schemaEnv, it.baseId, sch?.$ref) | ||
if (sch instanceof SchemaEnv) sch = sch.schema | ||
} | ||
const propSch = sch?.properties?.[tagName] | ||
if (typeof propSch != "object") { | ||
throw new Error(`discriminator: oneOf schemas must have "properties/${tagName}"`) | ||
throw new Error( | ||
`discriminator: oneOf subschemas (or referenced schemas) must have "properties/${tagName}"` | ||
) | ||
} | ||
@@ -72,0 +80,0 @@ tagRequired = tagRequired && (topRequired || hasRequired(sch)) |
{ | ||
"name": "ajv", | ||
"version": "8.8.2", | ||
"version": "8.9.0", | ||
"description": "Another JSON Schema Validator", | ||
@@ -74,3 +74,3 @@ "main": "dist/ajv.js", | ||
"@types/mocha": "^9.0.0", | ||
"@types/node": "^16.3.2", | ||
"@types/node": "^17.0.0", | ||
"@types/require-from-string": "^1.2.0", | ||
@@ -96,4 +96,5 @@ "@typescript-eslint/eslint-plugin": "^3.8.0", | ||
"karma-mocha": "^2.0.0", | ||
"lint-staged": "^11.0.0", | ||
"lint-staged": "^12.1.1", | ||
"mocha": "^9.0.2", | ||
"module-from-string": "^3.1.3", | ||
"node-fetch": "^3.0.0", | ||
@@ -100,0 +101,0 @@ "nyc": "^15.0.0", |
@@ -24,3 +24,3 @@ <img align="right" alt="Ajv logo" width="160" src="https://ajv.js.org/img/ajv.svg"> | ||
[<img src="https://ajv.js.org/img/retool.svg" width="22.5%" alt="Retool">](https://retool.com/?utm_source=sponsor&utm_campaign=ajv)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/tidelift.svg" width="22.5%" alt="Tidelift">](https://tidelift.com/subscription/pkg/npm-ajv?utm_source=npm-ajv&utm_medium=referral&utm_campaign=enterprise)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/reserved.svg" width="22.5%">](https://opencollective.com/ajv)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/reserved.svg" width="22.5%">](https://opencollective.com/ajv) | ||
[<img src="https://ajv.js.org/img/retool.svg" width="22.5%" alt="Retool">](https://retool.com/?utm_source=sponsor&utm_campaign=ajv)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/tidelift.svg" width="22.5%" alt="Tidelift">](https://tidelift.com/subscription/pkg/npm-ajv?utm_source=npm-ajv&utm_medium=referral&utm_campaign=enterprise)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/simplex.svg" width="22.5%" alt="SimpleX">](https://github.com/simplex-chat/simplex-chat)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/reserved.svg" width="22.5%">](https://opencollective.com/ajv) | ||
@@ -27,0 +27,0 @@ ## Contributing |
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
1012740
19813
40