Skip to content

Commit d60e300

Browse files
committed
fix: create new ajv instance when additionalMetaSchemas prop is null, add tests
1 parent 05f6e7d commit d60e300

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export default function validateFormData(
163163
schema,
164164
customValidate,
165165
transformErrors,
166-
additionalMetaSchemas
166+
additionalMetaSchemas = []
167167
) {
168168
// add more schemas to validate against
169169
if (

test/Form_test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
createComponent,
1010
createFormComponent,
1111
createSandbox,
12+
setProps,
1213
} from "./test_utils";
1314

1415
describe("Form", () => {
@@ -1977,4 +1978,42 @@ describe("Form", () => {
19771978
expect(node.getAttribute("novalidate")).not.to.be.null;
19781979
});
19791980
});
1981+
1982+
describe("Meta schema updates", () => {
1983+
it("Should update allowed meta schemas when additionalMetaSchemas is changed", () => {
1984+
const formProps = {
1985+
liveValidate: true,
1986+
schema: {
1987+
$schema: "http://json-schema.org/draft-04/schema#",
1988+
type: "string",
1989+
minLength: 8,
1990+
pattern: "d+",
1991+
},
1992+
formData: "short",
1993+
additionalMetaSchemas: [],
1994+
};
1995+
1996+
const { comp } = createFormComponent(formProps);
1997+
1998+
expect(comp.state.errorSchema).eql({});
1999+
2000+
setProps(comp, {
2001+
...formProps,
2002+
additionalMetaSchemas: [
2003+
require("ajv/lib/refs/json-schema-draft-04.json"),
2004+
],
2005+
});
2006+
2007+
expect(comp.state.errorSchema).eql({
2008+
__errors: [
2009+
"should NOT be shorter than 8 characters",
2010+
'should match pattern "d+"',
2011+
],
2012+
});
2013+
2014+
setProps(comp, formProps);
2015+
2016+
expect(comp.state.errorSchema).eql({});
2017+
});
2018+
});
19802019
});

0 commit comments

Comments
 (0)