add types and schemas
This commit is contained in:
34
src/common/schemas.ts
Normal file
34
src/common/schemas.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { isValid, parse } from "date-fns";
|
||||
import z from "zod";
|
||||
|
||||
export const timeSchema = z
|
||||
.string("Must be string.")
|
||||
.nonempty("Must not be empty.")
|
||||
.transform((val, ctx) => {
|
||||
const parsedDate = parse(val, "HH:mm", new Date());
|
||||
if (!isValid(parsedDate)) {
|
||||
ctx.issues.push({
|
||||
code: "custom",
|
||||
message: "Must be in 'HH:mm' format.",
|
||||
input: val,
|
||||
});
|
||||
return z.NEVER;
|
||||
}
|
||||
return val;
|
||||
});
|
||||
|
||||
export const dateSchema = z
|
||||
.string("Must be string.")
|
||||
.nonempty("Must not be empty.")
|
||||
.transform((val, ctx) => {
|
||||
const parsedDate = parse(val, "YYYY-MM-DD", new Date());
|
||||
if (!isValid(parsedDate)) {
|
||||
ctx.issues.push({
|
||||
code: "custom",
|
||||
message: "Must be in 'YYYY-MM-DD' format.",
|
||||
input: val,
|
||||
});
|
||||
return z.NEVER;
|
||||
}
|
||||
return val;
|
||||
});
|
||||
Reference in New Issue
Block a user