diff --git a/lib/2019.ts b/lib/2019.ts index 45a3fa535..096f9d954 100644 --- a/lib/2019.ts +++ b/lib/2019.ts @@ -1,5 +1,5 @@ -import type {AnySchemaObject} from "./types" -import AjvCore, {Options} from "./core" +import * as types from "./types" +import * as core from "./core" import draft7Vocabularies from "./vocabularies/draft7" import dynamicVocabulary from "./vocabularies/dynamic" @@ -10,8 +10,8 @@ import addMetaSchema2019 from "./refs/json-schema-2019-09" const META_SCHEMA_ID = "https://json-schema.org/draft/2019-09/schema" -class Ajv2019 extends AjvCore { - constructor(opts: Options = {}) { +class Ajv2019 extends core.Ajv { + constructor(opts: core.Options = {}) { super({ ...opts, dynamicRef: true, @@ -37,44 +37,78 @@ class Ajv2019 extends AjvCore { this.refs["http://json-schema.org/schema"] = META_SCHEMA_ID } - defaultMeta(): string | AnySchemaObject | undefined { + defaultMeta(): string | types.AnySchemaObject | undefined { return (this.opts.defaultMeta = super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined)) } } -module.exports = exports = Ajv2019 -Object.defineProperty(exports, "__esModule", {value: true}) +export = Ajv2019; +Object.defineProperty(Ajv2019, "__esModule", {value: true}) +Object.defineProperty(Ajv2019, "default", {value: Ajv2019}) -export default Ajv2019 +// eslint-disable-next-line @typescript-eslint/no-namespace, no-redeclare +declare namespace Ajv2019 { + // compatibility with NodeNext + export { Ajv2019 as default }; +} + +import * as compile from "./compile" +import * as compileValidate from "./compile/validate" +import * as vocabulariesErrors from "./vocabularies/errors" +import * as compileRules from "./compile/rules" +import * as typesJsonSchema from "./types/json-schema" +import * as compileCodegen from "./compile/codegen" +import * as runtimeValidationError from "./runtime/validation_error" +import * as compileRefError from "./compile/ref_error" + +// eslint-disable-next-line @typescript-eslint/no-namespace, no-redeclare +namespace Ajv2019 { + /* eslint-disable @typescript-eslint/no-unused-vars */ + export import Format = types.Format; + export import FormatDefinition = types.FormatDefinition; + export import AsyncFormatDefinition = types.AsyncFormatDefinition; + export import KeywordDefinition = types.KeywordDefinition; + export import KeywordErrorDefinition = types.KeywordErrorDefinition; + export import CodeKeywordDefinition = types.CodeKeywordDefinition; + export import MacroKeywordDefinition = types.MacroKeywordDefinition; + export import FuncKeywordDefinition = types.FuncKeywordDefinition; + export import Vocabulary = types.Vocabulary; + export import Schema = types.Schema; + export import SchemaObject = types.SchemaObject; + export import AnySchemaObject = types.AnySchemaObject; + export import AsyncSchema = types.AsyncSchema; + export import AnySchema = types.AnySchema; + export import ValidateFunction = types.ValidateFunction; + export import AsyncValidateFunction = types.AsyncValidateFunction; + export import ErrorObject = types.ErrorObject; + export import ErrorNoParams = types.ErrorNoParams; + +export import Plugin = core.Plugin; +export import Options = core.Options; +export import CodeOptions = core.CodeOptions; +export import InstanceOptions = core.InstanceOptions; +export import Logger = core.Logger; +export import ErrorsTextOptions = core.ErrorsTextOptions; -export { - Format, - FormatDefinition, - AsyncFormatDefinition, - KeywordDefinition, - KeywordErrorDefinition, - CodeKeywordDefinition, - MacroKeywordDefinition, - FuncKeywordDefinition, - Vocabulary, - Schema, - SchemaObject, - AnySchemaObject, - AsyncSchema, - AnySchema, - ValidateFunction, - AsyncValidateFunction, - ErrorObject, - ErrorNoParams, -} from "./types" +export import SchemaCxt = compile.SchemaCxt; +export import SchemaObjCxt = compile.SchemaObjCxt; -export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core" -export {SchemaCxt, SchemaObjCxt} from "./compile" -export {KeywordCxt} from "./compile/validate" -export {DefinedError} from "./vocabularies/errors" -export {JSONType} from "./compile/rules" -export {JSONSchemaType} from "./types/json-schema" -export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen" -export {default as ValidationError} from "./runtime/validation_error" -export {default as MissingRefError} from "./compile/ref_error" +export import KeywordCxt = compileValidate.KeywordCxt; +export import DefinedError = vocabulariesErrors.DefinedError; + +export import JSONType = compileRules.JSONType +export import JSONSchemaType = typesJsonSchema.JSONSchemaType +export import _ = compileCodegen._ +export import str = compileCodegen.str +export import stringify = compileCodegen.stringify +export import nil = compileCodegen.nil +export import Name = compileCodegen.Name +export import Code = compileCodegen.Code +export import CodeGen = compileCodegen.CodeGen +export import CodeGenOptions = compileCodegen.CodeGenOptions + +export import ValidationError = runtimeValidationError.ValidationError +export import MissingRefError = compileRefError.MissingRefError + /* eslint-enable @typescript-eslint/no-unused-vars */ +} diff --git a/lib/2020.ts b/lib/2020.ts index afbdda200..e9ff62866 100644 --- a/lib/2020.ts +++ b/lib/2020.ts @@ -1,5 +1,5 @@ -import type {AnySchemaObject} from "./types" -import AjvCore, {Options} from "./core" +import * as types from "./types" +import * as core from "./core" import draft2020Vocabularies from "./vocabularies/draft2020" import discriminator from "./vocabularies/discriminator" @@ -7,8 +7,8 @@ import addMetaSchema2020 from "./refs/json-schema-2020-12" const META_SCHEMA_ID = "https://json-schema.org/draft/2020-12/schema" -class Ajv2020 extends AjvCore { - constructor(opts: Options = {}) { +class Ajv2020 extends core.Ajv { + constructor(opts: core.Options = {}) { super({ ...opts, dynamicRef: true, @@ -31,44 +31,79 @@ class Ajv2020 extends AjvCore { this.refs["http://json-schema.org/schema"] = META_SCHEMA_ID } - defaultMeta(): string | AnySchemaObject | undefined { + defaultMeta(): string | types.AnySchemaObject | undefined { return (this.opts.defaultMeta = super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined)) } } -module.exports = exports = Ajv2020 -Object.defineProperty(exports, "__esModule", {value: true}) - -export default Ajv2020 - -export { - Format, - FormatDefinition, - AsyncFormatDefinition, - KeywordDefinition, - KeywordErrorDefinition, - CodeKeywordDefinition, - MacroKeywordDefinition, - FuncKeywordDefinition, - Vocabulary, - Schema, - SchemaObject, - AnySchemaObject, - AsyncSchema, - AnySchema, - ValidateFunction, - AsyncValidateFunction, - ErrorObject, - ErrorNoParams, -} from "./types" - -export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core" -export {SchemaCxt, SchemaObjCxt} from "./compile" -export {KeywordCxt} from "./compile/validate" -export {DefinedError} from "./vocabularies/errors" -export {JSONType} from "./compile/rules" -export {JSONSchemaType} from "./types/json-schema" -export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen" -export {default as ValidationError} from "./runtime/validation_error" -export {default as MissingRefError} from "./compile/ref_error" +export = Ajv2020; +Object.defineProperty(Ajv2020, "__esModule", {value: true}) +Object.defineProperty(Ajv2020, "default", {value: Ajv2020}) + +// eslint-disable-next-line @typescript-eslint/no-namespace, no-redeclare +declare namespace Ajv2020 { + // compatibility with NodeNext + export { Ajv2020 as default }; +} + +import * as compile from "./compile" +import * as compileValidate from "./compile/validate" +import * as vocabulariesErrors from "./vocabularies/errors" +import * as compileRules from "./compile/rules" +import * as typesJsonSchema from "./types/json-schema" +import * as compileCodegen from "./compile/codegen" +import * as runtimeValidationError from "./runtime/validation_error" +import * as compileRefError from "./compile/ref_error" + + +// eslint-disable-next-line @typescript-eslint/no-namespace, no-redeclare +namespace Ajv2020 { + /* eslint-disable @typescript-eslint/no-unused-vars */ + export import Format = types.Format; + export import FormatDefinition = types.FormatDefinition; + export import AsyncFormatDefinition = types.AsyncFormatDefinition; + export import KeywordDefinition = types.KeywordDefinition; + export import KeywordErrorDefinition = types.KeywordErrorDefinition; + export import CodeKeywordDefinition = types.CodeKeywordDefinition; + export import MacroKeywordDefinition = types.MacroKeywordDefinition; + export import FuncKeywordDefinition = types.FuncKeywordDefinition; + export import Vocabulary = types.Vocabulary; + export import Schema = types.Schema; + export import SchemaObject = types.SchemaObject; + export import AnySchemaObject = types.AnySchemaObject; + export import AsyncSchema = types.AsyncSchema; + export import AnySchema = types.AnySchema; + export import ValidateFunction = types.ValidateFunction; + export import AsyncValidateFunction = types.AsyncValidateFunction; + export import ErrorObject = types.ErrorObject; + export import ErrorNoParams = types.ErrorNoParams; + + export import Plugin = core.Plugin; + export import Options = core.Options; + export import CodeOptions = core.CodeOptions; + export import InstanceOptions = core.InstanceOptions; + export import Logger = core.Logger; + export import ErrorsTextOptions = core.ErrorsTextOptions; + + export import SchemaCxt = compile.SchemaCxt; + export import SchemaObjCxt = compile.SchemaObjCxt; + + export import KeywordCxt = compileValidate.KeywordCxt; + export import DefinedError = vocabulariesErrors.DefinedError; + + export import JSONType = compileRules.JSONType + export import JSONSchemaType = typesJsonSchema.JSONSchemaType + export import _ = compileCodegen._ + export import str = compileCodegen.str + export import stringify = compileCodegen.stringify + export import nil = compileCodegen.nil + export import Name = compileCodegen.Name + export import Code = compileCodegen.Code + export import CodeGen = compileCodegen.CodeGen + export import CodeGenOptions = compileCodegen.CodeGenOptions + + export import ValidationError = runtimeValidationError.ValidationError + export import MissingRefError = compileRefError.MissingRefError + /* eslint-enable @typescript-eslint/no-unused-vars */ +} diff --git a/lib/ajv.ts b/lib/ajv.ts index 4fa1180ba..165130d47 100644 --- a/lib/ajv.ts +++ b/lib/ajv.ts @@ -38,7 +38,7 @@ Object.defineProperty(Ajv, "default", {value: Ajv}) // eslint-disable-next-line @typescript-eslint/no-namespace, no-redeclare declare namespace Ajv { // compatibility with NodeNext - export {Ajv as default} + export { Ajv as default }; } import * as types from "./types" diff --git a/lib/core.ts b/lib/core.ts index 3686ffe76..46700e4af 100644 --- a/lib/core.ts +++ b/lib/core.ts @@ -272,7 +272,7 @@ export interface Logger { error(...args: unknown[]): unknown } -export default class Ajv { +export class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger @@ -748,6 +748,7 @@ export default class Ajv { } } +export default Ajv; export interface ErrorsTextOptions { separator?: string dataVar?: string diff --git a/lib/jtd.ts b/lib/jtd.ts index 96eb7b9dc..f8eea9969 100644 --- a/lib/jtd.ts +++ b/lib/jtd.ts @@ -1,5 +1,5 @@ -import type {AnySchemaObject, SchemaObject, JTDParser} from "./types" -import type {JTDSchemaType, SomeJTDSchemaType, JTDDataType} from "./types/jtd-schema" +import * as types from "./types"; +import * as typesJtdSchema from "./types/jtd-schema"; import AjvCore, {CurrentOptions} from "./core" import jtdVocabulary from "./vocabularies/jtd" import jtdMetaSchema from "./refs/jtd-schema" @@ -9,34 +9,8 @@ import {SchemaEnv} from "./compile" const META_SCHEMA_ID = "JTD-meta-schema" -type JTDOptions = CurrentOptions & { - // strict mode options not supported with JTD: - strict?: never - allowMatchingProperties?: never - allowUnionTypes?: never - validateFormats?: never - // validation and reporting options not supported with JTD: - $data?: never - verbose?: boolean - $comment?: never - formats?: never - loadSchema?: never - // options to modify validated data: - useDefaults?: never - coerceTypes?: never - // advanced options: - next?: never - unevaluated?: never - dynamicRef?: never - meta?: boolean - defaultMeta?: never - inlineRefs?: boolean - loopRequired?: never - multipleOfPrecision?: never -} - class Ajv extends AjvCore { - constructor(opts: JTDOptions = {}) { + constructor(opts: Ajv.JTDOptions = {}) { super({ ...opts, jtd: true, @@ -54,78 +28,136 @@ class Ajv extends AjvCore { this.addMetaSchema(jtdMetaSchema, META_SCHEMA_ID, false) } - defaultMeta(): string | AnySchemaObject | undefined { + defaultMeta(): string | types.AnySchemaObject | undefined { return (this.opts.defaultMeta = super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined)) } - compileSerializer(schema: SchemaObject): (data: T) => string + compileSerializer(schema: types.SchemaObject): (data: T) => string // Separated for type inference to work // eslint-disable-next-line @typescript-eslint/unified-signatures - compileSerializer(schema: JTDSchemaType): (data: T) => string - compileSerializer(schema: SchemaObject): (data: T) => string { + compileSerializer(schema: typesJtdSchema.JTDSchemaType): (data: T) => string + compileSerializer(schema: types.SchemaObject): (data: T) => string { const sch = this._addSchema(schema) return sch.serialize || this._compileSerializer(sch) } - compileParser(schema: SchemaObject): JTDParser + compileParser(schema: types.SchemaObject): types.JTDParser // Separated for type inference to work // eslint-disable-next-line @typescript-eslint/unified-signatures - compileParser(schema: JTDSchemaType): JTDParser - compileParser(schema: SchemaObject): JTDParser { + compileParser(schema: typesJtdSchema.JTDSchemaType): types.JTDParser + compileParser(schema: types.SchemaObject): types.JTDParser { const sch = this._addSchema(schema) - return (sch.parse || this._compileParser(sch)) as JTDParser + return (sch.parse || this._compileParser(sch)) as types.JTDParser } private _compileSerializer(sch: SchemaEnv): (data: T) => string { - compileSerializer.call(this, sch, (sch.schema as AnySchemaObject).definitions || {}) + compileSerializer.call(this, sch, (sch.schema as types.AnySchemaObject).definitions || {}) /* istanbul ignore if */ if (!sch.serialize) throw new Error("ajv implementation error") return sch.serialize } - private _compileParser(sch: SchemaEnv): JTDParser { - compileParser.call(this, sch, (sch.schema as AnySchemaObject).definitions || {}) + private _compileParser(sch: SchemaEnv): types.JTDParser { + compileParser.call(this, sch, (sch.schema as types.AnySchemaObject).definitions || {}) /* istanbul ignore if */ if (!sch.parse) throw new Error("ajv implementation error") return sch.parse } } -module.exports = exports = Ajv -Object.defineProperty(exports, "__esModule", {value: true}) - -export default Ajv - -export { - Format, - FormatDefinition, - AsyncFormatDefinition, - KeywordDefinition, - KeywordErrorDefinition, - CodeKeywordDefinition, - MacroKeywordDefinition, - FuncKeywordDefinition, - Vocabulary, - Schema, - SchemaObject, - AnySchemaObject, - AsyncSchema, - AnySchema, - ValidateFunction, - AsyncValidateFunction, - ErrorObject, - ErrorNoParams, - JTDParser, -} from "./types" - -export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core" -export {SchemaCxt, SchemaObjCxt} from "./compile" -export {KeywordCxt} from "./compile/validate" -export {JTDErrorObject} from "./vocabularies/jtd" -export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen" - -export {JTDSchemaType, SomeJTDSchemaType, JTDDataType} -export {JTDOptions} -export {default as ValidationError} from "./runtime/validation_error" -export {default as MissingRefError} from "./compile/ref_error" +export = Ajv; +Object.defineProperty(Ajv, "__esModule", {value: true}); +Object.defineProperty(Ajv, "default", {value: Ajv}); + +// eslint-disable-next-line @typescript-eslint/no-namespace, no-redeclare +declare namespace Ajv { + // compatibility with NodeNext + export { Ajv as default }; +} + +import * as core from "./core"; +import * as compile from "./compile"; +import * as compileValidate from "./compile/validate"; +import * as vocabulariesJtd from "./vocabularies/jtd"; +import * as compileCodegen from "./compile/codegen"; +import * as runtimeValidationError from "./runtime/validation_error"; +import * as compileRefError from "./compile/ref_error"; + +// eslint-disable-next-line @typescript-eslint/no-namespace, no-redeclare +namespace Ajv { + /* eslint-disable @typescript-eslint/no-unused-vars */ + export import Format = types.Format; + export import FormatDefinition = types.FormatDefinition; + export import AsyncFormatDefinition = types.AsyncFormatDefinition; + export import KeywordDefinition = types.KeywordDefinition; + export import KeywordErrorDefinition = types.KeywordErrorDefinition; + export import CodeKeywordDefinition = types.CodeKeywordDefinition; + export import MacroKeywordDefinition = types.MacroKeywordDefinition; + export import FuncKeywordDefinition = types.FuncKeywordDefinition; + export import Vocabulary = types.Vocabulary; + export import Schema = types.Schema; + export import SchemaObject = types.SchemaObject; + export import AnySchemaObject = types.AnySchemaObject; + export import AsyncSchema = types.AsyncSchema; + export import AnySchema = types.AnySchema; + export import ValidateFunction = types.ValidateFunction; + export import AsyncValidateFunction = types.AsyncValidateFunction; + export import ErrorObject = types.ErrorObject; + export import ErrorNoParams = types.ErrorNoParams; + export import JTDParser = types.JTDParser; + + export import Plugin = core.Plugin; + export import Options = core.Options; + export import CodeOptions = core.CodeOptions; + export import InstanceOptions = core.InstanceOptions; + export import Logger = core.Logger; + export import ErrorsTextOptions = core.ErrorsTextOptions; + + export import SchemaCxt = compile.SchemaCxt; + export import SchemaObjCxt = compile.SchemaObjCxt; + + export import KeywordCxt = compileValidate.KeywordCxt; + export import JTDErrorObject = vocabulariesJtd.JTDErrorObject; + export import _ = compileCodegen._; + export import str = compileCodegen.str; + export import stringify = compileCodegen.stringify; + export import nil = compileCodegen.nil; + export import Name = compileCodegen.Name; + export import Code = compileCodegen.Code; + export import CodeGen = compileCodegen.CodeGen; + export import CodeGenOptions = compileCodegen.CodeGenOptions; + + export import SomeJTDSchemaType = typesJtdSchema.SomeJTDSchemaType; + export import JTDDataType = typesJtdSchema.JTDDataType; + export import JTDSchemaType = typesJtdSchema.JTDSchemaType; + export import ValidationError = runtimeValidationError.ValidationError; + export import MissingRefError = compileRefError.MissingRefError; + + export type JTDOptions = CurrentOptions & { + // strict mode options not supported with JTD: + strict?: never + allowMatchingProperties?: never + allowUnionTypes?: never + validateFormats?: never + // validation and reporting options not supported with JTD: + $data?: never + verbose?: boolean + $comment?: never + formats?: never + loadSchema?: never + // options to modify validated data: + useDefaults?: never + coerceTypes?: never + // advanced options: + next?: never + unevaluated?: never + dynamicRef?: never + meta?: boolean + defaultMeta?: never + inlineRefs?: boolean + loopRequired?: never + multipleOfPrecision?: never + } + /* eslint-enable @typescript-eslint/no-unused-vars */ +} \ No newline at end of file diff --git a/lib/standalone/index.ts b/lib/standalone/index.ts index b6129ce9e..cc8f01857 100644 --- a/lib/standalone/index.ts +++ b/lib/standalone/index.ts @@ -94,7 +94,14 @@ function standaloneCode( } } -module.exports = exports = standaloneCode -Object.defineProperty(exports, "__esModule", {value: true}) +export = standaloneCode; + +// eslint-disable-next-line @typescript-eslint/no-namespace, no-redeclare +declare namespace standaloneCode { + // compatibility with NodeNext + export { standaloneCode as default }; +} + +Object.defineProperty(standaloneCode, "__esModule", {value: true}) +Object.defineProperty(standaloneCode, "default", {value: standaloneCode}) -export default standaloneCode