-
Notifications
You must be signed in to change notification settings - Fork 142
chore: add json example #686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add json example #686
Conversation
|
|
Just pushed a commit to your PR. There is no JSON field in Conform. Conform only understand string or File in an input. You will need to serialize it manually and make sure it to be serialized again when you access and update the field value (e.g. It might be worth adding an option to the |
|
@edmundhung is there anyway to use the jsonSchema type I had? for my use case I want to allow someone to add any JSON data because we're mapping it back to a users DB so we don't know the shape of the data. |
|
I have tested it once with your original type and it seems to recursively looping over and over again and crash. I am not sure why. It might be either a bug on the |
|
@edmundhung yep the jsonSchema type is in the GitHub docs of zod and works as expected https://github.com/colinhacks/zod?tab=readme-ov-file#json-type |
|
@lifeiscontent I have pushed up a fix to your branch. Let me know if this is what you were looking for. |
|
@edmundhung is there a way I can try a prerelease version of this quickly? I can try it in the project I'm working in and get back to you. |
1ead944 to
3c043ee
Compare
|
commit: @conform-to/dom
@conform-to/react
@conform-to/validitystate
@conform-to/yup
@conform-to/zod
|
Here you go ☝🏼 |
|
@edmundhung it works perfectly! :) |
|
@edmundhung any chance the work you did here could get merged? |
|
Just following up on this. It seems like this implementation will break people using union for other cases. But we have a solution that is actually working already: For example, we can create a JSON schema like this: // Copied from https://github.com/colinhacks/zod?tab=readme-ov-file#json-type
function getJsonSchema() {
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
type Literal = z.infer<typeof literalSchema>;
type Json = Literal | { [key: string]: Json } | Json[];
const jsonSchema: z.ZodType<Json> = z.lazy(() =>
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
);
// Wrap the JSON schema in `z.custom`
return z.custom<Json>(json => jsonSchema.safeParse(json).success, 'Invalid JSON');
}This works because Conform will no longer try to reconstruct the JSON schema with the additional coercion logic and so it should works as zod normally does. I will update this PR to add some tests for |
|
@lifeiscontent Just curious if #871 solves what you need, or is there anything else you want there. Let me know. |
|
@edmundhung yes, I think it should be fine |
|
Closing as this is already addressed with #871. |
@edmundhung can you explain to me how I might be able to get a JSON field working with conform? I've setup this example, but the
.defaultValueof the input renders as a blank input value