Coder Social home page Coder Social logo

Comments (6)

kristianmandrup avatar kristianmandrup commented on August 17, 2024

I'll have a look this weekend. Thanks for raising the issue. In the meantime you can play with the various "hooks" for customisation or see if other simpler examples work as expected and documented.

from schema-to-yup.

kristianmandrup avatar kristianmandrup commented on August 17, 2024

I would also recommend that you clone the repo and run the conditional (when) tests to see if you can duplicate it. Then you will get an error to where in the actual source code it fails. The output above is from the compiled distribution

from schema-to-yup.

kristianmandrup avatar kristianmandrup commented on August 17, 2024

Just ran npx jest schema-when-then.test

Error seems to be in src/constraint-builder.js:119:21

Adding a console log just before

    this.onConstraintAdded({ name: constraintName, value: values });
    console.log(constraintFn, { values });
    const newBase = constraintFn(values, errFn);  // <--- error
    return newBase;

I get this:

  console.log src/constraint-builder.js:119
    [Function: bound when] { values: [ 'isBig', { is: true, then: [NumberSchema] } ] }

The test output TypeError: ref must be a string, got: [object Object]

when  nameCountjson  encountered a declaration exception

    TypeError: ref must be a string, got: [object Object]

      117 | 
      118 |     this.onConstraintAdded({ name: constraintName, value: values });
    > 119 |     const newBase = constraintFn(values, errFn);
          |                     ^
      120 |     return newBase;
      121 |   }
      122 | 

      at new Reference (node_modules/yup/lib/Reference.js:25:40)
      at node_modules/yup/lib/mixed.js:433:14
          at Array.map (<anonymous>)
      at NumberSchema.when (node_modules/yup/lib/mixed.js:432:32)
      at ConstraintBuilder.constraintFn [as multiValueConstraint] (src/constraint-builder.js:119:21)
      at ConstraintBuilder.multiValueConstraint [as build] (src/constraint-builder.js:61:24)
      at ConstraintBuilder.build [as addConstraint] (src/constraint-builder.js:140:29)
      at YupNumber.addConstraint (src/types/mixed/mixed.js:223:47)
      at YupNumber.addConstraint [as when] (src/types/mixed/mixed.js:310:12)

Looking at the API contract for yup when

So it is essentially calling yup.when with [ 'isBig', { is: true, then: [NumberSchema] } ] as the value, when it should be ('isBig', { is: true, then: [NumberSchema] } ).

So it simply needs spreading the values in this case (ie. ...values). Problem was that I had tried to generalise the API calls to yup methods and assumed all cases taking multiple value arguments (ie multiValueConstraint(values, { constraintFn, constraintName, errFn }) would pass the value array in full to constraintFn (pointer to constraint method on yup instance).

It needs a way to know when the constraintFn should spread the values in the call.

So looking for the call to multiValueConstraint you will find:

    const constrOpts = {
      constraintName,
      yup,
      constraintFn,
      errFn
    };

    const multi = this.multiValueConstraint(values, constrOpts);

We can lookup the constraintName in a table that have entries for special case functions such as for when

There is also a problem with this logic. Should only execute the first one that returns a value

    const multi = this.multiValueConstraint(values, constrOpts);
    const present = this.presentConstraintValue(constraintValue, constrOpts);
    const nonPresent = this.nonPresentConstraintValue(
      constraintValue,
      constrOpts
    );

    const newBase = multi || present || nonPresent;

It should instead be sth like:

    const constrainFnNames = ['multiValueConstraint', 'presentConstraintValue', 'nonPresentConstraintValue']
    let newBase
    for (let name of constrainFnNames) {
      newBase = this[name](values, constrOpts)
      if (newBase) break;
    }

    if (newBase) {

I've done this work in the fix-when branch which I've now also merged with master. Should get us close to a fully working, more flexible and correct infrastructure. I no longer crashes, but looks like it is still not validated correctly. Need to go more in-depth, see the test/conditions tests and compare with manual calls of the same yup.when tests directly.

from schema-to-yup.

richardhealy avatar richardhealy commented on August 17, 2024

@kristianmandrup it fixed the ref errors for me. Any ideas when the above work will be on a tagged release?

from schema-to-yup.

kristianmandrup avatar kristianmandrup commented on August 17, 2024

I'll release it today.

from schema-to-yup.

kristianmandrup avatar kristianmandrup commented on August 17, 2024

[email protected] has just been released.

Also released a [email protected] to start a new minor iteration as it looks more stable now.

from schema-to-yup.

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.