enjoi icon indicating copy to clipboard operation
enjoi copied to clipboard

"abortEarly" does not work with "allOf"

Open ozum opened this issue 6 years ago • 1 comments

When used with allOf, abortEarly does not work. I suppose it is related to Joi extension for allOf.

I pasted a minimal sample code below:

Example Code

const Joi = require("joi");
const Enjoi = require("enjoi");

const jsonSchema = {
  allOf: [
    {
      allOf: [
        {
          type: "object",
          properties: {
            a: { type: "string" },
            b: { type: "string" },
          },
          required: ["a", "b"],
        },
        {
          type: "object",
          properties: {
            c: { type: "string" },
            d: { type: "string" },
          },
          required: ["d"],
        },
      ],
    },
  ],
};

const jsonSchema2 = {
  type: "object",
  properties: {
    a: { type: "string" },
    b: { type: "string" },
  },
  required: ["a", "b"],
};

const result = Joi.validate({}, Enjoi.schema(jsonSchema), { abortEarly: false });
const result2 = Joi.validate({}, Enjoi.schema(jsonSchema2), { abortEarly: false });

console.log(result.error);
console.log(result2.error);

Actual Result

result.error has single error. result2.error has multiple errors.

Expected Result

result.error has multiple error. result2.error has multiple errors.

Kind Regards,

ozum avatar Aug 29 '18 12:08 ozum

Unfortunately it may not be possible to do this. The reason is, through extend we are supposed to use createError or createOverrideError to return an error. This returns a single error...

If we want to have multiple errors then we need access to Error.process(errors, ...) and this is not exposed AFAIK.

tlivings avatar Aug 30 '18 19:08 tlivings