Skip to content

Commit

Permalink
fix tests and delete validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Puczynski committed Feb 25, 2019
1 parent 01a7758 commit 2ec6ecc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ function toErrorSchema(errors) {
}
parent = parent[segment];
}

if (Array.isArray(parent.__errors)) {
// We store the list of errors for this node in a property named __errors
// to avoid name collision with a possible sub schema field named
// "errors" (see `validate.createErrorHandler`).
parent.__errors = parent.__errors.concat(message);
} else {
parent.__errors = [message];
if (message) {
parent.__errors = [message];
}
}
return errorSchema;
}, {});
Expand Down Expand Up @@ -183,6 +186,7 @@ export default function validateFormData(
validationErrors = err;
}
let errors = transformAjvErrors(ajv.errors);

if (validationErrors && validationErrors.message) {
errors = [
...errors,
Expand All @@ -201,7 +205,7 @@ export default function validateFormData(
const errorSchema = toErrorSchema(errors);

if (typeof customValidate !== "function") {
return { errors, errorSchema, validationErrors };
return { errors, errorSchema };
}

const errorHandler = customValidate(formData, createErrorHandler(formData));
Expand Down
19 changes: 12 additions & 7 deletions test/validate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,14 @@ describe("Validation", () => {
{ datasetId: "some kind of text" },
schema
);
expect(errors.validationErrors.message).to.equal(
'no schema with key or ref "http://json-schema.org/draft-04/schema#"'
);
expect(errors.errors).to.eql([]);
const errMessage =
'no schema with key or ref "http://json-schema.org/draft-04/schema#"';
expect(errors.errors[0].stack).to.equal(errMessage);
expect(errors.errors).to.eql([
{
stack: errMessage,
},
]);
expect(errors.errorSchema).to.eql({});
});
it("should return a validation error about formData", () => {
Expand All @@ -137,7 +141,6 @@ describe("Validation", () => {
null,
[metaSchemaDraft4]
);
expect(errors.validationErrors).to.equal(null);
expect(errors.errors).to.have.lengthOf(1);
expect(errors.errors[0].stack).to.equal(
'.datasetId should match pattern "\\d+"'
Expand All @@ -151,7 +154,6 @@ describe("Validation", () => {
null,
[metaSchemaDraft4, metaSchemaDraft6]
);
expect(errors.validationErrors).to.equal(null);
expect(errors.errors).to.have.lengthOf(1);
expect(errors.errors[0].stack).to.equal(
'.datasetId should match pattern "\\d+"'
Expand Down Expand Up @@ -297,10 +299,13 @@ describe("Validation", () => {
});

it("should return an error list", () => {
expect(errors).to.have.length.of(1);
expect(errors).to.have.length.of(2);
expect(errors[0].name).eql("type");
expect(errors[0].property).eql(".properties['foo'].required");
expect(errors[0].message).eql("should be array");
expect(errors[1].stack).eql(
"schema is invalid: data.properties['foo'].required should be array"
);
});

it("should return an errorSchema", () => {
Expand Down

0 comments on commit 2ec6ecc

Please sign in to comment.