jet-schema
Advanced tools
Comparing version 1.3.0 to 1.4.0
import jetSchema from './jetSchema'; | ||
export type { TSchemaFnObjArg as TJetSchema, PublicInferType as inferType, } from './jetSchema'; | ||
export declare const schema: <T, U extends Required<{ [K in keyof T]: T[K] extends string | number ? import("./util").TEnum | import("./util").IValidatorFnOrObj<T[K]> : T[K] extends Date ? DateConstructor | import("./util").IValidatorFnOrObj<T[K]> : ((NonNullable<T[K]> extends infer T_1 ? { [K_1 in keyof T_1]: NonNullable<T[K]>[K_1]; } : never) extends (string extends keyof NonNullable<T[K]> ? never : { | ||
export { IError, TErrArg } from './error-stuff'; | ||
export declare const schema: <T, U extends Required<{ [K in keyof T]: T[K] extends string | number ? import("./util").TEnum | (((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is T[K]) | import("./jetSchema").IValidatorObj<T[K]>) : T[K] extends Date ? DateConstructor | (((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is T[K]) | import("./jetSchema").IValidatorObj<T[K]>) : ((NonNullable<T[K]> extends infer T_1 ? { [K_1 in keyof T_1]: NonNullable<T[K]>[K_1]; } : never) extends (string extends keyof NonNullable<T[K]> ? never : { | ||
[key: string]: string | number | boolean | (string extends keyof NonNullable<T[K]> ? never : any); | ||
}) ? true : false) extends true ? import("./jetSchema").ISchema<T[K]> : import("./util").IValidatorFnOrObj<T[K]>; }> = Required<{ [K in keyof T]: T[K] extends string | number ? import("./util").TEnum | import("./util").IValidatorFnOrObj<T[K]> : T[K] extends Date ? DateConstructor | import("./util").IValidatorFnOrObj<T[K]> : ((NonNullable<T[K]> extends infer T_1 ? { [K_1 in keyof T_1]: NonNullable<T[K]>[K_1]; } : never) extends (string extends keyof NonNullable<T[K]> ? never : { | ||
}) ? true : false) extends true ? import("./jetSchema").ISchema<T[K]> : ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is T[K]) | import("./jetSchema").IValidatorObj<T[K]>; }> = Required<{ [K in keyof T]: T[K] extends string | number ? import("./util").TEnum | (((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is T[K]) | import("./jetSchema").IValidatorObj<T[K]>) : T[K] extends Date ? DateConstructor | (((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is T[K]) | import("./jetSchema").IValidatorObj<T[K]>) : ((NonNullable<T[K]> extends infer T_1 ? { [K_1 in keyof T_1]: NonNullable<T[K]>[K_1]; } : never) extends (string extends keyof NonNullable<T[K]> ? never : { | ||
[key: string]: string | number | boolean | (string extends keyof NonNullable<T[K]> ? never : any); | ||
}) ? true : false) extends true ? import("./jetSchema").ISchema<T[K]> : import("./util").IValidatorFnOrObj<T[K]>; }>, R extends unknown extends T ? import("./jetSchema").IOptNul | import("./jetSchema").IOptNotNul | import("./jetSchema").INotOptButNul | import("./jetSchema").INotOptOrNul | import("./jetSchema").INullish : undefined extends T ? null extends T ? import("./jetSchema").IOptNul | import("./jetSchema").INullish : import("./jetSchema").IOptNotNul : null extends T ? import("./jetSchema").INotOptButNul : import("./jetSchema").INotOptOrNul = unknown extends T ? import("./jetSchema").IOptNul | import("./jetSchema").IOptNotNul | import("./jetSchema").INotOptButNul | import("./jetSchema").INotOptOrNul | import("./jetSchema").INullish : undefined extends T ? null extends T ? import("./jetSchema").IOptNul | import("./jetSchema").INullish : import("./jetSchema").IOptNotNul : null extends T ? import("./jetSchema").INotOptButNul : import("./jetSchema").INotOptOrNul>(schemaFnObjArg: U, ...options: unknown extends T ? [] | [R] : undefined extends T ? [R] : null extends T ? [R] : [] | [R]) => unknown extends T ? import("./jetSchema").ISchema<"nullish" extends keyof R ? NonNullable<Exclude<{ [K_3 in keyof { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }]-?: (x: undefined extends { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3] ? Partial<Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>> : Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>) => void; }[keyof U] extends (x: infer I) => void ? I extends infer U_1 ? { [K_4 in keyof U_1]: U_1[K_4]; } : never : never, undefined>> | null | undefined : ("optional" extends keyof R ? R[keyof R & "optional"] : false) extends infer T_2 ? T_2 extends ("optional" extends keyof R ? R[keyof R & "optional"] : false) ? T_2 extends true ? (("nullable" extends keyof R ? R[keyof R & "nullable"] : false) extends infer T_3 ? T_3 extends ("nullable" extends keyof R ? R[keyof R & "nullable"] : false) ? T_3 extends true ? NonNullable<Exclude<{ [K_3 in keyof { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }]-?: (x: undefined extends { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3] ? Partial<Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>> : Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>) => void; }[keyof U] extends (x: infer I) => void ? I extends infer U_1 ? { [K_4 in keyof U_1]: U_1[K_4]; } : never : never, undefined>> | null : NonNullable<Exclude<{ [K_3 in keyof { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }]-?: (x: undefined extends { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3] ? Partial<Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>> : Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>) => void; }[keyof U] extends (x: infer I) => void ? I extends infer U_1 ? { [K_4 in keyof U_1]: U_1[K_4]; } : never : never, undefined>> : never : never) | undefined : ("nullable" extends keyof R ? R[keyof R & "nullable"] : false) extends infer T_3 ? T_3 extends ("nullable" extends keyof R ? R[keyof R & "nullable"] : false) ? T_3 extends true ? NonNullable<Exclude<{ [K_3 in keyof { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }]-?: (x: undefined extends { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3] ? Partial<Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>> : Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>) => void; }[keyof U] extends (x: infer I) => void ? I extends infer U_1 ? { [K_4 in keyof U_1]: U_1[K_4]; } : never : never, undefined>> | null : NonNullable<Exclude<{ [K_3 in keyof { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }]-?: (x: undefined extends { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3] ? Partial<Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>> : Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends import("./util").IValidatorFnOrObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>) => void; }[keyof U] extends (x: infer I) => void ? I extends infer U_1 ? { [K_4 in keyof U_1]: U_1[K_4]; } : never : never, undefined>> : never : never : never : never> : import("./jetSchema").ISchema<T>; | ||
}) ? true : false) extends true ? import("./jetSchema").ISchema<T[K]> : ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is T[K]) | import("./jetSchema").IValidatorObj<T[K]>; }>, R extends unknown extends T ? import("./jetSchema").IOptNul | import("./jetSchema").IOptNotNul | import("./jetSchema").INotOptButNul | import("./jetSchema").INotOptOrNul | import("./jetSchema").INullish : undefined extends T ? null extends T ? import("./jetSchema").IOptNul | import("./jetSchema").INullish : import("./jetSchema").IOptNotNul : null extends T ? import("./jetSchema").INotOptButNul : import("./jetSchema").INotOptOrNul = unknown extends T ? import("./jetSchema").IOptNul | import("./jetSchema").IOptNotNul | import("./jetSchema").INotOptButNul | import("./jetSchema").INotOptOrNul | import("./jetSchema").INullish : undefined extends T ? null extends T ? import("./jetSchema").IOptNul | import("./jetSchema").INullish : import("./jetSchema").IOptNotNul : null extends T ? import("./jetSchema").INotOptButNul : import("./jetSchema").INotOptOrNul>(schemaFnObjArg: U, ...options: unknown extends T ? [] | [R] : undefined extends T ? [R] : null extends T ? [R] : [] | [R]) => unknown extends T ? import("./jetSchema").ISchema<"nullish" extends keyof R ? NonNullable<Exclude<{ [K_3 in keyof { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }]-?: (x: undefined extends { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3] ? Partial<Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>> : Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>) => void; }[keyof U] extends (x: infer I) => void ? I extends infer U_1 ? { [K_4 in keyof U_1]: U_1[K_4]; } : never : never, undefined>> | null | undefined : ("optional" extends keyof R ? R[keyof R & "optional"] : false) extends infer T_2 ? T_2 extends ("optional" extends keyof R ? R[keyof R & "optional"] : false) ? T_2 extends true ? (("nullable" extends keyof R ? R[keyof R & "nullable"] : false) extends infer T_3 ? T_3 extends ("nullable" extends keyof R ? R[keyof R & "nullable"] : false) ? T_3 extends true ? NonNullable<Exclude<{ [K_3 in keyof { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }]-?: (x: undefined extends { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3] ? Partial<Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>> : Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>) => void; }[keyof U] extends (x: infer I) => void ? I extends infer U_1 ? { [K_4 in keyof U_1]: U_1[K_4]; } : never : never, undefined>> | null : NonNullable<Exclude<{ [K_3 in keyof { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }]-?: (x: undefined extends { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3] ? Partial<Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>> : Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>) => void; }[keyof U] extends (x: infer I) => void ? I extends infer U_1 ? { [K_4 in keyof U_1]: U_1[K_4]; } : never : never, undefined>> : never : never) | undefined : ("nullable" extends keyof R ? R[keyof R & "nullable"] : false) extends infer T_3 ? T_3 extends ("nullable" extends keyof R ? R[keyof R & "nullable"] : false) ? T_3 extends true ? NonNullable<Exclude<{ [K_3 in keyof { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }]-?: (x: undefined extends { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3] ? Partial<Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>> : Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>) => void; }[keyof U] extends (x: infer I) => void ? I extends infer U_1 ? { [K_4 in keyof U_1]: U_1[K_4]; } : never : never, undefined>> | null : NonNullable<Exclude<{ [K_3 in keyof { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }]-?: (x: undefined extends { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3] ? Partial<Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>> : Record<K_3, { [K_2 in keyof U]: U[K_2] extends DateConstructor ? Date : U[K_2] extends ((arg: unknown, parentObj?: { | ||
[x: string]: unknown; | ||
[x: number]: unknown; | ||
[x: symbol]: unknown; | ||
}, key?: string) => arg is infer X) | import("./jetSchema").IValidatorObj<infer X> ? X : U[K_2] extends import("./jetSchema").ISchema<infer X_1> ? X_1 : U[K_2] extends unknown[] ? never : U[K_2] extends import("./util").TEnum ? U[K_2][keyof U[K_2]] : never; }[K_3]>) => void; }[keyof U] extends (x: infer I) => void ? I extends infer U_1 ? { [K_4 in keyof U_1]: U_1[K_4]; } : never : never, undefined>> : never : never : never : never> : import("./jetSchema").ISchema<T>; | ||
export default jetSchema; |
@@ -5,5 +5,5 @@ 'use strict'; | ||
function t(t){return void 0===t}function e(t){return "number"==typeof t}function n(t){return "string"==typeof t}function r(t){return !!t&&"object"==typeof t}function o(t){if(!function(t){return "object"==typeof t&&!Array.isArray(t)}(t))throw Error('"getEnumKeys" must receive a non-array object');let n=Object.keys(t).reduce(((e,n)=>(e.includes(n)||e.push(t[n]),e)),[]);return e(t[n[0]])&&(n=n.map((e=>t[e]))),[n[0],t=>n.some((e=>e===t))]}function i(t){if(!r(t)||Array.isArray(t))return !1;const o=t,i=Object.keys(o),l=Math.floor(i.length/2);if(!e(o[i[l]]))return function(t,e){if(r(t))for(const n of Object.entries(t))if(!e(n[0],n[1]))return !1;return !0}(t,((t,e)=>n(t)&&n(e)));if(i.length%2!=0)return !1;for(let t=0;t<l;t++){const e=i[t],n=o[e],r=i[t+l],f=o[r];if(n!==r||e!==String(f))return !1}return !0}const l=t=>t instanceof Date&&!isNaN(new Date(t).getTime());function f(t,e){return (n,r)=>(void 0!==n&&(n=t(n)),r?.(n),e(n))}const s='"init" must be true if schema is not optional.',u="This validation failed when setting up defaults.",a="Setup error: validator must be a function, enum, schema, or Date constructor.",c="schema.test failed: value was undefined but not optional.",d="schema.test failed: value was null but not nullable.",p="schema.parse failed: value must be an object.",h='Failed in the "new" function',m='Failed in the "test" function',v='Failed in the "parse" function',y="Argument had a property which was not in schema, which is not allowed in strict mode";function w(e){const n=function(t){const e=new Map;for(const n of t){const{vf:t,...r}=n;e.set(t,r);}return e}(e?.globals??[]),w=e?.cloneFn?e.cloneFn:g,F=e?.onError?function(t){return (e,n,r,o)=>{const i=D(e,n,r,o);return t(e,n,i)}}(e.onError):j;return (e,...g)=>{const[j]=g,D=function(e){let n={init:!0,safety:"filter"};t(e?.init)||(n.init=e.init);e?.id&&(n.id=e?.id);e?.safety&&(n.safety=e.safety);n=e?.nullish?{...n,optional:!0,nullable:!0}:{...n,optional:!!e?.optional,nullable:!!e?.nullable};return n}(j);let H=F;D.id&&(H=O(H,D.id)),D.optional||!1!==D.init&&!t(D.init)||H("","",s);const N=function(e,n,r,s,c=""){const d={},p={};for(const h in e){const m=e[h];if(d[h]={vf:t=>!1,default:()=>!1},m===Date)d[h]={vf:f((t=>new Date(t)),l),default:()=>new Date};else if("function"==typeof m||E(m)){let e,o,i=!1,l=!1;if(E(m)){const t=m;if(e=t.vf,"default"in t&&(o=t.default,i=!0),t.transform&&(e=f(t.transform,e),l=!0),t.onError){let e=t.onError;c&&(e=O(e,c)),d[h].onError=e;}}else e=m;const s=r.get(e);if(s&&(!i&&"default"in s&&(o=s.default),!l&&s.transform&&(e=f(s.transform,e)),s.onError)){let t=s.onError;c&&(t=O(t,c)),d[h].onError=t;}if(t(o))d[h].default=()=>{};else {const t=n(o);d[h].default=()=>t;}d[h].vf=e;}else if(b(m)){const t=m,e=t._schemaOptions.init;d[h].default=!0===e?()=>t.new():null===e?()=>null:()=>{},p[h]=()=>t.new(),d[h].vf=t.test;}else if(i(m)){const[t,e]=o(m);d[h].default=()=>n(t),d[h].vf=e;}else s(h,"",a);const v=d[h].vf,y=d[h].default();v(y)||(d[h].onError?d[h].onError(h,y,u):s(h,y,u));}return {childSchemaNewFns:p,allVldtrsHolder:d}}(e,w,n,H,D.id),S=function(t,e,n){return (r={})=>{const o={};for(const e in t){const n=t[e].default();void 0!==n&&(o[e]=n);}for(const i in r){const l=t[i];if(!l)continue;let f=r[i];l.vf(f,(t=>f=t))?o[i]=e(f):(l.onError?l.onError(i,f,h):n(i,f,h),o[i]="**ERROR**");}return o}}(N.allVldtrsHolder,w,H),V=function(e,n,o,i="filter",l){return f=>{if(t(f))return !!n||(l("",f,c),!1);if(null===f)return !!o||(l("",f,d),!1);if(!r(f))return !1;for(const t in f){const n=e[t];let r=f[t];if(n){if(!n.vf(r,(e=>{r=e,Object.defineProperty(f,t,{value:r});})))return n.onError?n.onError(t,r,m):l(t,r,m),!1}else {if("strict"===i)return l(t,r,y),!1;"pass"!==i&&Reflect.deleteProperty(f,t);}}return !0}}(N.allVldtrsHolder,D.optional,D.nullable,D.safety,H),k=function(t,e="filter",n){return o=>{if(!r(o))return n("",o,p),o;for(const r in o){const i=t[r];let l=o[r];i?i.vf(l,(t=>{l=t,Object.defineProperty(o,r,{value:l});}))||(i.onError?i.onError(r,l,v):n(r,l,v)):("pass"!==e&&Reflect.deleteProperty(o,r),"strict"===e&&n(r,l,y));}return o}}(N.allVldtrsHolder,D.safety,H);return {new:S,test:V,pick:t=>{const n=e[t],r=t;if(n)return {default:N.allVldtrsHolder[r].default,test:N.allVldtrsHolder[r].vf,...b(n)?{pick:n.pick,new:N.childSchemaNewFns[r],schema:()=>({...n})}:{}}},parse:k,_schemaOptions:D}}}function b(t){return r(t)&&"_schemaOptions"in t}function E(t){return r(t)&&"vf"in t&&"function"==typeof t.vf}function g(t){return t instanceof Date?new Date(t):r(t)?structuredClone(t):t}function O(t,e){return (n,r,o)=>t(n,r,o,e)}function j(t,e,n,r){const o=D(t,e,n,r);throw new Error(o)}function D(t,e,n,r){t&&(t=`The property "${t}" failed to pass validation.`);let o="";if(!t||e||n||r)if(t||!e||n||r)if(t||e||!n||r){let i;i=t?{message:t,value:e,"more-details":n}:{message:n,value:e},r&&(i["schema-id"]=r),o=JSON.stringify(i);}else o=n;else o=JSON.stringify(e);else o=t;return o}const F=w(); | ||
function t(t){return void 0===t}function n(t){return "number"==typeof t}function r(t){return "string"==typeof t}function e(t){return !!t&&"object"==typeof t}function o(t){if(!s(t))throw Error('"getEnumKeys" must receive an enum object.');let r=Object.keys(t).reduce(((n,r)=>(n.includes(r)||n.push(t[r]),n)),[]);return n(t[r[0]])&&(r=r.map((n=>t[n]))),[r[0],t=>r.some((n=>n===t))]}function s(t){if(!e(t)||Array.isArray(t))return !1;const o=t,s=Object.keys(o),f=Math.floor(s.length/2);if(!n(o[s[f]]))return function(t,n){if(e(t))for(const r of Object.entries(t))if(!n(r[0],r[1]))return !1;return !0}(t,((t,n)=>r(t)&&r(n)));if(s.length%2!=0)return !1;for(let t=0;t<f;t++){const n=s[t],r=o[n],e=s[t+f],i=o[e];if(r!==e||n!==String(i))return !1}return !0}const f=t=>t instanceof Date&&!isNaN(new Date(t).getTime());function i(t){return t instanceof Date?new Date(t):e(t)?structuredClone(t):t}const l='"init:" option must be "true" if schema is not optional.',a="Default value did not pass validation.",u="Validator function failed.",c="Argument was undefined but not optional.",d="Argument was null but not nullable",m="Argument must be an object.",p="Extra properties not allowed in strict mode.";function h(t){if(t){if(Array.isArray(t)&&!(t.length>0))return;let n;throw n=r(t)?t:Array.isArray(t)&&1===t.length?JSON.stringify(t[0]):JSON.stringify(t),new Error(n)}}function y(t,n,r,e,o){const s={};return e&&(s.property=e),5===arguments.length&&(s.value=o),t&&(s.message=t),n&&(s.location=n),r&&(s.schemaId=r),s}function g(n){const r=function(t){const n=new Map;for(const r of t){const{vf:t,...e}=r;n.set(t,e);}return n}(n?.globals??[]),p=n?.cloneFn??i,g=n?.onError??h;return (n,...i)=>{const[h]=i,E=function(n){const r={optional:!1,nullable:!1,init:!0,safety:"filter"};if(t(n))return r;t(n.init)||(r.init=n.init);n.id&&(r.schemaId=n.id);n.safety&&(r.safety=n.safety);t(n.optional)||(r.optional=!!n.optional);t(n.nullable)||(r.nullable=!!n.nullable);"nullish"in n&&!t(n.nullish)&&(r.optional=n.nullish,r.nullable=n.nullish);return r}(h);if(!E.optional&&(!1===E.init||t(E.init))){const t=y(l,".schema",E.schemaId);g(t);}const A=function(n,r,e,i,u){const c={},d={},m=[];for(const i in n){const p={vf:t=>!!t,default:()=>{},formatError:t=>t},h=n[i];if(h===Date)p.vf=f,p.transform=t=>new Date(t),p.default=()=>new Date;else if("function"==typeof h||b(h)){let n,o,s=!1;if(b(h)){const t=h;n=t.vf,"default"in t&&(o=t.default,s=!0),t.transform&&(p.transform=t.transform),t.formatError&&(p.formatError=t.formatError);}else n=h;const f=r.get(n);if(f&&(!s&&"default"in f&&(o=f.default),!p.transform&&f.transform&&(p.transform=f.transform),!p.formatError&&f.formatError&&(p.formatError=f.formatError)),t(o))p.default=()=>{};else {const t=e(o);p.default=()=>t;}p.vf=n;}else if(v(h)){const t=h,n=t._schemaOptions.init;p.default=!0===n?()=>t.new():null===n?()=>null:()=>{},d[i]=()=>t.new(),p.vf=t.test;}else if(s(h)){const[t,n]=o(h);p.default=()=>e(t),p.vf=n;}else {const t=y(l,"._setupAllVldtrsHolder",u,i);m.push(t);}const g=p.default();if(!p.vf(g)){const t=y(a,"._setupAllVldtrsHolder",u,i,g),n=p.formatError(t);m.push(n);}c[i]=p;}m.length>0&&i(m);return {childSchemaNewFns:d,allVldtrsHolder:c}}(n,r,p,g,E.schemaId),D=function(t,n,r,e){return (o={})=>{const s={},f=[];for(const n in t){const r=t[n].default();void 0!==r&&(s[n]=r);}for(const r in o){const i=t[r];if(!i)continue;let l=o[r];if(i.transform&&(l=i.transform(l)),s[r]=n(l),!i.vf(l)){const t=y(u,".new",e,r,l),n=i.formatError(t);f.push(n);}}return f.length>0&&r(f),s}}(A.allVldtrsHolder,p,g,E.schemaId),O=function(n,r,o){const{schemaId:s,safety:f}=r,i=y(c,".test",s),l=y(d,".test",s),a=w(n,f,".test",s);return n=>{if(t(n))return !!r.optional||(o(i),!1);if(null===n)return !!r.nullable||(o(l),!1);if(!e(n))return !1;const s=a(n);return !(s.length>0)||(o(s),!1)}}(A.allVldtrsHolder,E,g),V=function(t,n,r){const{schemaId:o,safety:s}=n,f=y(m,".parse",o),i=w(t,s,".test",o);return t=>{if(!e(t))return r(f),t;const n=i(t);return n.length>0&&r(n),t}}(A.allVldtrsHolder,E,g);return {new:D,test:O,pick:t=>{const r=n[t],e=t,o=A.allVldtrsHolder[e],s=o.transform;let f=o.vf;if(s&&(f=t=>(t=s(t),o.vf(t))),r)return {default:o.default,test:f,...v(r)?{pick:r.pick,new:A.childSchemaNewFns[e],schema:()=>({...r})}:{}}},parse:V,_schemaOptions:E}}}function v(t){return e(t)&&"_schemaOptions"in t}function b(t){return e(t)&&"vf"in t&&"function"==typeof t.vf}function w(t,n,r,e){return o=>{const s=[];for(const n in t){const f=t[n];let i=o[n];if(f.transform&&(i=f.transform(i),o[n]=i),!f.vf(i)){const t=y(u,r,e,n,i),o=f.formatError(t);s.push(o);}}if("pass"!==n)for(const f in o)if(!(f in t))if("strict"===n){const t=y(p,r,e,f);s.push(t);}else "filter"===n&&Reflect.deleteProperty(o,f);return s}}const E=g(); | ||
exports.default = w; | ||
exports.schema = F; | ||
exports.default = g; | ||
exports.schema = E; |
@@ -1,2 +0,3 @@ | ||
import { TFunc, IValidatorFnOrObj, TEnum, IValidatorObj, TValidatorFn } from './util'; | ||
import { TFunc, TEnum } from './util'; | ||
import { TFormatError, TOnError } from './error-stuff'; | ||
type TStaticObj<Prop> = string extends keyof Prop ? never : { | ||
@@ -10,6 +11,14 @@ [key: string]: string | number | boolean | TStaticObj<Prop>; | ||
type NotUndef<T> = Exclude<T, undefined>; | ||
type TModel = Record<string | number | symbol, unknown>; | ||
type AddNullablesHelper<T, isN> = isN extends true ? NonNullable<T> | null : NonNullable<T>; | ||
type AddNullables<T, isU, isN> = (isU extends true ? AddNullablesHelper<NotUndef<T>, isN> | undefined : AddNullablesHelper<NotUndef<T>, isN>); | ||
type GetTypePredicate<T> = T extends (x: unknown) => x is infer U ? U : never; | ||
type TOnError = (property: string, value?: unknown, origMessage?: string, schemaId?: string) => void; | ||
type TValidatorFn<T = unknown> = (arg: unknown, parentObj?: TModel, key?: string) => arg is T; | ||
export interface IValidatorObj<T = unknown> { | ||
vf: TValidatorFn<T>; | ||
default?: T; | ||
transform?: TFunc; | ||
formatError?: TFormatError; | ||
} | ||
type IValidatorFnOrObj<T> = TValidatorFn<T> | IValidatorObj<T>; | ||
type TPickRetVal<T, NnT = NonNullable<T>> = { | ||
@@ -23,3 +32,3 @@ test: (arg: unknown) => arg is T; | ||
} : unknown); | ||
export interface ISchema<T> { | ||
export interface ISchema<T = unknown> { | ||
new: (arg?: Partial<NonNullable<T>>) => NonNullable<T>; | ||
@@ -41,3 +50,3 @@ test: (arg: unknown) => arg is T; | ||
interface IJetOptions<M> { | ||
globals?: M extends IValidatorObj<unknown>[] ? M : never; | ||
globals?: M extends IValidatorObj[] ? M : never; | ||
cloneFn?: (value: unknown) => unknown; | ||
@@ -48,21 +57,10 @@ onError?: TOnError; | ||
[K in keyof M]: { | ||
vf: TValidatorFn<unknown>; | ||
vf: TValidatorFn; | ||
} & ('vf' extends keyof M[K] ? { | ||
default?: GetTypePredicate<M[K]['vf']>; | ||
transform?: TFunc; | ||
onError?: IValidatorObj<unknown>['onError']; | ||
formatError?: TFormatError; | ||
} : never); | ||
}; | ||
type TSchemaOptions<T> = (unknown extends T ? (IOptNul | IOptNotNul | INotOptButNul | INotOptOrNul | INullish) : (undefined extends T ? (null extends T ? (IOptNul | INullish) : IOptNotNul) : (null extends T ? INotOptButNul : INotOptOrNul))); | ||
type TSchemaOptionsHelper<T, R> = (unknown extends T ? ([] | [R]) : undefined extends T ? [R] : null extends T ? [R] : ([] | [R])); | ||
export type PublicInferType<S> = (S extends ISchema<infer X> ? X : never); | ||
type InferTypes<U, R, Schema = MakeKeysOptIfUndef<InferTypesHelper<U>>> = ('nullish' extends keyof R ? AddNullables<Schema, true, true> : AddNullables<Schema, 'optional' extends keyof R ? R['optional'] : false, 'nullable' extends keyof R ? R['nullable'] : false>); | ||
type MakeKeysOptIfUndef<T> = { | ||
[K in keyof T]-?: (x: undefined extends T[K] ? Partial<Record<K, T[K]>> : Record<K, T[K]>) => void; | ||
}[keyof T] extends (x: infer I) => void ? I extends infer U ? { | ||
[K in keyof U]: U[K]; | ||
} : never : never; | ||
type InferTypesHelper<U> = { | ||
[K in keyof U]: (U[K] extends DateConstructor ? Date : U[K] extends IValidatorFnOrObj<infer X> ? X : U[K] extends ISchema<infer X> ? X : U[K] extends unknown[] ? never : U[K] extends TEnum ? U[K][keyof U[K]] : never); | ||
}; | ||
type TSchemaOptions<T = unknown> = (unknown extends T ? (IOptNul | IOptNotNul | INotOptButNul | INotOptOrNul | INullish) : (undefined extends T ? (null extends T ? (IOptNul | INullish) : IOptNotNul) : (null extends T ? INotOptButNul : INotOptOrNul))); | ||
interface ISchemaOptionsBase { | ||
@@ -99,3 +97,14 @@ id?: string; | ||
} | ||
type TSchemaOptionsHelper<T, R> = (unknown extends T ? ([] | [R]) : undefined extends T ? [R] : null extends T ? [R] : ([] | [R])); | ||
export type PublicInferType<S> = (S extends ISchema<infer X> ? X : never); | ||
type InferTypes<U, R, Schema = MakeKeysOptIfUndef<InferTypesHelper<U>>> = ('nullish' extends keyof R ? AddNullables<Schema, true, true> : AddNullables<Schema, 'optional' extends keyof R ? R['optional'] : false, 'nullable' extends keyof R ? R['nullable'] : false>); | ||
type MakeKeysOptIfUndef<T> = { | ||
[K in keyof T]-?: (x: undefined extends T[K] ? Partial<Record<K, T[K]>> : Record<K, T[K]>) => void; | ||
}[keyof T] extends (x: infer I) => void ? I extends infer U ? { | ||
[K in keyof U]: U[K]; | ||
} : never : never; | ||
type InferTypesHelper<U> = { | ||
[K in keyof U]: (U[K] extends DateConstructor ? Date : U[K] extends IValidatorFnOrObj<infer X> ? X : U[K] extends ISchema<infer X> ? X : U[K] extends unknown[] ? never : U[K] extends TEnum ? U[K][keyof U[K]] : never); | ||
}; | ||
declare function jetSchema<M extends TGlobalsArr<M>>(options?: IJetOptions<M>): <T, U extends TSchemaFnObjArg<T> = Required<{ [K in keyof T]: T[K] extends string | number ? TEnum | IValidatorFnOrObj<T[K]> : T[K] extends Date ? DateConstructor | IValidatorFnOrObj<T[K]> : IsStaticObj<T[K], NonNullable<T[K]>> extends true ? ISchema<T[K]> : IValidatorFnOrObj<T[K]>; }>, R extends TSchemaOptions<T> = TSchemaOptions<T>>(schemaFnObjArg: U, ...options: TSchemaOptionsHelper<T, R>) => unknown extends T ? ISchema<InferTypes<U, R>> : ISchema<T>; | ||
export default jetSchema; |
@@ -15,29 +15,13 @@ "use strict"; | ||
const util_1 = require("./util"); | ||
const Errors = { | ||
Init: '"init" must be true if schema is not optional.', | ||
Default: 'This validation failed when setting up defaults.', | ||
Validator: 'Setup error: validator must be a function, enum, schema, or Date constructor.', | ||
Undef: 'schema.test failed: value was undefined but not optional.', | ||
Null: 'schema.test failed: value was null but not nullable.', | ||
NotAnObj: 'schema.test failed: value was neither undefined nor null but not an object.', | ||
ParseNotAnObj: 'schema.parse failed: value must be an object.', | ||
NewFn: 'Failed in the "new" function', | ||
TestFn: 'Failed in the "test" function', | ||
ParseFn: 'Failed in the "parse" function', | ||
StrictMode: 'Argument had a property which was not in schema, which is not allowed in strict mode', | ||
}; | ||
const error_stuff_1 = require("./error-stuff"); | ||
function jetSchema(options) { | ||
var _a; | ||
const globalsMap = _setupGlobalsMap((_a = options === null || options === void 0 ? void 0 : options.globals) !== null && _a !== void 0 ? _a : []), cloneFn = ((options === null || options === void 0 ? void 0 : options.cloneFn) ? options.cloneFn : _defaultClone), onError = ((options === null || options === void 0 ? void 0 : options.onError) ? _wrapCustomError(options.onError) : _defaultOnErr); | ||
var _a, _b, _c; | ||
const globalsMap = _setupGlobalsMap((_a = options === null || options === void 0 ? void 0 : options.globals) !== null && _a !== void 0 ? _a : []), cloneFn = ((_b = options === null || options === void 0 ? void 0 : options.cloneFn) !== null && _b !== void 0 ? _b : util_1.defaultCloneFn), onError = ((_c = options === null || options === void 0 ? void 0 : options.onError) !== null && _c !== void 0 ? _c : error_stuff_1.defaultOnError); | ||
return (schemaFnObjArg, ...options) => { | ||
const [schemaOptions] = options; | ||
const optionsF = _processOptions(schemaOptions); | ||
let onErrorF = onError; | ||
if (optionsF.id) { | ||
onErrorF = _wrapErrorWithSchemaId(onErrorF, optionsF.id); | ||
} | ||
const [schemaOptions] = options, optionsF = _processOptions(schemaOptions); | ||
if (!optionsF.optional && (optionsF.init === false || (0, util_1.isUndef)(optionsF.init))) { | ||
onErrorF('', '', Errors.Init); | ||
const err = (0, error_stuff_1.getErrObj)(error_stuff_1.Errors.Init, '.schema', optionsF.schemaId); | ||
onError(err); | ||
} | ||
const ret = _setupAllVldtrsHolder(schemaFnObjArg, cloneFn, globalsMap, onErrorF, optionsF.id), newFn = _setupNewFn(ret.allVldtrsHolder, cloneFn, onErrorF), testFn = _setupTestFn(ret.allVldtrsHolder, optionsF.optional, optionsF.nullable, optionsF.safety, onErrorF), parseFn = _setupParseFn(ret.allVldtrsHolder, optionsF.safety, onErrorF); | ||
const ret = _setupAllVldtrsHolder(schemaFnObjArg, globalsMap, cloneFn, onError, optionsF.schemaId), newFn = _setupNewFn(ret.allVldtrsHolder, cloneFn, onError, optionsF.schemaId), testFn = _setupTestFn(ret.allVldtrsHolder, optionsF, onError), parseFn = _setupParseFn(ret.allVldtrsHolder, optionsF, onError); | ||
return { | ||
@@ -47,5 +31,12 @@ new: newFn, | ||
pick: (p) => { | ||
const prop = schemaFnObjArg[p], key = p; | ||
const prop = schemaFnObjArg[p], key = p, vldrObj = ret.allVldtrsHolder[key], transformFn = vldrObj.transform; | ||
let testFn = vldrObj.vf; | ||
if (!!transformFn) { | ||
testFn = (val) => { | ||
val = transformFn(val); | ||
return vldrObj.vf(val); | ||
}; | ||
} | ||
if (!!prop) { | ||
return Object.assign({ default: ret.allVldtrsHolder[key].default, test: ret.allVldtrsHolder[key].vf }, (_isSchemaObj(prop) ? { | ||
return Object.assign({ default: vldrObj.default, test: testFn }, (_isSchemaObj(prop) ? { | ||
pick: prop.pick, | ||
@@ -62,19 +53,19 @@ new: ret.childSchemaNewFns[key], | ||
} | ||
function _setupAllVldtrsHolder(schemaArgObj, cloneFn, globalsMap, onError, schemaId = '') { | ||
const allVldtrsHolder = {}, childSchemaNewFns = {}; | ||
function _setupAllVldtrsHolder(schemaArgObj, globalsMap, cloneFn, onError, schemaId) { | ||
const allVldtrsHolder = {}, childSchemaNewFns = {}, errors = []; | ||
for (const key in schemaArgObj) { | ||
const vldrHolderObj = { | ||
vf: (arg) => !!arg, | ||
default: () => undefined, | ||
formatError: (error) => error, | ||
}; | ||
const schemaArgProp = schemaArgObj[key]; | ||
allVldtrsHolder[key] = { | ||
vf: (arg) => false, | ||
default: () => false, | ||
}; | ||
if (schemaArgProp === Date) { | ||
allVldtrsHolder[key] = { | ||
vf: (0, util_1.transform)((arg) => new Date(arg), util_1.isDate), | ||
default: () => new Date(), | ||
}; | ||
vldrHolderObj.vf = util_1.isDate; | ||
vldrHolderObj.transform = (arg) => new Date(arg); | ||
vldrHolderObj.default = () => new Date(); | ||
} | ||
else if ((typeof schemaArgProp === 'function') || | ||
_isValidatorObj(schemaArgProp)) { | ||
let vdlrFn, defaultVal, hasLocalDefault = false, hasLocalTransform = false; | ||
let vdlrFn, defaultVal, hasLocalDefault = false; | ||
if (_isValidatorObj(schemaArgProp)) { | ||
@@ -88,11 +79,6 @@ const localObj = schemaArgProp; | ||
if (!!localObj.transform) { | ||
vdlrFn = (0, util_1.transform)(localObj.transform, vdlrFn); | ||
hasLocalTransform = true; | ||
vldrHolderObj.transform = localObj.transform; | ||
} | ||
if (!!localObj.onError) { | ||
let customErr = localObj.onError; | ||
if (!!schemaId) { | ||
customErr = _wrapErrorWithSchemaId(customErr, schemaId); | ||
} | ||
allVldtrsHolder[key].onError = customErr; | ||
if (!!localObj.formatError) { | ||
vldrHolderObj.formatError = localObj.formatError; | ||
} | ||
@@ -108,11 +94,7 @@ } | ||
} | ||
if (!hasLocalTransform && globalsObj.transform) { | ||
vdlrFn = (0, util_1.transform)(globalsObj.transform, vdlrFn); | ||
if (!vldrHolderObj.transform && !!globalsObj.transform) { | ||
vldrHolderObj.transform = globalsObj.transform; | ||
} | ||
if (!!globalsObj.onError) { | ||
let customErr = globalsObj.onError; | ||
if (!!schemaId) { | ||
customErr = _wrapErrorWithSchemaId(customErr, schemaId); | ||
} | ||
allVldtrsHolder[key].onError = customErr; | ||
if (!vldrHolderObj.formatError && !!globalsObj.formatError) { | ||
vldrHolderObj.formatError = globalsObj.formatError; | ||
} | ||
@@ -122,8 +104,8 @@ } | ||
const defaultF = cloneFn(defaultVal); | ||
allVldtrsHolder[key].default = () => defaultF; | ||
vldrHolderObj.default = () => defaultF; | ||
} | ||
else { | ||
allVldtrsHolder[key].default = () => undefined; | ||
vldrHolderObj.default = () => undefined; | ||
} | ||
allVldtrsHolder[key].vf = vdlrFn; | ||
vldrHolderObj.vf = vdlrFn; | ||
} | ||
@@ -133,31 +115,33 @@ else if (_isSchemaObj(schemaArgProp)) { | ||
if (dflt === true) { | ||
allVldtrsHolder[key].default = () => childSchema.new(); | ||
vldrHolderObj.default = () => childSchema.new(); | ||
} | ||
else if (dflt === null) { | ||
allVldtrsHolder[key].default = () => null; | ||
vldrHolderObj.default = () => null; | ||
} | ||
else { | ||
allVldtrsHolder[key].default = () => undefined; | ||
vldrHolderObj.default = () => undefined; | ||
} | ||
childSchemaNewFns[key] = () => childSchema.new(); | ||
allVldtrsHolder[key].vf = childSchema.test; | ||
vldrHolderObj.vf = childSchema.test; | ||
} | ||
else if ((0, util_1.isEnum)(schemaArgProp)) { | ||
const [dflt, vldr] = (0, util_1.processEnum)(schemaArgProp); | ||
allVldtrsHolder[key].default = () => cloneFn(dflt); | ||
allVldtrsHolder[key].vf = vldr; | ||
vldrHolderObj.default = () => cloneFn(dflt); | ||
vldrHolderObj.vf = vldr; | ||
} | ||
else { | ||
onError(key, '', Errors.Validator); | ||
const errObj = (0, error_stuff_1.getErrObj)(error_stuff_1.Errors.Init, '._setupAllVldtrsHolder', schemaId, key); | ||
errors.push(errObj); | ||
} | ||
const vldr = allVldtrsHolder[key].vf, dfltVal = allVldtrsHolder[key].default(); | ||
if (!vldr(dfltVal)) { | ||
if (!!allVldtrsHolder[key].onError) { | ||
allVldtrsHolder[key].onError(key, dfltVal, Errors.Default); | ||
} | ||
else { | ||
onError(key, dfltVal, Errors.Default); | ||
} | ||
const dfltVal = vldrHolderObj.default(); | ||
if (!vldrHolderObj.vf(dfltVal)) { | ||
const errObj = (0, error_stuff_1.getErrObj)(error_stuff_1.Errors.DefaultVal, '._setupAllVldtrsHolder', schemaId, key, dfltVal); | ||
const errFin = vldrHolderObj.formatError(errObj); | ||
errors.push(errFin); | ||
} | ||
allVldtrsHolder[key] = vldrHolderObj; | ||
} | ||
if (errors.length > 0) { | ||
onError(errors); | ||
} | ||
return { | ||
@@ -174,5 +158,5 @@ childSchemaNewFns, | ||
} | ||
function _setupNewFn(allVldtrsHolder, cloneFn, onError) { | ||
function _setupNewFn(allVldtrsHolder, cloneFn, onError, schemaId) { | ||
return (partial = {}) => { | ||
const retVal = {}; | ||
const retVal = {}, errors = []; | ||
for (const key in allVldtrsHolder) { | ||
@@ -190,26 +174,27 @@ const val = allVldtrsHolder[key].default(); | ||
let val = partial[key]; | ||
if (vldrObj.vf(val, (transVal => val = transVal))) { | ||
retVal[key] = cloneFn(val); | ||
if (!!vldrObj.transform) { | ||
val = vldrObj.transform(val); | ||
} | ||
else { | ||
if (!!vldrObj.onError) { | ||
vldrObj.onError(key, val, Errors.NewFn); | ||
} | ||
else { | ||
onError(key, val, Errors.NewFn); | ||
} | ||
retVal[key] = '**ERROR**'; | ||
retVal[key] = cloneFn(val); | ||
if (!vldrObj.vf(val)) { | ||
const errObj = (0, error_stuff_1.getErrObj)(error_stuff_1.Errors.PropValidation, '.new', schemaId, key, val); | ||
const errFin = vldrObj.formatError(errObj); | ||
errors.push(errFin); | ||
} | ||
} | ||
if (errors.length > 0) { | ||
onError(errors); | ||
} | ||
return retVal; | ||
}; | ||
} | ||
function _setupTestFn(allVldtrsHolder, isOptional, isNullable, safety = 'filter', onError) { | ||
function _setupTestFn(allVldtrsHolder, options, onError) { | ||
const { schemaId, safety } = options, NotOptErr = (0, error_stuff_1.getErrObj)(error_stuff_1.Errors.UndefButNotOpt, '.test', schemaId), NullErr = (0, error_stuff_1.getErrObj)(error_stuff_1.Errors.NullButNotNullable, '.test', schemaId), runValidations = _setupRunValidations(allVldtrsHolder, safety, '.test', schemaId); | ||
return (arg) => { | ||
if ((0, util_1.isUndef)(arg)) { | ||
if (isOptional) { | ||
if (options.optional) { | ||
return true; | ||
} | ||
else { | ||
onError('', arg, Errors.Undef); | ||
onError(NotOptErr); | ||
return false; | ||
@@ -219,7 +204,7 @@ } | ||
else if (arg === null) { | ||
if (isNullable) { | ||
if (options.nullable) { | ||
return true; | ||
} | ||
else { | ||
onError('', arg, Errors.Null); | ||
onError(NullErr); | ||
return false; | ||
@@ -231,60 +216,22 @@ } | ||
} | ||
for (const key in arg) { | ||
const vldrObj = allVldtrsHolder[key]; | ||
let val = arg[key]; | ||
if (!vldrObj) { | ||
if (safety === 'strict') { | ||
onError(key, val, Errors.StrictMode); | ||
return false; | ||
} | ||
else if (safety !== 'pass') { | ||
Reflect.deleteProperty(arg, key); | ||
} | ||
continue; | ||
} | ||
if (!vldrObj.vf(val, (transVal => { | ||
val = transVal; | ||
Object.defineProperty(arg, key, { value: val }); | ||
}))) { | ||
if (!!vldrObj.onError) { | ||
vldrObj.onError(key, val, Errors.TestFn); | ||
} | ||
else { | ||
onError(key, val, Errors.TestFn); | ||
} | ||
return false; | ||
} | ||
const errors = runValidations(arg); | ||
if (errors.length > 0) { | ||
onError(errors); | ||
return false; | ||
} | ||
return true; | ||
else { | ||
return true; | ||
} | ||
}; | ||
} | ||
function _setupParseFn(allVldtrsHolder, safety = 'filter', onError) { | ||
function _setupParseFn(allVldtrsHolder, options, onError) { | ||
const { schemaId, safety } = options, notAnObjErr = (0, error_stuff_1.getErrObj)(error_stuff_1.Errors.NotAnObj, '.parse', schemaId), runValidations = _setupRunValidations(allVldtrsHolder, safety, '.test', schemaId); | ||
return (arg) => { | ||
if (!(0, util_1.isObj)(arg)) { | ||
onError('', arg, Errors.ParseNotAnObj); | ||
onError(notAnObjErr); | ||
return arg; | ||
} | ||
for (const key in arg) { | ||
const vldrObj = allVldtrsHolder[key]; | ||
let val = arg[key]; | ||
if (!vldrObj) { | ||
if (safety !== 'pass') { | ||
Reflect.deleteProperty(arg, key); | ||
} | ||
if (safety === 'strict') { | ||
onError(key, val, Errors.StrictMode); | ||
} | ||
continue; | ||
} | ||
if (!vldrObj.vf(val, (transVal => { | ||
val = transVal; | ||
Object.defineProperty(arg, key, { value: val }); | ||
}))) { | ||
if (!!vldrObj.onError) { | ||
vldrObj.onError(key, val, Errors.ParseFn); | ||
} | ||
else { | ||
onError(key, val, Errors.ParseFn); | ||
} | ||
} | ||
const errors = runValidations(arg); | ||
if (errors.length > 0) { | ||
onError(errors); | ||
} | ||
@@ -303,83 +250,66 @@ return arg; | ||
function _processOptions(options) { | ||
let base = { init: true, safety: 'filter' }; | ||
if (!(0, util_1.isUndef)(options === null || options === void 0 ? void 0 : options.init)) { | ||
base.init = options.init; | ||
const retVal = { | ||
optional: false, | ||
nullable: false, | ||
init: true, | ||
safety: 'filter', | ||
}; | ||
if ((0, util_1.isUndef)(options)) { | ||
return retVal; | ||
} | ||
if (!!(options === null || options === void 0 ? void 0 : options.id)) { | ||
base.id = options === null || options === void 0 ? void 0 : options.id; | ||
if (!(0, util_1.isUndef)(options.init)) { | ||
retVal.init = options.init; | ||
} | ||
if (!!(options === null || options === void 0 ? void 0 : options.safety)) { | ||
base.safety = options.safety; | ||
if (!!options.id) { | ||
retVal.schemaId = options.id; | ||
} | ||
if (!(options === null || options === void 0 ? void 0 : options.nullish)) { | ||
base = Object.assign(Object.assign({}, base), { optional: !!(options === null || options === void 0 ? void 0 : options.optional), nullable: !!(options === null || options === void 0 ? void 0 : options.nullable) }); | ||
if (!!options.safety) { | ||
retVal.safety = options.safety; | ||
} | ||
else { | ||
base = Object.assign(Object.assign({}, base), { optional: true, nullable: true }); | ||
if (!(0, util_1.isUndef)(options.optional)) { | ||
retVal.optional = !!options.optional; | ||
} | ||
return base; | ||
} | ||
function _defaultClone(arg) { | ||
if (arg instanceof Date) { | ||
return new Date(arg); | ||
if (!(0, util_1.isUndef)(options.nullable)) { | ||
retVal.nullable = !!options.nullable; | ||
} | ||
else if ((0, util_1.isObj)(arg)) { | ||
return structuredClone(arg); | ||
if (('nullish' in options) && !(0, util_1.isUndef)(options.nullish)) { | ||
retVal.optional = options.nullish; | ||
retVal.nullable = options.nullish; | ||
} | ||
else { | ||
return arg; | ||
} | ||
return retVal; | ||
} | ||
function _wrapErrorWithSchemaId(onError, schemaId) { | ||
return (property, value, moreDetails) => { | ||
return onError(property, value, moreDetails, schemaId); | ||
}; | ||
} | ||
function _wrapCustomError(onError) { | ||
return (property, value, moreDetails, schemaId) => { | ||
const origMessage = _getDefaultErrMsg(property, value, moreDetails, schemaId); | ||
return onError(property, value, origMessage); | ||
}; | ||
} | ||
function _defaultOnErr(property, value, moreDetails, schemaId) { | ||
const message = _getDefaultErrMsg(property, value, moreDetails, schemaId); | ||
throw new Error(message); | ||
} | ||
function _getDefaultErrMsg(property, value, moreDetails, id) { | ||
if (!!property) { | ||
property = `The property "${property}" failed to pass validation.`; | ||
} | ||
let message = ''; | ||
if (!!property && !value && !moreDetails && !id) { | ||
message = property; | ||
} | ||
else if (!property && !!value && !moreDetails && !id) { | ||
message = JSON.stringify(value); | ||
} | ||
else if (!property && !value && !!moreDetails && !id) { | ||
message = moreDetails; | ||
} | ||
else { | ||
let msgObj; | ||
if (!property) { | ||
msgObj = { | ||
message: moreDetails, | ||
value, | ||
}; | ||
function _setupRunValidations(allVldtrsHolder, safety, location, schemaId) { | ||
return (arg) => { | ||
const errors = []; | ||
for (const key in allVldtrsHolder) { | ||
const vldrObj = allVldtrsHolder[key]; | ||
let val = arg[key]; | ||
if (!!vldrObj.transform) { | ||
val = vldrObj.transform(val); | ||
arg[key] = val; | ||
} | ||
if (!vldrObj.vf(val)) { | ||
const errObj = (0, error_stuff_1.getErrObj)(error_stuff_1.Errors.PropValidation, location, schemaId, key, val); | ||
const errFin = vldrObj.formatError(errObj); | ||
errors.push(errFin); | ||
} | ||
} | ||
else { | ||
msgObj = { | ||
message: property, | ||
value, | ||
['more-details']: moreDetails, | ||
}; | ||
if (safety !== 'pass') { | ||
for (const key in arg) { | ||
if (key in allVldtrsHolder) { | ||
continue; | ||
} | ||
else if (safety === 'strict') { | ||
const errObj = (0, error_stuff_1.getErrObj)(error_stuff_1.Errors.StrictMode, location, schemaId, key); | ||
errors.push(errObj); | ||
} | ||
else if (safety === 'filter') { | ||
Reflect.deleteProperty(arg, key); | ||
} | ||
} | ||
} | ||
if (!!id) { | ||
msgObj['schema-id'] = id; | ||
} | ||
message = JSON.stringify(msgObj); | ||
} | ||
return message; | ||
return errors; | ||
}; | ||
} | ||
exports.default = jetSchema; | ||
//# sourceMappingURL=jetSchema.js.map |
export type TFunc = (...args: any[]) => any; | ||
export type TBasicObj = Record<string, unknown>; | ||
export type TEnum = Record<string, string | number>; | ||
export type TValidatorFn<T> = (arg: unknown, cb?: ((transformedVal: T) => void)) => arg is T; | ||
export interface IValidatorObj<T> { | ||
vf: TValidatorFn<T>; | ||
default?: T; | ||
transform?: TFunc; | ||
onError?: (property: string, value?: unknown, moreDetails?: string, schemaId?: string) => void; | ||
} | ||
export type IValidatorFnOrObj<T> = TValidatorFn<T> | IValidatorObj<T>; | ||
export declare function isUndef(param: unknown): param is undefined; | ||
@@ -22,2 +14,2 @@ export declare function isNum(param: unknown): param is number; | ||
export declare const isDate: (val: unknown) => val is Date; | ||
export declare function transform<T>(transFn: TFunc, vldt: ((arg: unknown) => arg is T)): TValidatorFn<T>; | ||
export declare function defaultCloneFn(arg: unknown): unknown; |
@@ -13,3 +13,3 @@ "use strict"; | ||
exports.isBasicObj = isBasicObj; | ||
exports.transform = transform; | ||
exports.defaultCloneFn = defaultCloneFn; | ||
function isUndef(param) { | ||
@@ -31,4 +31,4 @@ return param === undefined; | ||
function processEnum(arg) { | ||
if (!isNonArrObj(arg)) { | ||
throw Error('"getEnumKeys" must receive a non-array object'); | ||
if (!isEnum(arg)) { | ||
throw Error('"getEnumKeys" must receive an enum object.'); | ||
} | ||
@@ -50,3 +50,3 @@ let vals = Object.keys(arg).reduce((arr, key) => { | ||
function isEnum(arg) { | ||
if (!(isObj(arg) && !Array.isArray(arg))) { | ||
if (!isObj(arg) || Array.isArray(arg)) { | ||
return false; | ||
@@ -88,11 +88,13 @@ } | ||
exports.isDate = isDate; | ||
function transform(transFn, vldt) { | ||
return (arg, cb) => { | ||
if (arg !== undefined) { | ||
arg = transFn(arg); | ||
} | ||
cb === null || cb === void 0 ? void 0 : cb(arg); | ||
return vldt(arg); | ||
}; | ||
function defaultCloneFn(arg) { | ||
if (arg instanceof Date) { | ||
return new Date(arg); | ||
} | ||
else if (isObj(arg)) { | ||
return structuredClone(arg); | ||
} | ||
else { | ||
return arg; | ||
} | ||
} | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "jet-schema", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Simple, typescript-first schema validation tool", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66494
17
786