-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support of 'when' #14
Comments
Could you provide an example for the input and output you expect. |
Please add whatever you need. It should be structured to make it easy to extend as needed. Currently only supports "core" functionality, not all that JSON schema can express. |
Well, at least something simple like:
What do you think of this? |
It’s doable. Any pull requests are welcome. But no one can make promises when it will be implemented otherwise. I need to do some work on the library but there are some more pressing issues at first. |
I will work on this today and add support for some basic conditions |
Working on branch here: https://github.com/kristianmandrup/schema-to-yup/tree/when-condition |
Should be enough foundation to finish simple support for |
Almost got a basic implementation working. Hope that helps you. |
See describe("manual setup", () => {
var inst = yup.object({
isBig: yup.boolean(),
count: yup.number().when("isBig", {
is: true,
then: yup.number().min(5),
otherwise: yup.number().min(0)
})
});
test("validate", () => {
const result = inst.validateSync({
isBig: true,
count: 10
});
console.log({ result });
expect(result).toBe(true);
});
});
describe("use WhenCondition", () => {
const { constraint } = whenCondition;
console.log({ constraint });
const count = yup.number().when(constraint);
var inst = yup.object({
isBig: yup.boolean(),
count
});
test("validate", () => {
const result = inst.validateSync({
isBig: true,
count: 10
});
console.log({ result });
expect(result).toBe(true);
});
}); Just need to make when() {
const when = this.constraints.when;
if (!isObjectType(when)) return this;
const { constraint } = this.createWhenConditionFor(when);
if (!constraint) {
this.warn(`Invalid when constraint for: ${when}`);
return this;
} else {
this.logInfo(`Adding when constraint for ${this.key}`, constraint);
// use buildConstraint or addConstraint to add when constraint (to this.base)
this.addConstraint("when", { values: constraint, errName: "when" });
}
return this;
} |
This feature is ~90-95% done now. |
Greatly improved design now. Major refactoring, loads of unit tests. Almost there ;) |
Now all |
Should be working now. Works in isolation with all tests passing. This can be used as a template/blueprint for adding similar, more advanced conditional logic! # all tests pass
$ jest test/conditions/when-condition.test.js
# all tests pass
$ jest test/conditions/when-entry.test.js
# never invalid for current test/schema setup
$ jest test/schema-when-then.test.js |
Now I'm adding tests for manual approach, then comparing with engine generated schema and result. See describe("manual then", () => {
const bigJson = {
valid: {
isBig: true,
count: 5
},
invalid: {
isBig: true,
count: 4
}
};
describe("simple", () => {
const yupSchema = yup.object({
isBig: yup.boolean(),
count: yup.number().when("isBig", {
is: true, // alternatively: (val) => val == true
then: yup.number().min(5)
})
});
const tester = createManualTester(yupSchema);
test("valid", () => {
tester(bigJson.valid, true);
});
test("invalid", () => {
tester(bigJson.invalid, false);
});
});
}); |
Finally works with the |
Please test it out before I merge into master ;) |
Released: |
Hi!
Many thanks for the useful library! I just want to ask if it is possible already but I am not aware of it or if it will be possible any time soon to include 'when' into json schema? I have to achieve this https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema somehow.
Thanks!
The text was updated successfully, but these errors were encountered: