@tldraw/validate
Advanced tools
Comparing version 2.0.0-canary.d9548556cbea to 2.0.0-canary.e3b00e7c11c3
@@ -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[]>; | ||
@@ -59,9 +59,9 @@ lengthGreaterThan1(): Validator<T[]>; | ||
*/ | ||
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>); | ||
} | ||
@@ -97,3 +97,3 @@ | ||
readonly id: string; | ||
}>(name: string, validator: Validator<T>): Validator<T>; | ||
}>(name: string, validator: Validatable<T>): Validator<T>; | ||
@@ -127,3 +127,3 @@ /** | ||
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>; | ||
@@ -134,7 +134,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); | ||
@@ -157,3 +157,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>; | ||
@@ -203,2 +203,3 @@ } | ||
ValidatorFn, | ||
Validatable, | ||
ValidationError, | ||
@@ -231,3 +232,3 @@ TypeOf, | ||
/** @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; | ||
@@ -260,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) => { | ||
@@ -280,2 +281,7 @@ readonly [K in Key]: Variant; | ||
/** @public */ | ||
declare type Validatable<T> = { | ||
validate: (value: unknown) => T; | ||
}; | ||
/** @public */ | ||
declare class ValidationError extends Error { | ||
@@ -289,3 +295,3 @@ readonly rawMessage: string; | ||
/** @public */ | ||
declare class Validator<T> { | ||
declare class Validator<T> implements Validatable<T> { | ||
readonly validationFn: ValidatorFn<T>; | ||
@@ -292,0 +298,0 @@ constructor(validationFn: ValidatorFn<T>); |
{ | ||
"name": "@tldraw/validate", | ||
"description": "A runtime validation library by tldraw.", | ||
"version": "2.0.0-canary.d9548556cbea", | ||
"version": "2.0.0-canary.e3b00e7c11c3", | ||
"packageManager": "yarn@3.5.0", | ||
@@ -45,3 +45,3 @@ "author": { | ||
"dependencies": { | ||
"@tldraw/utils": "2.0.0-canary.d9548556cbea" | ||
"@tldraw/utils": "2.0.0-canary.e3b00e7c11c3" | ||
}, | ||
@@ -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>) {} | ||
@@ -164,3 +167,3 @@ | ||
export class ArrayOfValidator<T> extends Validator<T[]> { | ||
constructor(readonly itemValidator: Validator<T>) { | ||
constructor(readonly itemValidator: Validatable<T>) { | ||
super((value) => { | ||
@@ -196,3 +199,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]> | ||
}, | ||
@@ -243,3 +246,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> { | ||
@@ -254,3 +257,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 } | ||
@@ -301,4 +304,4 @@ } | ||
constructor( | ||
public readonly keyValidator: Validator<Key>, | ||
public readonly valueValidator: Validator<Value> | ||
public readonly keyValidator: Validatable<Key>, | ||
public readonly valueValidator: Validatable<Value> | ||
) { | ||
@@ -456,3 +459,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) | ||
@@ -475,3 +478,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> { | ||
@@ -487,4 +490,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> { | ||
@@ -530,3 +533,3 @@ return new DictValidator(keyValidator, valueValidator) | ||
name: string, | ||
validator: Validator<T> | ||
validator: Validatable<T> | ||
): Validator<T> { | ||
@@ -533,0 +536,0 @@ return new Validator((value) => { |
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
122671
1732
+ Added@tldraw/utils@2.0.0-canary.e3b00e7c11c3(transitive)
- Removed@tldraw/utils@2.0.0-canary.d9548556cbea(transitive)