Coder Social home page Coder Social logo

Comments (4)

Deco avatar Deco commented on June 9, 2024 1

I just tried my own suggestion and it worked.

const {Boolean, Number, String, Literal, Array, Record, Union, Optional, Null, Lazy} = require("runtypes");

const Person = Lazy(() =>
    Record({
        name: String,
        partner: Optional(Person.Or(Null)),
    })
);

try {
    const jsonBlob1 = JSON.parse(`{ "name": "Bob", "partner": { "name": "Janet" }}`);
    Person.check(jsonBlob1);
    console.info("Scenario 1 succeeded!");
} catch (e) {
    console.error(`Scenario 1 failed: ${e}`);
}

try {
    const jsonBlob2 = JSON.parse(`{ "name": "Bob", "partner": null }`);
    Person.check(jsonBlob2);
    console.info("Scenario 2 succeeded!");
} catch (e) {
    console.error(`Scenario 2 failed: ${e}`);
}

try {
    const jsonBlob3 = JSON.parse(`{ "name": "Bob" }`);
    Person.check(jsonBlob3);
    console.info("Scenario 3 succeeded!");
} catch (e) {
    console.error(`Scenario 3 failed: ${e}`);
}
Scenario 1 succeeded!
Scenario 2 succeeded!
Scenario 3 succeeded!

The order of operations here is a bit of a gotcha. Perhaps even warning worthy?

from runtypes.

JakeShirley avatar JakeShirley commented on June 9, 2024 1

Excellent, thanks for the quick responses! This makes total sense, if your example @yuhr isn't in the README/wiki, I'd suggest adding it. It made it very clear!

from runtypes.

Deco avatar Deco commented on June 9, 2024

Try switching the order of optional and Or Null, and let us know how it goes:
So instead of Optional(Person).Or(Null),, try Optional(Person.Or(Null)).

from runtypes.

yuhr avatar yuhr commented on June 9, 2024

Yeah, if you want mark your property to be optional (i.e. able to be absent), Optional must be the outmost wrapper of your property. Optional(runtype) is no different from Union(runtype, Undefined) in any places other than direct children of a Record. If you wrote partner: Optional(Person).Or(Null) it's just equivalent to partner: Union(Optional(Person), Null) and then partner: Union(Union(Person, Undefined), Null), so it means the property must be present and be Person | undefined | null.

To summarize the correspondence of the runtypes to the TS's types:

  • Record({ property: Optional(String).Or(Null) }): { property: string | null | undefined }
  • Record({ property: Optional(String.Or(Null)) }): { property?: string | null | undefined }

from runtypes.

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.