zod icon indicating copy to clipboard operation
zod copied to clipboard

Export `RawCreateParams` and `processCreateParams` to allow custom type definitions?

Open prescience-data opened this issue 3 years ago • 1 comments

I'm attempting to build out a series of local Zod type definitions and following the pattern from types.ts

However, I've hit a snag not being able to access some of the final elements, specifically RawCreateParams and processCreateParams.

Example

export enum ZodThirdPartyTypeKind {
  ZodSymbol = "ZodSymbol"
}

type ZodSymbolCheck =
  | {
      kind: "description"
      value: string
      message?: string
    }
  | {
      kind: "prefix"
      value: string
      message?: string
    }

export interface ZodSymbolDef extends ZodTypeDef {
  checks: ZodSymbolCheck[]
  typeName: "ZodSymbol"
}

export class ZodSymbol extends ZodType<symbol, ZodSymbolDef> {
  public _parse(input: ParseInput): ParseReturnType<any> {
    const parsedType = this._getType(input)
    if (parsedType !== ZodParsedType.symbol) {
      const ctx = this._getOrReturnCtx(input)
      addIssueToContext(ctx, {
        code: ZodIssueCode.invalid_type,
        expected: ZodParsedType.symbol,
        received: ctx.parsedType
      })
      return INVALID
    }

    const status = new ParseStatus()
    let ctx: undefined | ParseContext = undefined
    for (const check of this._def.checks) {
      switch (check.kind) {
        case "description":
          if (input.data.description !== check.value) {
            ctx = this._getOrReturnCtx(input, ctx)
            addIssueToContext(ctx, {
              code: ZodIssueCode.custom,
              message: check.message
            })
            status.dirty()
          }
          break
        case "prefix":
          if (input.data.description?.startsWith(check.value)) {
            ctx = this._getOrReturnCtx(input, ctx)
            addIssueToContext(ctx, {
              code: ZodIssueCode.custom,
              message: check.message
            })
            status.dirty()
          }
          break
        default:
          util.assertNever(check as never)
      }
    }

    return { status: status.value, value: input.data }
  }

  public static create = (
    params?: RawCreateParams // MISSING!
  ): ZodSymbol =>  
    new ZodSymbol({
      checks: [],
      typeName: ZodThirdPartyTypeKind.ZodSymbol,
      ...processCreateParams(params)  // MISSING!
    })
}


export const symbolType = ZodSymbol.create

prescience-data avatar Jun 26 '22 08:06 prescience-data

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 30 '22 23:08 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 31 '22 08:10 stale[bot]

Could we please reopen this issue. We hit the same problem: We want to add our local ZodFile type for validating Files (We use this together with <input type="file" />. Everything works fine, but we are missing the processCreateParams.

Thanks a lot and also for this awesome library <3

kaciakmaciak avatar Jun 13 '23 11:06 kaciakmaciak