Giter VIP home page Giter VIP logo

Comments (7)

gcanti avatar gcanti commented on May 12, 2024 2

I think I got a repro

export function withDefault<T extends t.Any>(type: T, defaultValue: t.TypeOf<T>): t.Type<t.InputOf<T>, t.TypeOf<T>> {
  return new t.Type(
    `withDefault(${type.name}, ${JSON.stringify(defaultValue)})`,
    type.is,
    (v, c) => type.validate(v != null ? v : defaultValue, c),
    type.serialize
  )
}

const T1 = t.partial({
  name: withDefault(t.string, 'foo')
})

console.log(t.validate({}, T1).fold(() => 'error', a => JSON.stringify(a)))
// => `{}`

Related commit 37c74a5

My opinion is that it's better to give the passed type a chance to perform a deserialization and, in case of failure, fallback to undefinedType. I'll revert that change.

from io-ts.

safareli avatar safareli commented on May 12, 2024 2

This is the version for decoders that I use:

export function withDefault<T extends D.Decoder<unknown, unknown>>(
  decoder: T,
  defaultValue: D.TypeOf<T>
): D.Decoder<D.InputOf<T>, D.TypeOf<T>> {
  return D.union(
    decoder,
    pipe(
      undefinedDecoder,
      D.map(() => defaultValue)
    )
  );
}

export const undefinedDecoder = {
  decode: (val: unknown) => {
    return val === undefined
      ? D.success(undefined)
      : D.failure(val, "undefined");
  },
};

from io-ts.

gcanti avatar gcanti commented on May 12, 2024

I think that is already possible

function withDefault<T extends t.Any>(type: T, defaultValue: t.TypeOf<T>): t.Type<t.TypeOf<T>> {
  return new t.Type(
    type.name,
    (v, c) => type.validate(v != null ? v : defaultValue, c)
  )
}

from io-ts.

gyzerok avatar gyzerok commented on May 12, 2024

Yeah, that make sense. I would try it as you described in my code and see if it's helpful. If it is, I guess we can discuss making it part of API.

from io-ts.

gunzip avatar gunzip commented on May 12, 2024

while this worked for properties in partial types as well until 0.9.1, this does not validate / populate default values anymore in t.partial(s) because of some changes in 0.9.2. this was a breaking change for us :)

ref.

https://github.com/teamdigitale/digital-citizenship-functions/blob/tech-debt/lib/utils/default.ts
https://github.com/teamdigitale/digital-citizenship-functions/blob/tech-debt/lib/utils/__tests__/default.test.ts

from io-ts.

gcanti avatar gcanti commented on May 12, 2024

@gunzip links are broken, could you please provide a test case?

from io-ts.

gunzip avatar gunzip commented on May 12, 2024

sorry, here are the correct links:

https://github.com/teamdigitale/digital-citizenship-functions/blob/master/lib/utils/default.ts

https://github.com/teamdigitale/digital-citizenship-functions/blob/master/lib/utils/__tests__/default.test.ts

from io-ts.

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.