Replies: 1 comment
-
I was interested in supporting Standard Schema and looked into how to implement it. In conclusion, it's difficult to support. First, I'll explain how Conform validates FormData as a prerequisite.
The rough processing order is as follows. The important thing is the "Schema conversion for validating FormData" in step 2. Since FormData values are all treated as strings, they need to be converted to the type specified by the developer, such as numeric or boolean. Next, the data validation method for Standard Schema is as follows. import type {StandardSchemaV1} from '@standard-schema/spec';
import * as z from 'zod';
export async function standardValidate<T extends StandardSchemaV1>(
schema: T,
input: StandardSchemaV1.InferInput<T>
): Promise<StandardSchemaV1.InferOutput<T>> {
let result = schema['~standard'].validate(input);
if (result instanceof Promise) result = await result;
// if the `issues` field exists, the validation failed
if (result.issues) {
throw new Error(JSON.stringify(result.issues, null, 2));
}
return result.value;
}
const zodResult = await standardValidate(z.string(), 'hello'); As you can see from this code, the validation entity of Standard Schema is Zod, and Standard Schema is merely to standardize Schema validation and results. The problem with this is that the "Schema conversion for validating FormData" process, which explains the process of validating Conform's FormData, requires support for each passed Schema library, even when using Standard Schema. import { useForm } from '@conform-to/react';
import { coerceZodSchema } from '@conform-to/zod'; // new API
import { parseWithStandardSchema } from '@conform-to/standard-schema'; // new Library
import { z } from 'zod';
const schema = z.object({
email: z.string(),
password: z.string(),
});
function ExampleForm() {
const [form, { email, password }] = useForm({
onValidate({ formData }) {
return parseWithStandardSchema(formData, {
schema: coerceZodSchema(schema),
});
},
});
// ...
} If the code shown in the proposal does not provide These questions have arisen, and I believe that support is difficult due to the difficulty of supporting Standard Schema and the demands of developers using Conform. Of course, if you have any good ideas other than mine, please let me know. I would also like to consider implementation. |
Beta Was this translation helpful? Give feedback.
-
https://github.com/standard-schema/standard-schema
Currently,
zod
andyup
are only supported officially byconform
.If the Standard Schema is supported by
conform
, we can utilize the other validation libraries.Beta Was this translation helpful? Give feedback.
All reactions