zod#ZodType TypeScript Examples
The following examples show how to use
zod#ZodType.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: date-in-schema.ts From express-zod-api with MIT License | 6 votes |
export class ZodDateIn extends ZodType<Date, ZodDateInDef, string> {
_parse(input: ParseInput): ParseReturnType<Date> {
const { status, ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.string) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.string,
received: ctx.parsedType,
});
return INVALID;
}
if (!isoDateRegex.test(ctx.data as string)) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_string,
validation: "regex",
});
status.dirty();
}
const date = new Date(ctx.data);
if (!isValidDate(date)) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_date,
});
return INVALID;
}
return { status: status.value, value: date };
}
static create = () =>
new ZodDateIn({
typeName: zodDateInKind,
});
}
Example #2
Source File: date-out-schema.ts From express-zod-api with MIT License | 6 votes |
export class ZodDateOut extends ZodType<string, ZodDateOutDef, Date> {
_parse(input: ParseInput): ParseReturnType<string> {
const { status, ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.date) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.date,
received: ctx.parsedType,
});
return INVALID;
}
if (!isValidDate(ctx.data)) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_date,
});
return INVALID;
}
return { status: status.value, value: (ctx.data as Date).toISOString() };
}
static create = () =>
new ZodDateOut({
typeName: zodDateOutKind,
});
}
Example #3
Source File: upload-schema.ts From express-zod-api with MIT License | 6 votes |
export class ZodUpload extends ZodType<UploadedFile, ZodUploadDef> {
_parse(input: ParseInput): ParseReturnType<UploadedFile> {
const { ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.object || !isUploadedFile(ctx.data)) {
addIssueToContext(ctx, {
code: ZodIssueCode.custom,
message: `Expected file upload, received ${ctx.parsedType}`,
});
return INVALID;
}
return OK(ctx.data);
}
static create = () =>
new ZodUpload({
typeName: zodUploadKind,
});
}
Example #4
Source File: file-schema.ts From express-zod-api with MIT License | 5 votes |
export class ZodFile extends ZodType<string, ZodFileDef> {
_parse(input: ParseInput): ParseReturnType<string> {
const { status, ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.string) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.string,
received: ctx.parsedType,
});
return INVALID;
}
for (const check of this._def.checks) {
if (check.kind === "base64") {
if (!base64Regex.test(ctx.data)) {
addIssueToContext(ctx, {
code: ZodIssueCode.custom,
message: check.message,
});
status.dirty();
}
}
}
return { status: status.value, value: ctx.data };
}
binary = (message?: ErrMessage) =>
new ZodFile({
...this._def,
checks: [...this._def.checks, { kind: "binary", ...errToObj(message) }],
});
base64 = (message?: ErrMessage) =>
new ZodFile({
...this._def,
checks: [...this._def.checks, { kind: "base64", ...errToObj(message) }],
});
get isBinary() {
return !!this._def.checks.find((check) => check.kind === "binary");
}
get isBase64() {
return !!this._def.checks.find((check) => check.kind === "base64");
}
static create = () =>
new ZodFile({
checks: [],
typeName: zodFileKind,
});
}