@tldraw/validate
Advanced tools
Comparing version 2.0.0-canary.2372478556f4 to 2.0.0-canary.271d0088e996
@@ -21,8 +21,8 @@ /** | ||
*/ | ||
declare function arrayOf<T>(itemValidator: Validator<T>): ArrayOfValidator<T>; | ||
declare function arrayOf<T>(itemValidator: Validatable<T>): ArrayOfValidator<T>; | ||
/** @public */ | ||
declare class ArrayOfValidator<T> extends Validator<T[]> { | ||
readonly itemValidator: Validator<T>; | ||
constructor(itemValidator: Validator<T>); | ||
readonly itemValidator: Validatable<T>; | ||
constructor(itemValidator: Validatable<T>); | ||
nonEmpty(): Validator<T[]>; | ||
@@ -46,10 +46,2 @@ lengthGreaterThan1(): Validator<T[]>; | ||
/** @public */ | ||
declare const boxModel: ObjectValidator<{ | ||
x: number; | ||
y: number; | ||
w: number; | ||
h: number; | ||
}>; | ||
/** | ||
@@ -60,9 +52,9 @@ * Validation that an option is a dict with particular keys and values. | ||
*/ | ||
declare function dict<Key extends string, Value>(keyValidator: Validator<Key>, valueValidator: Validator<Value>): DictValidator<Key, Value>; | ||
declare function dict<Key extends string, Value>(keyValidator: Validatable<Key>, valueValidator: Validatable<Value>): DictValidator<Key, Value>; | ||
/** @public */ | ||
declare class DictValidator<Key extends string, Value> extends Validator<Record<Key, Value>> { | ||
readonly keyValidator: Validator<Key>; | ||
readonly valueValidator: Validator<Value>; | ||
constructor(keyValidator: Validator<Key>, valueValidator: Validator<Value>); | ||
readonly keyValidator: Validatable<Key>; | ||
readonly valueValidator: Validatable<Value>; | ||
constructor(keyValidator: Validatable<Key>, valueValidator: Validatable<Value>); | ||
} | ||
@@ -90,2 +82,5 @@ | ||
/** @public */ | ||
declare function literalEnum<const Values extends readonly unknown[]>(...values: Values): Validator<Values[number]>; | ||
/** | ||
@@ -99,3 +94,3 @@ * A named object with an ID. Errors will be reported as being part of the object with the given | ||
readonly id: string; | ||
}>(name: string, validator: Validator<T>): Validator<T>; | ||
}>(name: string, validator: Validatable<T>): Validator<T>; | ||
@@ -116,2 +111,5 @@ /** | ||
/** @public */ | ||
declare function nullable<T>(validator: Validatable<T>): Validator<null | T>; | ||
/** | ||
@@ -130,3 +128,3 @@ * Validates that a value is a finite non-NaN number. | ||
declare function object<Shape extends object>(config: { | ||
readonly [K in keyof Shape]: Validator<Shape[K]>; | ||
readonly [K in keyof Shape]: Validatable<Shape[K]>; | ||
}): ObjectValidator<Shape>; | ||
@@ -137,7 +135,7 @@ | ||
readonly config: { | ||
readonly [K in keyof Shape]: Validator<Shape[K]>; | ||
readonly [K in keyof Shape]: Validatable<Shape[K]>; | ||
}; | ||
private readonly shouldAllowUnknownProperties; | ||
constructor(config: { | ||
readonly [K in keyof Shape]: Validator<Shape[K]>; | ||
readonly [K in keyof Shape]: Validatable<Shape[K]>; | ||
}, shouldAllowUnknownProperties?: boolean); | ||
@@ -160,3 +158,3 @@ allowUnknownProperties(): ObjectValidator<Shape>; | ||
extend<Extension extends Record<string, unknown>>(extension: { | ||
readonly [K in keyof Extension]: Validator<Extension[K]>; | ||
readonly [K in keyof Extension]: Validatable<Extension[K]>; | ||
}): ObjectValidator<Shape & Extension>; | ||
@@ -166,7 +164,3 @@ } | ||
/** @public */ | ||
declare const point: ObjectValidator<{ | ||
x: number; | ||
y: number; | ||
z: number | undefined; | ||
}>; | ||
declare function optional<T>(validator: Validatable<T>): Validator<T | undefined>; | ||
@@ -206,3 +200,7 @@ /** | ||
setEnum, | ||
optional, | ||
nullable, | ||
literalEnum, | ||
ValidatorFn, | ||
Validatable, | ||
ValidationError, | ||
@@ -227,5 +225,3 @@ TypeOf, | ||
array, | ||
unknownObject, | ||
point, | ||
boxModel | ||
unknownObject | ||
} | ||
@@ -236,3 +232,3 @@ } | ||
/** @public */ | ||
declare type TypeOf<V extends Validator<unknown>> = V extends Validator<infer T> ? T : never; | ||
declare type TypeOf<V extends Validatable<unknown>> = V extends Validatable<infer T> ? T : never; | ||
@@ -265,3 +261,3 @@ /** | ||
declare type UnionValidatorConfig<Key extends string, Config> = { | ||
readonly [Variant in keyof Config]: Validator<any> & { | ||
readonly [Variant in keyof Config]: Validatable<any> & { | ||
validate: (input: any) => { | ||
@@ -285,2 +281,7 @@ readonly [K in Key]: Variant; | ||
/** @public */ | ||
declare type Validatable<T> = { | ||
validate: (value: unknown) => T; | ||
}; | ||
/** @public */ | ||
declare class ValidationError extends Error { | ||
@@ -294,3 +295,3 @@ readonly rawMessage: string; | ||
/** @public */ | ||
declare class Validator<T> { | ||
declare class Validator<T> implements Validatable<T> { | ||
readonly validationFn: ValidatorFn<T>; | ||
@@ -297,0 +298,0 @@ constructor(validationFn: ValidatorFn<T>); |
@@ -32,12 +32,13 @@ "use strict"; | ||
boolean: () => boolean, | ||
boxModel: () => boxModel, | ||
dict: () => dict, | ||
integer: () => integer, | ||
literal: () => literal, | ||
literalEnum: () => literalEnum, | ||
model: () => model, | ||
nonZeroInteger: () => nonZeroInteger, | ||
nonZeroNumber: () => nonZeroNumber, | ||
nullable: () => nullable, | ||
number: () => number, | ||
object: () => object, | ||
point: () => point, | ||
optional: () => optional, | ||
positiveInteger: () => positiveInteger, | ||
@@ -138,7 +139,3 @@ positiveNumber: () => positiveNumber, | ||
nullable() { | ||
return new Validator((value) => { | ||
if (value === null) | ||
return null; | ||
return this.validate(value); | ||
}); | ||
return nullable(this); | ||
} | ||
@@ -150,7 +147,3 @@ /** | ||
optional() { | ||
return new Validator((value) => { | ||
if (value === void 0) | ||
return void 0; | ||
return this.validate(value); | ||
}); | ||
return optional(this); | ||
} | ||
@@ -388,13 +381,19 @@ /** | ||
} | ||
const point = object({ | ||
x: number, | ||
y: number, | ||
z: number.optional() | ||
}); | ||
const boxModel = object({ | ||
x: number, | ||
y: number, | ||
w: number, | ||
h: number | ||
}); | ||
function optional(validator) { | ||
return new Validator((value) => { | ||
if (value === void 0) | ||
return void 0; | ||
return validator.validate(value); | ||
}); | ||
} | ||
function nullable(validator) { | ||
return new Validator((value) => { | ||
if (value === null) | ||
return null; | ||
return validator.validate(value); | ||
}); | ||
} | ||
function literalEnum(...values) { | ||
return setEnum(new Set(values)); | ||
} | ||
//# sourceMappingURL=validation.js.map |
{ | ||
"name": "@tldraw/validate", | ||
"description": "A runtime validation library by tldraw.", | ||
"version": "2.0.0-canary.2372478556f4", | ||
"version": "2.0.0-canary.271d0088e996", | ||
"packageManager": "yarn@3.5.0", | ||
@@ -45,3 +45,3 @@ "author": { | ||
"dependencies": { | ||
"@tldraw/utils": "2.0.0-canary.2372478556f4" | ||
"@tldraw/utils": "2.0.0-canary.271d0088e996" | ||
}, | ||
@@ -48,0 +48,0 @@ "jest": { |
@@ -6,2 +6,5 @@ import { exhaustiveSwitchError, getOwnProperty, hasOwnProperty } from '@tldraw/utils' | ||
/** @public */ | ||
export type Validatable<T> = { validate: (value: unknown) => T } | ||
function formatPath(path: ReadonlyArray<number | string>): string | null { | ||
@@ -81,6 +84,6 @@ if (!path.length) { | ||
/** @public */ | ||
export type TypeOf<V extends Validator<unknown>> = V extends Validator<infer T> ? T : never | ||
export type TypeOf<V extends Validatable<unknown>> = V extends Validatable<infer T> ? T : never | ||
/** @public */ | ||
export class Validator<T> { | ||
export class Validator<T> implements Validatable<T> { | ||
constructor(readonly validationFn: ValidatorFn<T>) {} | ||
@@ -105,6 +108,3 @@ | ||
nullable(): Validator<T | null> { | ||
return new Validator((value) => { | ||
if (value === null) return null | ||
return this.validate(value) | ||
}) | ||
return nullable(this) | ||
} | ||
@@ -117,6 +117,3 @@ | ||
optional(): Validator<T | undefined> { | ||
return new Validator((value) => { | ||
if (value === undefined) return undefined | ||
return this.validate(value) | ||
}) | ||
return optional(this) | ||
} | ||
@@ -166,3 +163,3 @@ | ||
export class ArrayOfValidator<T> extends Validator<T[]> { | ||
constructor(readonly itemValidator: Validator<T>) { | ||
constructor(readonly itemValidator: Validatable<T>) { | ||
super((value) => { | ||
@@ -198,3 +195,3 @@ const arr = array.validate(value) | ||
public readonly config: { | ||
readonly [K in keyof Shape]: Validator<Shape[K]> | ||
readonly [K in keyof Shape]: Validatable<Shape[K]> | ||
}, | ||
@@ -245,3 +242,3 @@ private readonly shouldAllowUnknownProperties = false | ||
extend<Extension extends Record<string, unknown>>(extension: { | ||
readonly [K in keyof Extension]: Validator<Extension[K]> | ||
readonly [K in keyof Extension]: Validatable<Extension[K]> | ||
}): ObjectValidator<Shape & Extension> { | ||
@@ -256,3 +253,3 @@ return new ObjectValidator({ ...this.config, ...extension }) as ObjectValidator< | ||
type UnionValidatorConfig<Key extends string, Config> = { | ||
readonly [Variant in keyof Config]: Validator<any> & { | ||
readonly [Variant in keyof Config]: Validatable<any> & { | ||
validate: (input: any) => { readonly [K in Key]: Variant } | ||
@@ -303,4 +300,4 @@ } | ||
constructor( | ||
public readonly keyValidator: Validator<Key>, | ||
public readonly valueValidator: Validator<Value> | ||
public readonly keyValidator: Validatable<Key>, | ||
public readonly valueValidator: Validatable<Value> | ||
) { | ||
@@ -458,3 +455,3 @@ super((object) => { | ||
*/ | ||
export function arrayOf<T>(itemValidator: Validator<T>): ArrayOfValidator<T> { | ||
export function arrayOf<T>(itemValidator: Validatable<T>): ArrayOfValidator<T> { | ||
return new ArrayOfValidator(itemValidator) | ||
@@ -477,3 +474,3 @@ } | ||
export function object<Shape extends object>(config: { | ||
readonly [K in keyof Shape]: Validator<Shape[K]> | ||
readonly [K in keyof Shape]: Validatable<Shape[K]> | ||
}): ObjectValidator<Shape> { | ||
@@ -489,4 +486,4 @@ return new ObjectValidator(config) | ||
export function dict<Key extends string, Value>( | ||
keyValidator: Validator<Key>, | ||
valueValidator: Validator<Value> | ||
keyValidator: Validatable<Key>, | ||
valueValidator: Validatable<Value> | ||
): DictValidator<Key, Value> { | ||
@@ -532,3 +529,3 @@ return new DictValidator(keyValidator, valueValidator) | ||
name: string, | ||
validator: Validator<T> | ||
validator: Validatable<T> | ||
): Validator<T> { | ||
@@ -557,14 +554,22 @@ return new Validator((value) => { | ||
/** @public */ | ||
export const point = object({ | ||
x: number, | ||
y: number, | ||
z: number.optional(), | ||
}) | ||
export function optional<T>(validator: Validatable<T>): Validator<T | undefined> { | ||
return new Validator((value) => { | ||
if (value === undefined) return undefined | ||
return validator.validate(value) | ||
}) | ||
} | ||
/** @public */ | ||
export const boxModel = object({ | ||
x: number, | ||
y: number, | ||
w: number, | ||
h: number, | ||
}) | ||
export function nullable<T>(validator: Validatable<T>): Validator<T | null> { | ||
return new Validator((value) => { | ||
if (value === null) return null | ||
return validator.validate(value) | ||
}) | ||
} | ||
/** @public */ | ||
export function literalEnum<const Values extends readonly unknown[]>( | ||
...values: Values | ||
): Validator<Values[number]> { | ||
return setEnum(new Set(values)) | ||
} |
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
123620
+ Added@tldraw/utils@2.0.0-canary.271d0088e996(transitive)
- Removed@tldraw/utils@2.0.0-canary.2372478556f4(transitive)