react-router-typesafe-routes
Version:
Enhanced type safety via validation for all route params in React Router v7.
26 lines (25 loc) • 941 B
JavaScript
import { parser, type } from "../lib/index.mjs";
import { StringSchema, DateSchema, NumberSchema, BooleanSchema } from "yup";
function configure({ parserFactory }) {
function yup(schema, parser) {
let typeHint = "unknown";
if (!schema.spec.nullable) {
if (schema instanceof StringSchema) {
typeHint = "string";
}
else if (schema instanceof NumberSchema) {
typeHint = "number";
}
else if (schema instanceof BooleanSchema) {
typeHint = "boolean";
}
else if (schema instanceof DateSchema) {
typeHint = "date";
}
}
return type((value) => schema.validateSync(value), parser !== null && parser !== void 0 ? parser : parserFactory(typeHint));
}
return { yup };
}
const { yup } = configure({ parserFactory: parser });
export { configure, yup };