Skip to content
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 for if, else, then rules #2506

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export function optionsList(schema) {
});
} else {
const altSchemas = schema.oneOf || schema.anyOf;
return altSchemas.map((schema, i) => {
return altSchemas.map(schema => {
const value = toConstant(schema);
const label = schema.title || String(value);
return {
Expand Down Expand Up @@ -665,12 +665,40 @@ export function stubExistingAdditionalProperties(
return schema;
}

const resolveCondition = (schema, rootSchema, formdata) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename formdata to formData

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment describing what this function does

let {
if: expression,
then,
else: otherwise,
...resolvedSchemaLessConditional
} = schema;

const conditionalSchema = isValid(expression, formdata, rootSchema)
? then
: otherwise;

if (conditionalSchema) {
return retrieveSchema(
mergeSchemas(
resolvedSchemaLessConditional,
retrieveSchema(conditionalSchema, rootSchema, formdata)
Comment on lines +683 to +684
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed that this was swapped (presumably to fix a bug?). Is there a test case we could add that would catch a regression?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
if: ...,
then: {minLength: 5},
minLength: 10
}

Expected behavior: inner property (minLength: 5) should override outer property (minLength: 10). We should add a test case for this

),
rootSchema,
formdata
);
} else {
return retrieveSchema(resolvedSchemaLessConditional, rootSchema, formdata);
}
};

export function resolveSchema(schema, rootSchema = {}, formData = {}) {
if (schema.hasOwnProperty("$ref")) {
return resolveReference(schema, rootSchema, formData);
} else if (schema.hasOwnProperty("dependencies")) {
const resolvedSchema = resolveDependencies(schema, rootSchema, formData);
return retrieveSchema(resolvedSchema, rootSchema, formData);
} else if (schema.hasOwnProperty("if")) {
return resolveCondition(schema, rootSchema, formData);
} else if (schema.hasOwnProperty("allOf")) {
return {
...schema,
Expand Down
204 changes: 204 additions & 0 deletions packages/core/test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,210 @@ describe("utils", () => {
});
});
});

describe("ifThenElse", () => {
it("should resolve if, then", () => {
const schema = {
type: "object",
properties: {
country: {
default: "United States of America",
enum: ["United States of America", "Canada"],
},
},
if: {
properties: { country: { const: "United States of America" } },
},
then: {
properties: { postal_code: { pattern: "[0-9]{5}(-[0-9]{4})?" } },
},
else: {
properties: {
postal_code: { pattern: "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" },
},
},
};
const definitions = {};
const formData = {
country: "United States of America",
postal_code: "20500",
};
expect(retrieveSchema(schema, { definitions }, formData)).eql({
type: "object",
properties: {
country: {
default: "United States of America",
enum: ["United States of America", "Canada"],
},
postal_code: { pattern: "[0-9]{5}(-[0-9]{4})?" },
},
});
});
it("should resolve if, else", () => {
const schema = {
type: "object",
properties: {
country: {
default: "United States of America",
enum: ["United States of America", "Canada"],
},
},
if: {
properties: { country: { const: "United States of America" } },
},
then: {
properties: { postal_code: { pattern: "[0-9]{5}(-[0-9]{4})?" } },
},
else: {
properties: {
postal_code: { pattern: "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" },
},
},
};
const definitions = {};
const formData = {
country: "Canada",
postal_code: "K1M 1M4",
};
expect(retrieveSchema(schema, { definitions }, formData)).eql({
type: "object",
properties: {
country: {
default: "United States of America",
enum: ["United States of America", "Canada"],
},
postal_code: { pattern: "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" },
},
});
});
it("should resolve multiple conditions", () => {
const schema = {
type: "object",
properties: {
animal: {
enum: ["Cat", "Fish"],
},
},
allOf: [
{
if: {
properties: { animal: { const: "Cat" } },
},
then: {
properties: {
food: { type: "string", enum: ["meat", "grass", "fish"] },
},
},
required: ["food"],
},
{
if: {
properties: { animal: { const: "Fish" } },
},
then: {
properties: {
food: {
type: "string",
enum: ["insect", "worms"],
},
water: {
type: "string",
enum: ["lake", "sea"],
},
},
required: ["food", "water"],
},
},
{
required: ["animal"],
},
],
};
const definitions = {};
const formData = {
animal: "Cat",
};

expect(retrieveSchema(schema, { definitions }, formData)).eql({
type: "object",
properties: {
animal: {
enum: ["Cat", "Fish"],
},
food: { type: "string", enum: ["meat", "grass", "fish"] },
},
required: ["animal", "food"],
});
});
it.only("should resolve $ref", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove .only

const schema = {
type: "object",
properties: {
animal: {
enum: ["Cat", "Fish"],
},
},
allOf: [
{
if: {
properties: { animal: { const: "Cat" } },
},
then: {
$ref: "#/definitions/cat",
},
required: ["food"],
},
{
if: {
properties: { animal: { const: "Fish" } },
},
then: {
$ref: "#/definitions/fish",
},
},
{
required: ["animal"],
},
],
};

const definitions = {
cat: {
properties: {
food: { type: "string", enum: ["meat", "grass", "fish"] },
},
},
fish: {
properties: {
food: {
type: "string",
enum: ["insect", "worms"],
},
water: {
type: "string",
enum: ["lake", "sea"],
},
},
required: ["food", "water"],
},
};

const formData = {
animal: "Cat",
};

expect(retrieveSchema(schema, { definitions }, formData)).eql({
type: "object",
properties: {
animal: {
enum: ["Cat", "Fish"],
},
food: { type: "string", enum: ["meat", "grass", "fish"] },
},
required: ["animal", "food"],
});
});
});
});

describe("shouldRender", () => {
Expand Down
45 changes: 45 additions & 0 deletions packages/playground/src/samples/ifThenElse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module.exports = {
schema: {
type: "object",
properties: {
animal: {
enum: ["Cat", "Fish"],
},
},
allOf: [
{
if: {
properties: { animal: { const: "Cat" } },
},
then: {
properties: {
food: { type: "string", enum: ["meat", "grass", "fish"] },
},
required: ["food"],
},
},
{
if: {
properties: { animal: { const: "Fish" } },
},
then: {
properties: {
food: {
type: "string",
enum: ["insect", "worms"],
},
water: {
type: "string",
enum: ["lake", "sea"],
},
},
required: ["food", "water"],
},
},
{
required: ["animal"],
},
],
},
formData: {},
};
2 changes: 2 additions & 0 deletions packages/playground/src/samples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import nullable from "./nullable";
import nullField from "./null";
import errorSchema from "./errorSchema";
import defaults from "./defaults";
import ifThenElse from "./ifThenElse";

export const samples = {
Simple: simple,
Expand All @@ -52,6 +53,7 @@ export const samples = {
"Any Of": anyOf,
"One Of": oneOf,
"All Of": allOf,
"If Then Else": ifThenElse,
"Null fields": nullField,
Nullable: nullable,
ErrorSchema: errorSchema,
Expand Down