Type safe output
What would be the benefit of building a validation library without a type safe output?
Inspired by the Zod
parse output type, Regle
will also infer your validator types to know which properties are guaranteed to be defined.
r$.$validate
Using r$.$validate
will asynchronously run and wait for all your validators to finish, and will return an object containing the status of your form.
ts
const { result, data } = await r$.$validate();
If your result is true
, the data will be type safe.
It will check if the required
rule is present, but it will not work with requiredIf
or requiredUnless
, because we can't know the condition at build time.
ts
type Form = {
firstName?: string;
lastName?: string;
}
const form: Ref<Form> = ref({ firstName: '', lastName: '' })
const { r$ } = useRegle(form, {
lastName: { required },
});
async function submit() {
const { result, data } = await r$.$validate();
if (result) {
console.log(data);
}
}