Coder Social home page Coder Social logo

Comments (2)

woifes avatar woifes commented on June 18, 2024 1

Hello and thank you for the response! This is exactly what I am looking for. I achvied to miss the underlying property somehow...

Mostly I use it for definitons. If Package A defines a really generic one I want to specialize the definitions in a depending package.

In your first example it is not really the same since I am then able to have a configuration without the "A"s (so it is smaller and less repetitive) and fill up the "A"s incode to later delegate it. But if I need more configuration (like a "C") then it is added automatically.

Thank you very much!

from runtypes.

yuhr avatar yuhr commented on June 18, 2024

There's no straightforward way to do it like that. Constraint (and also Intersect) runtype only knows about its underlying runtype (intersectees for Intersect), so if you want to manipulate the underlying one, you have to unwrap first and then rewrap it by a constraint function again, e.g.

const myRuntype = Record({
    A: Number,
    B: Number,
}).withConstraint((r) => {
    return r.A > r.B || `A has to be bigger than B`;
});

const myOtherRuntype = myRuntype.underlying.omit("A").withConstraint((r) => {
    if (myRuntype.underlying.pick("A").guard(r)) {
        return r.A > r.B || `A has to be bigger than B`;
    } else {
        return `A has to be present and a number`;
    }
});

But I don't see the motivation of this, as the above myRuntype and myOtherRuntype are essentially the same.

Optionalizing a property can be done by:

myRuntype.underlying.omit('A').extend({ A: myRuntype.underlying.fields["A"].optional() }).withConstraint(...)

Setting an optional property to required is, if we have:

const myRuntype = Record({
    A: Number.optional(),
    B: Number,
}).withConstraint(...);

then:

myRuntype.underlying.extend({ A: myRuntype.underlying.fields["A"].underlying }).withConstraint(...)

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.