Skip to content
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

JSON type #933

Open
jhnns opened this issue Nov 18, 2024 · 4 comments
Open

JSON type #933

jhnns opened this issue Nov 18, 2024 · 4 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@jhnns
Copy link

jhnns commented Nov 18, 2024

Hello, thanks for this great library 👋

I was wondering if you'd be interested in a documentation for a JSON serializable type. That's the type that you can safely serialize and de-serialize via JSON.parse(JSON.stringify(thing)). Zod mentions it in their documentation.

My suggestion (based on Zod's implementation) would be:

const JsonLiteral = v.union([v.string(), v.number(), v.boolean(), v.null()]);
type JsonLiteral = v.InferOutput<typeof JsonLiteral>;

type Json = JsonLiteral | { [key: string]: Json } | Array<Json>;
const Json = v.lazy(
  (): v.UnionSchema<any, undefined> =>
    v.union([JsonLiteral, v.array(Json), v.record(v.string(), Json)]),
);

Maybe this could even be included in valibot so that people don't need to copy it.

@fabian-hiller
Copy link
Owner

Where would you place to code in the docs?

Here is a JSON schema I wrote a few months ago:

type Json = string | number | boolean | null | { [key: string]: Json } | Json[];
type LiteralIssue = StringIssue | NumberIssue | BooleanIssue | NullIssue;
type JsonIssue = LiteralIssue | ArrayIssue | RecordIssue;
type JsonSchema = BaseSchema<Json, Json, UnionIssue<JsonIssue> | JsonIssue>;
export const jsonSchema = v.lazy(() =>
	v.union([v.string(), v.number(), v.boolean(), v.null_(), v.array(jsonSchema), v.record(v.string(), jsonSchema)])
) as JsonSchema;

@fabian-hiller fabian-hiller self-assigned this Nov 20, 2024
@fabian-hiller fabian-hiller added the documentation Improvements or additions to documentation label Nov 20, 2024
@jhnns
Copy link
Author

jhnns commented Nov 20, 2024

I think I'd expect it under Guides > Advanced.
What do you think about implementing it as valibot action?

@fabian-hiller
Copy link
Owner

We could at it to https://valibot.dev/guides/other/. Feel free to create a PR.

I see and DX advantage of adding a json schema. But I have to think about it because it is a very special kind of schema compared to our other schemas that just mimic TS type functionality. Also, we have had ideas in the past to add something like a json action to validate JSON strings. We need to make sure the naming makes sense. I think this is something I would prefer to investigate after our v1 release.

jhnns added a commit to jhnns/valibot that referenced this issue Nov 21, 2024
Adds documentation for a JSON type schema as discussed in fabian-hiller#933
@jhnns
Copy link
Author

jhnns commented Nov 21, 2024

I've created a PR. valibot.dev/guides/other is the perfect spot, right below lazy :)

I understand that shipping a json schema is a special case that you might want to think about. I think it would be nice 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants