Comments (6)
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.
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.
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.
@kristianmandrup it fixed the ref errors for me. Any ideas when the above work will be on a tagged release?
from schema-to-yup.
I'll release it today.
from schema-to-yup.
[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)
- Required Logic broken? HOT 8
- strict uppercase doesn't work with minLength and maxLength HOT 7
- handling trailing and leading spaces, `trim` doesn't work ? HOT 1
- Get all properties under a schema HOT 1
- Custom error messages not having any effect HOT 4
- Custom message for type error HOT 18
- which version of yup does schema-to-yup support HOT 3
- "isNullable" is not working for Object type HOT 3
- Update yup to the newest 1.x version HOT 2
- Previously working required message, drops when combining refValueFor
- Previously working required message, drops when combining refValueFor HOT 1
- `ensure` on array does not work/missing example HOT 2
- Tests fail on fresh clone and install of dependencies HOT 3
- There is no regexp function that validate using a pattern which covers Unicode
- `TypeError: Cannot add property required, object is not extensible` on Node server using `esm` HOT 1
- Latest daft version `2020-12` is not supported HOT 2
- Question : why nullable and not type: ["null", ...] ? HOT 7
- Missing support for 'integer' HOT 11
- Multiple when causing Cyclic errors HOT 4
- TypeError: Invalid attempt to spread non-iterable instance.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from schema-to-yup.