Giter Club home page Giter Club logo

Comments (2)

realtebo avatar realtebo commented on July 20, 2024 1

I installed zod-to-ts to diagnose the problem.

Actually, having used coerce for the boolean, every missing property is mapped as a boolean false. and the ts types are all optionals (there is a ? added after the field name in every coerced field)

We resolved using discriminatedUnion, thanks for the tip!

I kindly suggest to add a tip in the documentation about this behaviour.

from zod.

eswarty avatar eswarty commented on July 20, 2024

Hi! Not totally sure what is causing the problem with the union type, but I think a discriminated union might work instead, e.g.:

const hammamAlarmEventFieldDataPayloadSchema = z.object({
  type: z.literal("hamman"),
  BoilerFullTooSoon: z.coerce.boolean(),
  EvLoad: z.coerce.boolean(),
  EvDrain: z.coerce.boolean(),
  VinBus: z.coerce.boolean(),
  Ntc: z.coerce.boolean(),
  Fan: z.coerce.boolean(),
  Slave: z.coerce.boolean(),
  HeaterFail: z.coerce.boolean(),
  Capacitive: z.coerce.boolean(),
  CabTemp: z.coerce.boolean(),
  Door: z.coerce.boolean(),
});

const saunaAlarmEventFieldDataPayloadSchema = z.object({
  type: z.literal("sauna"),
  StoveTemp: z.coerce.boolean(),
  VinBus: z.coerce.boolean(),
  Ntc: z.coerce.boolean(),
  ThermalProtection: z.coerce.boolean(),
  CabTemp: z.coerce.boolean(),
  Potentiometer: z.coerce.boolean(),
});

const eccIotInfoSchema = z.object({
  Id: z.string(),
  Ts: z.coerce.date(),
  Type: z.nativeEnum(FieldDataType),
  HostId: z.string(),
  Payload: z.discriminatedUnion("type", [
    saunaAlarmEventFieldDataPayloadSchema,
    genericAlarmEventFieldDataPayloadSchema,
    hammamAlarmEventFieldDataPayloadSchema,
  ]),
});


export const rawEventFieldDataSchema = z.object({
  App: z.string(),
  Cat: z.nativeEnum(EventCategory),
  EccIOTEvent: eccIotInfoSchema,
});

export type RawEventFieldData = z.infer<typeof rawEventFieldDataSchema>;
const body = {
    App: "EccIOT",
    Cat: "Event",
    EccIOTEvent: {
      Id: "ecc-D8E39657C",
      Ts: "2020/02/28 10:14:10",
      Type: "HAlarm",
      HostId: "0A229332",
      Payload: {
        type: "hamman",
        BoilerFullTooSoon: 0,
        EvLoad: 0,
        EvDrain: 0,
        VinBus: 0,
        Ntc: 0,
        Fan: 0,
        Slave: 0,
        HeaterFail: 0,
        Capacitive: 0,
        CabTemp: 0,
        Door: 0,
      },
    },
  };

  const rawData: RawEventFieldData = rawEventFieldDataSchema.parse(body);

  console.log(rawData);

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.