UNPKG

@moostjs/zod

Version:
336 lines (328 loc) 22.3 kB
import { z } from 'zod'; export { z } from 'zod'; import * as moost from 'moost'; import { Mate, TMoostMetadata, TMateParamMeta, TPipeFn } from 'moost'; import * as zod_lib_helpers_errorUtil from 'zod/lib/helpers/errorUtil'; type TObject = object; type TFunction = Function; interface TEmpty { } type TPrimitives = 'undefined' | 'boolean' | 'number' | 'bigint' | 'string' | 'symbol'; interface TLogger { error: (...args: unknown[]) => void; warn: (...args: unknown[]) => void; log: (...args: unknown[]) => void; info: (...args: unknown[]) => void; } interface TZodOpts { coerce?: true | undefined; errorMap?: z.ZodErrorMap; invalid_type_error?: string; required_error?: string; } declare function getZodMate(): Mate<TMoostMetadata<TEmpty> & TZodMate & { params: Array<TZodMate & TMateParamMeta>; }, TMoostMetadata<TEmpty> & TZodMate & { params: Array<TZodMate & TMateParamMeta>; }>; interface TZodMate { zodType?: z.ZodType | ((opts?: TZodOpts) => z.ZodType); zodCoerce?: true; zodPreprocess?: Array<(arg: unknown) => unknown>; zodMarkedAsArray?: boolean; zodFn?: TZodFunctionDefinition[]; zodClassName?: string; zodPropName?: string; zodParamIndex?: number; zodDefault?: unknown; zodValidate?: true; zodSkip?: boolean; zodObj?: 'strict' | 'passthrough' | 'strip'; default?: unknown; } type TZodFunction = (type: z.ZodType | z.ZodString) => z.ZodType | z.ZodString; interface TZodFunctionDefinition { decorator: string; fn: TZodFunction; } type TZodMetadata = ReturnType<ReturnType<typeof getZodMate>['read']>; declare function validate<T extends TObject | z.ZodType, S extends boolean>(data: unknown, dto: (new () => T) | z.ZodType<T>, opts?: TZodOpts, safe?: S, logger?: TLogger): Promise<S extends true ? z.SafeParseReturnType<unknown, T> : T>; declare function getZodTypeForProp(origin: { type: TFunction; key: string | symbol; index?: number; }, target: { type?: TFunction; value?: unknown; additionalMeta?: TZodMetadata; }, opts?: TZodOpts, logger?: TLogger): z.ZodType; declare function getZodType(target: { type?: TFunction; value?: unknown; additionalMeta?: TZodMetadata; }, opts?: TZodOpts, logger?: TLogger): TMoostZodType; type TMoostZodType = z.ZodType & { __type_ref?: TFunction; }; declare const mate: moost.Mate<moost.TMoostMetadata<TEmpty> & TZodMate & { params: (TZodMate & moost.TMateParamMeta)[]; }, moost.TMoostMetadata<TEmpty> & TZodMate & { params: (TZodMate & moost.TMateParamMeta)[]; }>; declare const Validatable: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const Strict: () => ClassDecorator; declare const Passthrough: () => ClassDecorator; declare const Strip: () => ClassDecorator; declare const Zod: (type: TZodMate['zodType']) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const ZodSkip: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const LazyType: <T extends Function | z.ZodType<any, z.ZodTypeDef, any>>(getter: () => T, opts?: Parameters<typeof z.lazy>[1]) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const Coerce: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const Default: (value: unknown) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsArray: (types?: (TFunction | z.ZodType | TPrimitives) | Array<TFunction | z.ZodType | TPrimitives>, opts?: { coerce?: true; }) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const Refine: (check: (arg: string) => unknown, message?: string | Partial<z.util.Omit<z.ZodCustomIssue, "code">> | ((arg: string) => Partial<z.util.Omit<z.ZodCustomIssue, "code">>) | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const SuperRefine: (refinement: (arg: string, ctx: z.RefinementCtx) => Promise<void>) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const Trim: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const Transform: (transform: (arg: any, ctx: z.RefinementCtx) => unknown) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const OnCatch: <Output>(def: Output | ((ctx: { error: z.ZodError; input: unknown; }) => Output)) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const HasLength: (len: number, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const Min: (minLength: number, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const Max: (maxLength: number, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const DateFrom: (minDate: Date, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const DateTo: (maxDate: Date, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsNullable: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsNullish: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsEmail: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsUrl: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsEmoji: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsUuid: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsCuid: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsCuid2: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsUlid: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsDatetime: (options?: string | { message?: string | undefined; precision?: number | null | undefined; offset?: boolean | undefined; local?: boolean | undefined; } | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsIp: (options?: string | { version?: z.IpVersion | undefined; message?: string | undefined; } | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const MatchesRegex: (regex: RegExp, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const StartsWith: (value: string, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const EndsWith: (value: string, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsGt: (value: number, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsGte: (value: number, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsLt: (value: number, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsLte: (value: number, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsInt: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsPositive: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsNonnegative: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsNegative: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsNonpositive: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsMultipleOf: (value: number, message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsFinite: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsSafeNumber: (message?: zod_lib_helpers_errorUtil.errorUtil.ErrMessage | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const Includes: (value: string, options?: { message?: string | undefined; position?: number | undefined; } | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsString: (params?: ({ errorMap?: z.ZodErrorMap | undefined; invalid_type_error?: string | undefined; required_error?: string | undefined; message?: string | undefined; description?: string | undefined; } & { coerce?: true | undefined; }) | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsNumber: (params?: ({ errorMap?: z.ZodErrorMap | undefined; invalid_type_error?: string | undefined; required_error?: string | undefined; message?: string | undefined; description?: string | undefined; } & { coerce?: boolean | undefined; }) | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsBigint: (params?: ({ errorMap?: z.ZodErrorMap | undefined; invalid_type_error?: string | undefined; required_error?: string | undefined; message?: string | undefined; description?: string | undefined; } & { coerce?: boolean | undefined; }) | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsBoolean: (params?: ({ errorMap?: z.ZodErrorMap | undefined; invalid_type_error?: string | undefined; required_error?: string | undefined; message?: string | undefined; description?: string | undefined; } & { coerce?: boolean | undefined; }) | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsDate: (params?: ({ errorMap?: z.ZodErrorMap | undefined; invalid_type_error?: string | undefined; required_error?: string | undefined; message?: string | undefined; description?: string | undefined; } & { coerce?: boolean | undefined; }) | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsSymbol: (params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsUndefined: (params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsNull: (params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsVoid: (params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsAny: (params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsUnknown: (params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsNever: (params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsTuple: (schemas: Array<TFunction | z.ZodType | TPrimitives>) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsEnum: (values: [string, ...string[]], params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsNativeEnum: (values: z.EnumLike, params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsSet: (type: TFunction | z.ZodType | TPrimitives) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsMap: (type: TFunction | z.ZodType | TPrimitives, type2: TFunction | z.ZodType | TPrimitives) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsLiteral: (value: z.Primitive, params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsNaN: (params?: z.RawCreateParams) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsRecord: (type: TFunction | z.ZodType | TPrimitives, type2?: TFunction | z.ZodType | TPrimitives) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsUnion: (types_0: Function | z.ZodType<any, z.ZodTypeDef, any> | TPrimitives, types_1: Function | z.ZodType<any, z.ZodTypeDef, any> | TPrimitives, ...types_2: (Function | z.ZodType<any, z.ZodTypeDef, any> | TPrimitives)[]) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsDiscriminatedUnion: (discriminator: string, options: [ TFunction | z.ZodType | TPrimitives, TFunction | z.ZodType | TPrimitives, ...Array<TFunction | z.ZodType | TPrimitives> ]) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsIntersection: (left: TFunction | z.ZodType | TPrimitives, right: TFunction | z.ZodType | TPrimitives) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const IsPromise: (type: TFunction | z.ZodType | TPrimitives) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const Preprocess: <T>(fn: (arg: unknown) => T) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const ToNumber: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const ToBoolean: (truthful?: unknown[], falsy?: unknown[]) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const IsCustom: (check?: ((data: any) => any) | undefined, params?: string | (Partial<z.util.Omit<z.ZodCustomIssue, "code">> & { fatal?: boolean | undefined; }) | ((input: any) => Partial<z.util.Omit<z.ZodCustomIssue, "code">> & { fatal?: boolean | undefined; }) | undefined, fatal?: boolean | undefined) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator & { optional: () => ReturnType<typeof mate.decorate>; nullable: () => ReturnType<typeof mate.decorate>; nullish: () => ReturnType<typeof mate.decorate>; }; declare const And: (type: TFunction | z.ZodType | TPrimitives) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const Or: (type: TFunction | z.ZodType | TPrimitives) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; declare const ZodPipeline: (opts?: ({ formatError?: ((e: z.ZodError, value: unknown, metas: moost.TPipeMetas<TEmpty>, level: "CLASS" | "PROP" | "PARAM" | "METHOD") => Error) | undefined; } & TZodOpts) | undefined) => TPipeFn<TZodMate>; export { And, Coerce, DateFrom, DateTo, Default, EndsWith, HasLength, Includes, IsAny, IsArray, IsBigint, IsBoolean, IsCuid, IsCuid2, IsCustom, IsDate, IsDatetime, IsDiscriminatedUnion, IsEmail, IsEmoji, IsEnum, IsFinite, IsGt, IsGte, IsInt, IsIntersection, IsIp, IsLiteral, IsLt, IsLte, IsMap, IsMultipleOf, IsNaN, IsNativeEnum, IsNegative, IsNever, IsNonnegative, IsNonpositive, IsNull, IsNullable, IsNullish, IsNumber, IsPositive, IsPromise, IsRecord, IsSafeNumber, IsSet, IsString, IsSymbol, IsTuple, IsUlid, IsUndefined, IsUnion, IsUnknown, IsUrl, IsUuid, IsVoid, LazyType, MatchesRegex, Max, Min, OnCatch, Or, Passthrough, Preprocess, Refine, StartsWith, Strict, Strip, SuperRefine, type TMoostZodType, type TZodFunction, type TZodFunctionDefinition, type TZodMate, type TZodMetadata, ToBoolean, ToNumber, Transform, Trim, Validatable, Zod, ZodPipeline, ZodSkip, getZodMate, getZodType, getZodTypeForProp, validate };