Giter VIP home page Giter VIP logo

Comments (9)

fabian-hiller avatar fabian-hiller commented on May 13, 2024 1

This should work with recursive.

from valibot.

ZerNico avatar ZerNico commented on May 13, 2024 1

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.

fabian-hiller avatar fabian-hiller commented on May 13, 2024

Looks good. Does it work as expected?

from valibot.

ZerNico avatar ZerNico commented on May 13, 2024

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.

fabian-hiller avatar fabian-hiller commented on May 13, 2024

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.

firxworx avatar firxworx commented on May 13, 2024

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.

fabian-hiller avatar fabian-hiller commented on May 13, 2024

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.

fabian-hiller avatar fabian-hiller commented on May 13, 2024

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.

fabian-hiller avatar fabian-hiller commented on May 13, 2024

Lazy (formerly recursive) schemas are now documented here:

from valibot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.