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

Fix default values and matching schemas when oneOf / anyOf subschemas contain references #2272

Merged
merged 21 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 21 additions & 12 deletions packages/core/src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { deepEquals, getDefaultFormState } from "./utils";

let formerCustomFormats = null;
let formerMetaSchema = null;
const rootSchemaId = "__rjsf_rootSchema";
NixBiks marked this conversation as resolved.
Show resolved Hide resolved

import { isObject, mergeObjects } from "./utils";

Expand Down Expand Up @@ -264,17 +265,25 @@ export default function validateFormData(
}

/**
* Get a similar schema where ref's are prefixed with "__rjsf_rootSchema"
* Get a similar schema where ref's are prefixed with `rootSchemaId`
NixBiks marked this conversation as resolved.
Show resolved Hide resolved
* This is used in isValid to make references to the rootSchema
*/
function withIdRefPrefix(schema) {
const obj = { ...schema };
for (let key of Object.keys(obj)) {
const value = obj[key];
if (key === "$ref") {
obj[key] = "__rjsf_rootSchema" + value;
} else if (value.constructor === Object) {
obj[key] = withIdRefPrefix(value);
function withIdRefPrefix(schemaNode) {
NixBiks marked this conversation as resolved.
Show resolved Hide resolved
let obj = schemaNode;
NixBiks marked this conversation as resolved.
Show resolved Hide resolved
if (schemaNode.constructor === Object) {
obj = { ...schemaNode };
for (let key of Object.keys(obj)) {
NixBiks marked this conversation as resolved.
Show resolved Hide resolved
const value = obj[key];
if (key === "$ref") {
obj[key] = rootSchemaId + value;
} else {
obj[key] = withIdRefPrefix(value);
}
}
} else if (Array.isArray(schemaNode)) {
obj = [...schemaNode];
for (var i = 0; i < obj.length; i++) {
obj[i] = withIdRefPrefix(obj[i]);
NixBiks marked this conversation as resolved.
Show resolved Hide resolved
}
}
return obj;
Expand All @@ -293,19 +302,19 @@ export function isValid(schema, data, rootSchema) {
// that lives in the rootSchema but not in the schema in question.
if (rootSchema) {
NixBiks marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

I think rootSchema is always given -- so maybe we can just remove this check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right - except for some tests, but I've just updated those tests to add the schema as rootSchema as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively I could leave the tests as is and have this instead

return ajv
  .addSchema(rootSchema || schema, ROOT_SCHEMA_PREFIX)
  .validate(withIdRefPrefix(schema), data);

So if no rootSchema is provided then the schema itself will be used. Any preferences with one or the other?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to just go ahead and update the tests rather than adding unnecessary functionality to this function.

const result = ajv
NixBiks marked this conversation as resolved.
Show resolved Hide resolved
.addSchema(rootSchema, "__rjsf_rootSchema")
.addSchema(rootSchema, rootSchemaId)
.validate(withIdRefPrefix(schema), data);

// make sure we remove the rootSchema from the global ajv instance
ajv.removeSchema("__rjsf_rootSchema");
ajv.removeSchema(rootSchemaId);
return result;
} else {
return ajv.validate(schema, data);
}
} catch (e) {
try {
// make sure we also remove the rootSchema if an error occured before removing but after adding
ajv.removeSchema("__rjsf_rootSchema");
ajv.removeSchema(rootSchemaId);
} catch (e) {
return false;
}
Expand Down
20 changes: 20 additions & 0 deletions packages/core/test/validate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ describe("Validation", () => {

expect(isValid(schema, { foo: "bar" })).to.be.false;
});

it("should return true if the data is valid against the schema including refs to rootSchema", () => {
const schema = {
anyOf: [{ $ref: "#/defs/foo" }],
};
const rootSchema = {
defs: {
foo: {
properties: {
name: { type: "string" },
},
},
},
};
const formData = {
name: "John Doe",
};

expect(isValid(schema, formData, rootSchema)).to.be.true;
});
});

describe("validate.validateFormData()", () => {
Expand Down