Comments (9)
This should work with recursive
.
from valibot.
Awesome, I even looked through the code, must have totally overlooked it then, thanks!
Is this the right approach to use it then?
import { BaseSchema, object, recursive, string, parse, Output, merge, array } from "valibot";
const baseCategorySchema = object({
name: string(),
});
type Category = Output<typeof baseCategorySchema> & {
subcategories?: Category[];
};
const categorySchema: BaseSchema<Category> = merge([baseCategorySchema, object({ subcategories: recursive(() => array(categorySchema)) })]);
const result = parse(categorySchema, {
name: "People",
subcategories: [
{
name: "Politicians",
subcategories: [
{
name: "Presidents",
subcategories: [],
},
],
},
],
});
from valibot.
Looks good. Does it work as expected?
from valibot.
Jup seems to work fine. An example like this could probably be added to the docs then.
This is exactly the example from Zod but converted to Valibot.
from valibot.
As soon as I find the time, I'll probably add advanced guides to the docs and include that. Thanks for your contribution!
from valibot.
Random (related) question/confirmation as I check out valibot for the first time :) ...
I was curious to see if the following would be the right approach to parsing a general JSON value with Valibot?
import {
type BaseSchema,
type Output,
union,
boolean,
number,
string,
nullType,
recursive,
array,
record,
} from 'valibot'
export type JsonPrimitive = Output<typeof vJsonPrimitive>
export type Json = JsonPrimitive | { [key: string]: Json } | Json[]
export const vJsonPrimitive = union([number(), boolean(), string(), nullType()])
export const vJson: BaseSchema<Json> = union([
vJsonPrimitive,
recursive(() => array(vJson)),
recursive(() => record(vJson)),
])
Note zod's docs have a similar example (https://github.com/colinhacks/zod#json-type):
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)]))
This is a common use-case... let me know if you are open to adding it to the docs (I am happy to make a PR if I can confirm this is the right approach with valibot :) ).
from valibot.
The code looks good to me. For the docs, the most I would change is some naming. However, at this point I don't know where I would add it to the docs. Right now the focus is not on the docs. But that will change in the next few weeks. It might even make sense to add a json
schema to Valibot instead of adding this functionality to the docs.
from valibot.
Also, it might be interesting to add some kind of json
validation function to be able to check in a string pipeline if a input corresponds to stringified JSON.
from valibot.
Lazy (formerly recursive) schemas are now documented here:
from valibot.
Related Issues (20)
- `intersect` of `object` and `record` does not work like Typescript HOT 9
- using `union` as key of `record` adds `undefined` to the value type HOT 13
- `isoTimestamp` only accepts UTC timestamps HOT 3
- Not able to validate file uploads on the backend, even though it works on the frontend HOT 4
- Search fails to work HOT 1
- Timestamp regex doesn't support timezone other than Zulu HOT 1
- Can't use Valibot with H3 Data validation HOT 6
- optional string field with string min/max options set wrong meta in veevalidate HOT 2
- How to conditionally require object with all its fields HOT 2
- `<select>` custom validation inside a `variant` schema HOT 4
- Function to support for json type HOT 2
- Number literal don't trigger variant validation HOT 7
- `_addIssue` blocks `every`, `some` & `check` actions rewrite due to a possible type issue HOT 2
- Email regex pattern does not allow all valid characters HOT 1
- Are there any similarities about zod `.superRefine`? HOT 1
- Error when building HOT 6
- How to access other field? HOT 2
- Allow null default value while enforcing required validation HOT 1
- Force typeing on object schema HOT 2
- Investigate memory, performance and bundle size impact when caching basic schema objects
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.