colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.91k stars 1.18k forks source link

ZodSchema type issue #625

Closed alimansoor97 closed 3 years ago

alimansoor97 commented 3 years ago

I working on a typescript function which accepts zod schema and a model which will be validated.

this is the schema which i will parse later in the code below

export const postClient = z.object({ max_budget: z.number().min(0), max_no_offer: z.number().min(0), expires_in: z.date(), discount: z.number().min(0), buy_quantity: z.number().min(0), get_quantity: z.number().min(0), expire_type: z.string().min(0), branches: z.array(zToObjectId), name: z.string(), type: z.string(), items: z.array(zToObjectId), name_ar: z.string(), items_options: z.array(zToObjectId).nullable(), })

I also created a type based on this schema export type OfferClientT = z.infer<typeof postClient>

export interface JSONValid<Model, Schema> { schema: ZodSchema<Schema> model: Model }

export function jsValidate<T = any, TT = any>({ data, console }: Args<JSONValid<T, T>>): Results<TT> { let bool: Boolean = false let report: ReportLog = { code: generateid(), message: "", err: null, valid: false, functionDetails: funcName() } let dataRt try { dataRt = data.schema.parse(data.model) bool = true report.valid = true report.message = "Parsing zod schema was successful" } catch (err) { logging(err, console) report.message = "Failed to validate the json schema using zod" report.err = err //TODO: send email with the err to me :> } return { bool: bool, data: dataRt, report, } }

const { bool, data, report } = jsValidate<OfferClientT, OfferClientT>({ data: { model: req.body, schema: postClient }, console: true, })

The issue is here, whenever i try to add the zodschema into the schema key i get this error:

Type 'ZodObject<{ max_budget: ZodNumber; max_no_offer: ZodNumber; expires_in: ZodDate; discount: ZodNumber; buy_quantity: ZodNumber; get_quantity: ZodNumber; ... 6 more ...; items_options: ZodNullable<...>; }, "strip", ZodTypeAny, { ...; }, { ...; }>' is missing the following properties from type 'ZodType<{ type?: string; max_budget?: number; max_no_offer?: number; expires_in?: Date; discount?: number; buy_quantity?: number; get_quantity?: number; expire_type?: string; branches?: (string | ObjectId)[]; name?: string; items?: (string | ObjectId)[]; name_ar?: string; items_options?: (string | ObjectId)[]; }, Zo...': _parseInternal, _parseInternalOptionalParams, _parseWithInvalidFallbackts(2739) index.ts(22, 3): The expected type comes from property 'schema' which is declared here on type 'JSONValid<{ type?: string; max_budget?: number; max_no_offer?: number; expires_in?: Date; discount?: number; buy_quantity?: number; get_quantity?: number; expire_type?: string; branches?: (string | ObjectId)[]; name?: string; items?: (string | ObjectId)[]; name_ar?: string; items_options?: (string | ObjectId)[]; }, ...'

scotttrinh commented 3 years ago

@alimansoor97

I think we can definitely help you out here, but do you mind moving all of this into something like TypeScript Playground or CodeSandbox? It's a little hard to follow the code you posted above.