Giter VIP home page Giter VIP logo

Comments (2)

colinhacks avatar colinhacks commented on August 20, 2024 1

I don't want to do this at the moment, since it would mark a subtle shift from being a "parse" tool into a "cast" tool. Casting gets messy quickly what with default values, required/non-required keys, recursive/cyclical types, primitive castings, etc. I always found yup/joi casting difficult to reason about.

I'd recommend building your own a default-setting + validation "pipeline" by combining Zod with something like lodash. You can use_.defaultsDeep() to set defaults on an unvalidated object, like so:

import * as _ from 'lodash';
import * as z from "zod";

const dogDefaults = {
  breed: "Unknown"
}

const Dog = z.object({
  breed: z.string(),
  age: z.number()
});
type Dog = z.TypeOf<typeof Dog>;

const unknownDog: any = { /* whatever */ };

const cleanAndValidate = (rawData: unknown)=>{
  return Dog.parse(_.defaultsDeep(unknownDog, dogDefaults)); 
}

const validDog = cleanAndValidate(unknownDog); 
// typeof validDog => Dog

from zod.

tuchk4 avatar tuchk4 commented on August 20, 2024

I would like to push defaults feature into the zod.

zod is really nice tool, but it is hard to use it without the default values.

For example:

  1. There is the schema:
const LibrarySchema = z.object({
  namespace: z.string().optional(),
  components: z.record(
    z.object({
      names: z.string(),
    }),
  ).optional(),
});

type ILibrarySchema = z.infer<typeof LibrarySchema>;
  1. ILibrarySchema equals to:
type ILibrarySchema = {
    namespace?: string | undefined;
    components?: Record<string, {
        names: string;
    }> | undefined;
}

so all fields are optional, as expected.

  1. Then I would like to fill schema with the default values
const parsed = LibrarySchema.parse(raw);
const config = withDefaults(parser);

Here is the problem. I can't correctly write the type for withDefaults function.

const withDefaults = (config:ILibrarySchema): ???  => { ... };

It is not possible to return ILibrarySchema since it has optional fields, but I would like to return config with the default values, which means, that some fields should not be optional.


I expect defaults to be:

const LibrarySchema = z.object({
  namespace: z.string().optional().default('_'),
  components: z.record(
    z.object({
      names: z.string(),
    }),
  ).optional().default({}),
});

type ILibrarySchema = z.infer<typeof LibrarySchema>;

ILibrarySchema to be:

type ILibrarySchema = {
    namespace: string
    components: Record<string, {
        names: string;
    }>;
}

cc @vriad

from zod.

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.