zod#ParseReturnType TypeScript Examples
The following examples show how to use
zod#ParseReturnType.
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 |
_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 };
}
Example #2
Source File: date-out-schema.ts From express-zod-api with MIT License | 6 votes |
_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() };
}
Example #3
Source File: file-schema.ts From express-zod-api with MIT License | 6 votes |
_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 };
}
Example #4
Source File: upload-schema.ts From express-zod-api with MIT License | 6 votes |
_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);
}