Comparing version 2.3.0 to 2.4.0
@@ -20,5 +20,6 @@ "use strict"; | ||
const annotations = (_a = validator_1.exposeCoreValidator(validator)._annotations) === null || _a === void 0 ? void 0 : _a.options; | ||
if (validator_2.isRaw(validator) && validator.fragment) { | ||
const raw = validator_2.getRaw(validator); | ||
if (raw && raw.fragment) { | ||
if (!(annotations === null || annotations === void 0 ? void 0 : annotations.name)) | ||
return { ...annotations, name: validator.fragment }; | ||
return { ...annotations, name: raw.fragment }; | ||
} | ||
@@ -31,4 +32,5 @@ return annotations; | ||
const name = (_b = (_a = validator_1.exposeCoreValidator(validator)._annotations) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.name; | ||
if (!name && validator_2.isRaw(validator) && validator.fragment) { | ||
return validator.fragment; | ||
const raw = validator_2.getRaw(validator); | ||
if (!name && raw && raw.fragment) { | ||
return raw.fragment; | ||
} | ||
@@ -41,4 +43,5 @@ return name; | ||
const name = (_b = (_a = validator_1.exposeCoreValidator(validator)._annotations) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.name; | ||
const otherNames = validator_2.isRaw(validator) && validator.fragment | ||
? Object.keys(validator.toSchema().definitions) | ||
const raw = validator_2.getRaw(validator); | ||
const otherNames = raw && raw.fragment | ||
? Object.keys(raw.toSchema().definitions) | ||
: []; | ||
@@ -45,0 +48,0 @@ return name ? [...new Set([name, ...otherNames])] : otherNames; |
@@ -26,3 +26,3 @@ import { CoreValidator } from "../validators/core/validator"; | ||
object: <T extends { | ||
[key: string]: BaseValidator<unknown, BaseValidator<unknown, any>>; | ||
[key: string]: CoreValidator<unknown>; | ||
}>(obj: T) => ObjectValidator<ExtractObject<T>>; | ||
@@ -32,5 +32,5 @@ array: ArrayFunction & TupleFunction; | ||
null: () => NullValidator<null>; | ||
anyOf: <T_1 extends BaseValidator<unknown, BaseValidator<unknown, any>>>(validators: readonly T_1[]) => AnyOfValidator<TypeOf<T_1, false>>; | ||
allOf: <T_2 extends BaseValidator<unknown, BaseValidator<unknown, any>>>(validators: readonly T_2[]) => AllOfValidator<TypeOf<T_2, false>>; | ||
if: <T_3 extends BaseValidator<unknown, BaseValidator<unknown, any>>>(validator: T_3) => IfValidator<TypeOf<T_3, false>>; | ||
anyOf: <T_1 extends CoreValidator<unknown>>(validators: readonly T_1[]) => AnyOfValidator<TypeOf<T_1, false>>; | ||
allOf: <T_2 extends CoreValidator<unknown>>(validators: readonly T_2[]) => AllOfValidator<TypeOf<T_2, false>>; | ||
if: <T_3 extends CoreValidator<unknown>>(validator: T_3) => IfValidator<TypeOf<T_3, false>>; | ||
any: () => AnyValidator<any>; | ||
@@ -37,0 +37,0 @@ unknown: () => AnyValidator<unknown>; |
@@ -13,2 +13,6 @@ import type { ExportRefMethod, OnTopLevelNameConflict, OnNonSuretypeValidator } from "./types"; | ||
} | ||
export interface ExtractedJsonSchema { | ||
schema: SchemaWithDefinitions; | ||
lookup: Map<CoreValidator<unknown>, any>; | ||
} | ||
/** | ||
@@ -20,5 +24,3 @@ * Get the JSON schema (as a JavaScript object) for an array of schema | ||
*/ | ||
export declare function extractJsonSchema(validators: Array<CoreValidator<unknown>>, { refMethod, onTopLevelNameConflict, onNonSuretypeValidator, }?: ExtractJsonSchemaOptions): { | ||
schema: SchemaWithDefinitions; | ||
}; | ||
export declare function extractJsonSchema(validators: Array<CoreValidator<unknown>>, { refMethod, onTopLevelNameConflict, onNonSuretypeValidator, }?: ExtractJsonSchemaOptions): ExtractedJsonSchema; | ||
export declare type ExtractSingleSchemaResult = { | ||
@@ -25,0 +27,0 @@ schema: Record<string, any>; |
@@ -39,5 +39,5 @@ "use strict"; | ||
} | ||
const traverser = new tree_traverser_1.TreeTraverserImpl(validators, refMethod); | ||
const { schema } = traverser.getSchema(); | ||
return { schema }; | ||
const traverser = new tree_traverser_1.TreeTraverserImpl(validators, refMethod, onNonSuretypeValidator === 'lookup'); | ||
const { schema, lookup } = traverser.getSchema(); | ||
return { schema, lookup }; | ||
} | ||
@@ -54,4 +54,5 @@ exports.extractJsonSchema = extractJsonSchema; | ||
function extractSingleJsonSchema(validator) { | ||
if (validator_1.isRaw(validator)) | ||
return { schema: validator.toSchema(), fragment: validator.fragment }; | ||
const raw = validator_1.getRaw(validator); | ||
if (raw) | ||
return { schema: raw.toSchema(), fragment: raw.fragment }; | ||
const { schema: { definitions } } = extractJsonSchema([validator], { | ||
@@ -58,0 +59,0 @@ refMethod: 'no-refs', |
@@ -7,3 +7,3 @@ export * from "./api"; | ||
export { ValidateFunction, compile, validate }; | ||
export { ExtractJsonSchemaOptions, SchemaWithDefinitions, extractJsonSchema, extractSingleJsonSchema, } from "./extract-json-schema"; | ||
export { ExtractJsonSchemaOptions, SchemaWithDefinitions, ExtractedJsonSchema, extractJsonSchema, extractSingleJsonSchema, } from "./extract-json-schema"; | ||
export { getValidatorSchema } from "./validation"; | ||
@@ -10,0 +10,0 @@ export { Annotations, TopLevelAnnotations, getAnnotations, } from "./annotations"; |
@@ -62,5 +62,6 @@ "use strict"; | ||
const ajv = new Ajv(options); | ||
if (validator_1.isRaw(validator) && validator.fragment) { | ||
const { fragment } = validator; | ||
ajv.addSchema(validator.toSchema()); | ||
const raw = validator_1.getRaw(validator); | ||
if (raw && raw.fragment) { | ||
const { fragment } = raw; | ||
ajv.addSchema(raw.toSchema()); | ||
const validatorFn = ajv.getSchema(`#/definitions/${fragment}`); | ||
@@ -67,0 +68,0 @@ if (!validatorFn) |
import { TreeTraverser, CoreValidator } from "./validators/core/validator"; | ||
import type { ExportRefMethod } from "./types"; | ||
export interface SchemaResult { | ||
schema: any; | ||
duplicates: Map<string, number>; | ||
lookup: Map<CoreValidator<unknown>, any>; | ||
} | ||
export declare class TreeTraverserImpl implements TreeTraverser { | ||
private refMethod; | ||
private allowUnnamed; | ||
private initialValidators; | ||
@@ -9,10 +15,8 @@ private extraValidators; | ||
private definitions; | ||
private lookupMap; | ||
private duplicates; | ||
currentSchemaName: string | undefined; | ||
constructor(initialValidators: Array<CoreValidator<unknown>>, refMethod: ExportRefMethod); | ||
constructor(validators: Array<CoreValidator<unknown>>, refMethod: ExportRefMethod, allowUnnamed: boolean); | ||
visit(validator: CoreValidator<unknown>): any; | ||
getSchema(): { | ||
schema: any; | ||
duplicates: Map<string, number>; | ||
}; | ||
getSchema(): SchemaResult; | ||
private getValidatorName; | ||
@@ -19,0 +23,0 @@ private insert; |
@@ -8,4 +8,5 @@ "use strict"; | ||
class TreeTraverserImpl { | ||
constructor(initialValidators, refMethod) { | ||
constructor(validators, refMethod, allowUnnamed) { | ||
this.refMethod = refMethod; | ||
this.allowUnnamed = allowUnnamed; | ||
this.initialValidators = new Map(); | ||
@@ -15,6 +16,9 @@ this.extraValidators = new Map(); | ||
this.definitions = {}; | ||
this.lookupMap = new Map(); | ||
this.duplicates = new Map(); | ||
this.currentSchemaName = undefined; | ||
const rawValidators = initialValidators.filter(validator_1.isRaw); | ||
const regularValidators = initialValidators.filter(validator => !validator_1.isRaw(validator)); | ||
const rawValidators = validators | ||
.filter(validator_1.isRaw) | ||
.map(validator => validator_1.getRaw(validator)); | ||
const regularValidators = validators.filter(validator => !validator_1.isRaw(validator)); | ||
rawValidators | ||
@@ -25,3 +29,3 @@ .forEach(validator => { | ||
Object | ||
.entries(schema) | ||
.entries(schema.definitions) | ||
.forEach(([fragment, subSchema]) => { | ||
@@ -33,4 +37,8 @@ const name = this.getNextName(fragment); | ||
else { | ||
this.lookupMap.set(validator, schema); | ||
const name = this.getNextName(annotations_1.getName(validator)); | ||
this.definitions[name] = schema; | ||
if (name) | ||
this.definitions[name] = schema; | ||
else if (!allowUnnamed) | ||
throw new TypeError("Got unnamed validator"); | ||
} | ||
@@ -54,2 +62,3 @@ }); | ||
duplicates: this.duplicates, | ||
lookup: this.lookupMap, | ||
}; | ||
@@ -75,4 +84,8 @@ } | ||
insert({ name, validator }) { | ||
this.currentSchemaName = name; | ||
this.definitions[name] = validation_1.validatorToSchema(validator, this); | ||
if (name) | ||
this.currentSchemaName = name; | ||
const schema = validation_1.validatorToSchema(validator, this); | ||
this.lookupMap.set(validator, schema); | ||
if (name) | ||
this.definitions[name] = schema; | ||
this.currentSchemaName = undefined; | ||
@@ -82,3 +95,6 @@ return name; | ||
makeRef(validator, extra) { | ||
const name = this.getNextName(annotations_1.getName(validator)); | ||
const baseName = annotations_1.getName(validator); | ||
if (!baseName && !extra && this.allowUnnamed) | ||
return { validator }; | ||
const name = this.getNextName(baseName); | ||
if (extra) | ||
@@ -100,4 +116,4 @@ this.extraValidators.set(validator, name); | ||
let i = baseName ? 1 : 0; | ||
while (++i) { | ||
const name = iterationName + `_${i}`; | ||
while (true) { | ||
const name = iterationName + `_${++i}`; | ||
if (!this.validatorNames.has(name)) { | ||
@@ -108,5 +124,4 @@ this.validatorNames.add(name); | ||
} | ||
return 'x'; // TS-dummy | ||
} | ||
} | ||
exports.TreeTraverserImpl = TreeTraverserImpl; |
export declare type ExportRefMethod = 'no-refs' | 'provided' | 'ref-all'; | ||
export declare type OnTopLevelNameConflict = 'error' | 'rename'; | ||
export declare type OnNonSuretypeValidator = 'error' | 'ignore' | 'create-name'; | ||
export declare type OnNonSuretypeValidator = 'error' | 'ignore' | 'create-name' | 'lookup'; |
import type { AnyType } from "./validators/types"; | ||
import { CoreValidator, TreeTraverser } from "./validators/core/validator"; | ||
import { BaseValidator } from "./validators/base/validator"; | ||
export declare function validatorToSchema<T extends CoreValidator<unknown>>(validator: T, traverser: TreeTraverser): any; | ||
export declare function validatorType<T extends BaseValidator<unknown>>(validator: T): AnyType; | ||
export declare function validatorType<T extends CoreValidator<unknown>>(validator: T): AnyType; | ||
export declare function cloneValidator<T extends CoreValidator<unknown>>(validator: T, clean: boolean): T; | ||
@@ -7,0 +6,0 @@ export declare function attachSchemaToValidator<Fn extends Function>(validator: Fn, schema: CoreValidator<unknown>): typeof validator; |
@@ -5,4 +5,3 @@ "use strict"; | ||
const validator_1 = require("./validators/core/validator"); | ||
const validator_2 = require("./validators/base/validator"); | ||
const validator_3 = require("./validators/raw/validator"); | ||
const validator_2 = require("./validators/raw/validator"); | ||
function validatorToSchema(validator, traverser) { | ||
@@ -13,3 +12,3 @@ return validator_1.exposeCoreValidator(validator).toSchema(traverser); | ||
function validatorType(validator) { | ||
return validator_2.exposeBaseValidator(validator).type; | ||
return validator_1.exposeCoreValidator(validator).type; | ||
} | ||
@@ -37,6 +36,10 @@ exports.validatorType = validatorType; | ||
function uniqValidators(validators) { | ||
validators = [...new Set(validators)]; | ||
return [ | ||
...new Map(validators.map(validator => validator_3.isRaw(validator) | ||
? [validator.toSchema(), validator] | ||
: [{}, validator])) | ||
...new Map(validators.map(validator => { | ||
const raw = validator_2.getRaw(validator); | ||
return raw | ||
? [raw.toSchema(), raw] | ||
: [{}, validator]; | ||
})) | ||
.values() | ||
@@ -43,0 +46,0 @@ ]; |
import { AnyType } from "../types"; | ||
import { TreeTraverser } from "../core/validator"; | ||
import { CoreValidator, TreeTraverser } from "../core/validator"; | ||
import { BaseValidator } from "../base/validator"; | ||
@@ -8,3 +8,3 @@ import { RequiredValidator } from "../required/validator"; | ||
protected type: AnyType; | ||
constructor(validators: ReadonlyArray<BaseValidator<T>>); | ||
constructor(validators: ReadonlyArray<CoreValidator<T>>); | ||
required(): RequiredValidator<T, this>; | ||
@@ -11,0 +11,0 @@ protected toSchema(traverser: TreeTraverser): { |
import { Type } from "../types"; | ||
import { ValueValidator } from "../value/validator"; | ||
import { TreeTraverser } from "../core/validator"; | ||
import { BaseValidator } from "../base/validator"; | ||
import { CoreValidator, TreeTraverser } from "../core/validator"; | ||
import { Writeable } from "../functional"; | ||
@@ -13,3 +12,3 @@ export declare class ArrayValidator<T extends Array<any>> extends ValueValidator<T, ArrayValidator<T>> { | ||
private _contains; | ||
constructor(validator: BaseValidator<any>); | ||
constructor(validator: CoreValidator<any>); | ||
const<V extends readonly T[number][]>(value: V): ArrayValidator<Writeable<typeof value>>; | ||
@@ -25,3 +24,3 @@ enum<V extends readonly T[number][]>(...values: V[]): ArrayValidator<Writeable<(typeof values)[number]>>; | ||
maxItems(max: number): this; | ||
contains(validator: BaseValidator<any>): this; | ||
contains(validator: CoreValidator<any>): this; | ||
unique(unique?: boolean): this; | ||
@@ -28,0 +27,0 @@ protected toSchema(traverser: TreeTraverser): { |
@@ -6,3 +6,2 @@ import { AnyType } from "../types"; | ||
protected _parent: this | undefined; | ||
protected abstract type: AnyType; | ||
protected setupClone(clean: boolean, clone: U): this; | ||
@@ -12,8 +11,8 @@ } | ||
_annotations: AnnotationsHolder | undefined; | ||
abstract type: AnyType; | ||
abstract toSchema(traverser: TreeTraverser): any; | ||
abstract clone(clean?: boolean): this; | ||
_parent: this | undefined; | ||
abstract type: AnyType; | ||
abstract setupClone(clean: boolean, clone: any): this; | ||
} | ||
export declare function exposeBaseValidator<T extends BaseValidator<unknown>>(validator: T): InternalBaseValidator; |
import { AnnotationsHolder } from "../../annotations"; | ||
import { AnyType } from "../types"; | ||
export interface TreeTraverser { | ||
@@ -14,2 +15,3 @@ visit(validator: CoreValidator<unknown>): any; | ||
protected abstract clone(clean?: boolean): this; | ||
protected abstract type: AnyType; | ||
protected getJsonSchemaObject(traverser: TreeTraverser): { | ||
@@ -23,2 +25,3 @@ examples?: string[] | undefined; | ||
_annotations: AnnotationsHolder | undefined; | ||
abstract type: AnyType; | ||
abstract toSchema(traverser: TreeTraverser): any; | ||
@@ -25,0 +28,0 @@ abstract clone(clean?: boolean): this; |
import { AnyType } from "../types"; | ||
import { TreeTraverser } from "../core/validator"; | ||
import { CoreValidator, TreeTraverser } from "../core/validator"; | ||
import { BaseValidator } from "../base/validator"; | ||
@@ -8,5 +8,5 @@ import { TypeOf } from "../functional"; | ||
protected type: AnyType; | ||
protected _if: undefined | BaseValidator<unknown>; | ||
protected _then: undefined | BaseValidator<unknown>; | ||
protected _else: undefined | BaseValidator<unknown>; | ||
protected _if: undefined | CoreValidator<unknown>; | ||
protected _then: undefined | CoreValidator<unknown>; | ||
protected _else: undefined | CoreValidator<unknown>; | ||
protected constructor(); | ||
@@ -27,3 +27,3 @@ required(): RequiredValidator<T, this>; | ||
protected constructor(); | ||
else<U extends BaseValidator<unknown>>(validator: U): ElseValidator<T | TypeOf<U>>; | ||
else<U extends CoreValidator<unknown>>(validator: U): ElseValidator<T | TypeOf<U>>; | ||
protected clone(clean?: boolean): this; | ||
@@ -33,6 +33,6 @@ } | ||
protected type: AnyType; | ||
protected _if: BaseValidator<unknown>; | ||
constructor(validator: BaseValidator<T>); | ||
then<U extends BaseValidator<unknown>>(validator: U): ThenValidator<TypeOf<U>>; | ||
protected _if: CoreValidator<unknown>; | ||
constructor(validator: CoreValidator<T>); | ||
then<U extends CoreValidator<unknown>>(validator: U): ThenValidator<TypeOf<U>>; | ||
protected clone(clean?: boolean): this; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { BaseValidator } from "./base/validator"; | ||
import { CoreValidator } from "./core/validator"; | ||
import { RequiredValidator } from "./required/validator"; | ||
@@ -6,9 +6,9 @@ import { SubType } from "./types"; | ||
export declare type RequiredKeys<T extends { | ||
[key: string]: BaseValidator<unknown>; | ||
[key: string]: CoreValidator<unknown>; | ||
}> = SubType<T, RequiredValidator<any, any>>; | ||
export declare type OptionalKeys<T extends { | ||
[key: string]: BaseValidator<unknown>; | ||
[key: string]: CoreValidator<unknown>; | ||
}> = SubType<T, RequiredValidator<any, any>, true>; | ||
export declare type ExtractObject<T extends { | ||
[key: string]: BaseValidator<unknown>; | ||
[key: string]: CoreValidator<unknown>; | ||
}> = { | ||
@@ -15,0 +15,0 @@ [P in keyof RequiredKeys<T>]-?: TypeOf<T[P], true> & unknown; |
import { Type } from "../types"; | ||
import { TreeTraverser } from "../core/validator"; | ||
import { BaseValidator } from "../base/validator"; | ||
import { CoreValidator, TreeTraverser } from "../core/validator"; | ||
import { ValueValidator } from "../value/validator"; | ||
@@ -11,5 +10,5 @@ import { AdditionalProperties, TypeOf } from "../functional"; | ||
constructor(properties: { | ||
[key: string]: BaseValidator<unknown>; | ||
[key: string]: CoreValidator<unknown>; | ||
}); | ||
protected chainedAdditional(): BaseValidator<unknown> | boolean; | ||
protected chainedAdditional(): CoreValidator<unknown> | boolean; | ||
const<V extends T>(value: V): ObjectValidator<V>; | ||
@@ -28,3 +27,3 @@ enum<V extends T>(...values: (keyof V extends keyof T ? V[] : T[])): ObjectValidator<typeof values[number]>; | ||
additional(type: true): ObjectValidator<AdditionalProperties<T, unknown>>; | ||
additional<U extends BaseValidator<unknown>>(type: U): ObjectValidator<AdditionalProperties<T, TypeOf<U>>>; | ||
additional<U extends CoreValidator<unknown>>(type: U): ObjectValidator<AdditionalProperties<T, TypeOf<U>>>; | ||
protected toSchema(traverser: TreeTraverser): { | ||
@@ -31,0 +30,0 @@ additionalProperties?: any; |
import { AnyType } from "../types"; | ||
import { TreeTraverser } from "../core/validator"; | ||
import { CoreValidator, TreeTraverser } from "../core/validator"; | ||
import { BaseValidator } from "../base/validator"; | ||
@@ -8,3 +8,3 @@ import { RequiredValidator } from "../required/validator"; | ||
protected type: AnyType; | ||
constructor(validators: ReadonlyArray<BaseValidator<T>>); | ||
constructor(validators: ReadonlyArray<CoreValidator<T>>); | ||
required(): RequiredValidator<T, this>; | ||
@@ -11,0 +11,0 @@ protected toSchema(traverser: TreeTraverser): { |
import { CoreValidator } from "../core/validator"; | ||
import { RequiredValidator } from "../required/validator"; | ||
import { AnyType } from "../types"; | ||
export declare class RawValidator extends CoreValidator<unknown> { | ||
private jsonSchema; | ||
readonly fragment?: string | undefined; | ||
protected type: AnyType; | ||
constructor(jsonSchema: any, fragment?: string | undefined); | ||
toSchema(): any; | ||
required(): RequiredValidator<unknown, this>; | ||
protected clone(_clean?: boolean): this; | ||
} | ||
export declare function isRaw(validator: CoreValidator<unknown>): validator is RawValidator; | ||
export declare function isRaw(validator: CoreValidator<unknown>): boolean; | ||
export declare function getRaw(validator: CoreValidator<unknown>): RawValidator | undefined; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isRaw = exports.RawValidator = void 0; | ||
exports.getRaw = exports.isRaw = exports.RawValidator = void 0; | ||
const validator_1 = require("../core/validator"); | ||
const validator_2 = require("../required/validator"); | ||
class RawValidator extends validator_1.CoreValidator { | ||
@@ -10,2 +11,3 @@ constructor(jsonSchema, fragment) { | ||
this.fragment = fragment; | ||
this.type = 'raw'; | ||
} | ||
@@ -15,2 +17,5 @@ toSchema() { | ||
} | ||
required() { | ||
return new validator_2.RequiredValidator(this); | ||
} | ||
clone(_clean = false) { | ||
@@ -25,1 +30,6 @@ return new RawValidator(JSON.parse(JSON.stringify(this.jsonSchema))); | ||
exports.isRaw = isRaw; | ||
function getRaw(validator) { | ||
validator = validator_2.extractRequiredValidator(validator); | ||
return isRaw(validator) ? validator : undefined; | ||
} | ||
exports.getRaw = getRaw; |
import { AnyType } from "../types"; | ||
import { TreeTraverser } from "../core/validator"; | ||
import { CoreValidator, TreeTraverser } from "../core/validator"; | ||
import { BaseValidator } from "../base/validator"; | ||
export declare class RequiredValidator<T, U extends BaseValidator<T>> extends BaseValidator<T, RequiredValidator<T, U>> { | ||
private validator; | ||
export declare class RequiredValidator<T, U extends CoreValidator<T>> extends BaseValidator<T, RequiredValidator<T, U>> { | ||
protected validator: U; | ||
constructor(validator: U); | ||
@@ -11,2 +11,6 @@ protected get type(): AnyType; | ||
} | ||
export declare function isRequired(validator: BaseValidator<unknown>): boolean; | ||
export declare abstract class InternalRequiredValidator extends RequiredValidator<unknown, CoreValidator<unknown>> { | ||
abstract validator: CoreValidator<unknown>; | ||
} | ||
export declare function isRequired(validator: CoreValidator<unknown>): boolean; | ||
export declare function extractRequiredValidator(validator: CoreValidator<unknown>): CoreValidator<unknown>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isRequired = exports.RequiredValidator = void 0; | ||
exports.extractRequiredValidator = exports.isRequired = exports.InternalRequiredValidator = exports.RequiredValidator = void 0; | ||
const validator_1 = require("../base/validator"); | ||
@@ -26,2 +26,5 @@ const validation_1 = require("../../validation"); | ||
exports.RequiredValidator = RequiredValidator; | ||
class InternalRequiredValidator extends RequiredValidator { | ||
} | ||
exports.InternalRequiredValidator = InternalRequiredValidator; | ||
function isRequired(validator) { | ||
@@ -31,1 +34,7 @@ return validator instanceof RequiredValidator; | ||
exports.isRequired = isRequired; | ||
function extractRequiredValidator(validator) { | ||
return validator instanceof RequiredValidator | ||
? validator.validator | ||
: validator; | ||
} | ||
exports.extractRequiredValidator = extractRequiredValidator; |
import { Type } from "../types"; | ||
import { ValueValidator } from "../value/validator"; | ||
import { TreeTraverser } from "../core/validator"; | ||
import { BaseValidator } from "../base/validator"; | ||
import { CoreValidator, TreeTraverser } from "../core/validator"; | ||
import { AnyValidator } from "../any/validator"; | ||
@@ -9,4 +8,4 @@ import { ArrayOf, ArrayOfWithRest } from "../array-types"; | ||
import type { If, Is, And, Extends, GreaterThan, LengthOf } from "meta-types"; | ||
export declare class TupleValidator<T extends any[], U extends BaseValidator<unknown>[], N extends number, // First optional index | ||
A extends false | BaseValidator<unknown, any>> extends ValueValidator<T, TupleValidator<T, U, N, A>> { | ||
export declare class TupleValidator<T extends any[], U extends CoreValidator<unknown>[], N extends number, // First optional index | ||
A extends false | CoreValidator<unknown>> extends ValueValidator<T, TupleValidator<T, U, N, A>> { | ||
private validators; | ||
@@ -20,3 +19,3 @@ protected type: Type; | ||
private _additional; | ||
constructor(validators: ReadonlyArray<BaseValidator<any>>); | ||
constructor(validators: ReadonlyArray<CoreValidator<any>>); | ||
const<V extends readonly T[number][]>(value: V): TupleValidator<Writeable<typeof value>, U, N, A>; | ||
@@ -42,4 +41,4 @@ enum<V extends readonly T[number][]>(...values: V[]): TupleValidator<Writeable<(typeof values)[number]>, U, N, A>; | ||
additional<A extends boolean>(type: A): typeof type extends false ? this : TupleValidator<ArrayOfWithRest<U, any, N>, U, N, AnyValidator>; | ||
additional<B extends BaseValidator<unknown>>(type: B): TupleValidator<ArrayOfWithRest<U, TypeOf<B>, N>, U, N, B>; | ||
contains(validator: BaseValidator<any>): this; | ||
additional<B extends CoreValidator<unknown>>(type: B): TupleValidator<ArrayOfWithRest<U, TypeOf<B>, N>, U, N, B>; | ||
contains(validator: CoreValidator<any>): this; | ||
unique(unique?: boolean): this; | ||
@@ -46,0 +45,0 @@ protected toSchema(traverser: TreeTraverser): { |
export declare type Type = "string" | "number" | "integer" | "object" | "array" | "boolean" | "null"; | ||
export declare type AnyType = Type | "any" | "any-of" | "all-of" | "if" | "recursive"; | ||
export declare type AnyType = Type | "raw" | "any" | "any-of" | "all-of" | "if" | "recursive"; | ||
export declare type FilterProperties<T, Cond> = { | ||
@@ -4,0 +4,0 @@ [K in keyof T]: T[K] extends Cond ? K : never; |
{ | ||
"name": "suretype", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Typesafe JSON (Schema) validator with magic powers 🧙♂️", | ||
@@ -5,0 +5,0 @@ "author": "Gustaf Räntilä", |
@@ -275,7 +275,52 @@ [![npm version][npm-image]][npm-url] | ||
const { schema: jsonSchema } = extractJsonSchema( [ userSchema, messageSchema ] ); | ||
const { schema: jsonSchema, lookup } = | ||
extractJsonSchema( [ userSchema, messageSchema ], { /* opts... */ } ); | ||
``` | ||
The `jsonSchema` *object* (which can be `JSON.stringify`'d) will be something like: | ||
An optional second argument can be provided on the form: | ||
```ts | ||
interface ExtractJsonSchemaOptions { | ||
refMethod?: ExportRefMethod; | ||
onTopLevelNameConflict?: OnTopLevelNameConflict; | ||
onNonSuretypeValidator?: OnNonSuretypeValidator; | ||
} | ||
``` | ||
The `ExportRefMethod` type is a string union defined as: | ||
```ts | ||
| 'no-refs' // Don't ref anything. Inline all types to monolith types. | ||
| 'provided' // Reference types that are explicitly provided. | ||
| 'ref-all' // Ref all provided types and those with names, suretype()'d. | ||
``` | ||
The `OnTopLevelNameConflict` type is a string union defined as: | ||
```ts | ||
| 'error' // Fail the operation | ||
| 'rename' // Rename the validators to a unique name | ||
``` | ||
The `OnNonSuretypeValidator` type is a string union defined as: | ||
```ts | ||
| 'error' // Fail the operation | ||
| 'ignore' // Ignore, don't export | ||
| 'create-name' // Create a name 'Unknown' | ||
| 'lookup' // Provide in lookup table | ||
``` | ||
If `lookup` is specified, it allows unnamed validators. They won't exist in the resulting schema, but in a lookup table next to it. This lookup table will always exist, using this setting will simply allow unnamed validators. | ||
The result is an object on the form: | ||
```ts | ||
interface ExtractedJsonSchema { | ||
schema: SchemaWithDefinitions; // Contains a 'definitions' property | ||
lookup: Map< CoreValidator< unknown >, any >; | ||
} | ||
``` | ||
The `lookup` is useful to lookup the json schema for a certain validator object reference, especially unnamed ones which are not included in the schema. | ||
In the example above, the `jsonSchema` *object* (which can be `JSON.stringify`'d) will be something like: | ||
<details style="padding-left: 32px;border-left: 4px solid gray;"> | ||
@@ -282,0 +327,0 @@ <summary>JSON Schema</summary> |
181205
4570
404