Comments (9)
- 1
but, I would also enjoy the other way around - json-schema to valibot schemas - perhaps we can leverage existing tools around zod/arktype for this...
from valibot.
Thank you, thats great! I plan to merge PR #211 this week or next. If your library works reliably, feel free to add it to our ecosystem page.
Ok my tool/library to convert to JSON schema is now pretty complete thanks to the new reflections API on pipe validation!
I opened a PR to add it to the ecosystem page #263
from valibot.
It's on my list to check out a possible implementation. Since I'm currently writing my bachelor's thesis, I won't get to it until September.
from valibot.
Maybe https://github.com/sinclairzx81/typebox could be used as inspiration?
from valibot.
I've started to build myself a small CLI tool to convert from Valibot to JSON schema:
https://github.com/gcornut/valibot-json-schema
It loads a JS/TS module with esbuild-runner
and manually converts the exported Valibot schemas into JSON schema. For my use case I don't mind having a big JS module doing the conversion of all schemas but I totally see how that would be a problem in a modular library like Valibot.
Like you say, some more metadata on schema validations (minLength, email, etc.) would help. In the meantime I might manually attach some custom metadata.
Something like:
import { withJSONSchemaFeatures } from '@gcornut/valibot-json-schema/extension';
const MyArray = withJSONSchemaFeatures(array([minLength(1)]), { minItems: 1 });
// => returns the ArraySchema with a custom Symbol keyed property used by internally during conversion to JSON schema
It's redundant but it's could be a cheap & easy way to do this 🤔
from valibot.
Thank you, thats great! I plan to merge PR #211 this week or next. If your library works reliably, feel free to add it to our ecosystem page.
from valibot.
A native Valibot to JSON schema or JSON schema to Valibot implementation is not currently planned. Technically, however, it is largely possible through our new reflection API and can be covered by third-party packages like valibot-json-schema. If JSON Schema is important to you, I suggest you take a look at TypeBox.
I am therefore closing this issue. However, you can still contact me through this issue if you have any questions or feedback.
from valibot.
There is one existing solution to convert from valibot to json schema. You can generate json schema from the TS types with ts-json-schema-generator
.
Of course it's not a perfect solution because some of the features of valibot do not translate to TS types but would translate to JSON schema (like minLength
to constrain array size).
An example on how to use this:
- define a schema
src/schema.ts
import { object, literal, type Input } from 'valibot';
const FooElementSchema = object({ type: literal('foo') });
export type FooElement = Input<typeof FooElementSchema>;
- then execute
npx ts-json-schema-generator --path src/schema.ts -t FooElement -o dist/schema.json
The result:
{
"$ref": "#/definitions/FooElement",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"FooElement": {
"additionalProperties": false,
"description": "Input inference type.",
"properties": {
"type": {
"const": "foo",
"description": "Input inference type.",
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
}
}
}
from valibot.
Thanks for the tip. Here is what I can currently say about this issue: We are working in PR #211 to make the properties of the schemas and validation functions accessible. This would theoretically allow direct serialization. However, it is important to note that serialization is not a goal of Valibot. This has to do with the fact that Valibot prioritizes functionality, bundle size and DX over serialization, since not every functionality can be serialized. Also, validating against a JSON schema as in TypeBox prevents tree shaking and code splitting. In the long run, however, I can imagine offering a JSON variant with the same API under valibot/json
.
from valibot.
Related Issues (20)
- [Proposal] Considering Biome for formating and linting HOT 1
- Branded schema doesn't provide the type guard thru the `is`. HOT 2
- Add CIDR Schema Type HOT 3
- Reporting of Errors (ValiError Format compared with ZodError Format) HOT 3
- Nullable, how can i do
- isoTimestamp only supports decimal fraction up to 3 digits HOT 8
- Cannot use `brand` with `url` or `email` HOT 1
- Document minimum typescript version HOT 7
- Nextjs build Failed HOT 6
- How can I define a schema so that it can be validated as a function? HOT 1
- getDefaults returns undefined for optional with a default of empty string or false HOT 2
- The validation of the object is not rigorous enough HOT 3
- Optional Objects with Literal Fields and Raw Default Result in "| undefined" Type HOT 1
- union(): Union wrong behavior when type validation pass HOT 2
- A GPT companion for the Zod to Valibot conversion HOT 1
- `union()` of object with overlapping field drops other fields HOT 4
- Supporte Metadata like description
- Support non-string array in `picklist` HOT 4
- customAsync not resolved inside issues? HOT 4
- Change omit() parameter type to `readonly TKeys[]` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from valibot.