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

Custom error for minLength on array property is not working #102

Open
yuval-hazaz opened this issue May 1, 2021 · 1 comment
Open

Custom error for minLength on array property is not working #102

yuval-hazaz opened this issue May 1, 2021 · 1 comment

Comments

@yuval-hazaz
Copy link

yuval-hazaz commented May 1, 2021

code sandbox:
https://codesandbox.io/s/friendly-boyd-c2qtm?file=/src/index.ts

The bellow schema returns the correct error message for minLength when it is defined on the parent or child prop, but it returns the default error when defined on an array property.

import Ajv from "ajv";
import ajvErrors from "ajv-errors";

export function validate<T>(values: T, validationSchema: object) {
  const ajv = new Ajv({ allErrors: true });

  ajvErrors(ajv);

  const validate = ajv.compile(validationSchema);

  validate(values);

  console.log(...validate.errors);
}

const SCHEMA = {
  type: "object",
  required: ["parent", "child", "arr"],
  errorMessage: {
    properties: {
      parent: "parent custom length error ***111"
    }
  },
  properties: {
    parent: {
      type: "string",
      minLength: 5
    },
    arr: {
      type: "array",
      items: {
        type: "object",
        required: ["arrayProp"],
        errorMessage: {
          required:"custom required array prop",
          properties: {
            arrayProp: "arrayProp custom length error ***333 "
          }
        },
        properties: {
          arrayProp: {
            type: "string",
            minLength: 5
          }
        }
      }
    },
    child: {
      type: "object",
      required: ["childProp"],
      errorMessage: {
        properties: {
          childProp: "childProp custom length error ***222"
        }
      },
      properties: {
        childProp: {
          type: "string",
          minLength: 5
        }
      }
    }
  }
};

const DATA = {
  parent: "aaaa",
  child: {
    childProp: "aaaa"
  },
  arr: [
    {
      arrayProp: "aaaa"
    }
  ]
};

validate(DATA, SCHEMA);

these are the results:

{instancePath: "/arr/0/arrayProp", schemaPath: "#/properties/arr/items/properties/arrayProp/minLength", keyword: "minLength", params: Object, message: "must NOT have fewer than 5 characters"}
{instancePath: "/child/childProp", schemaPath: "#/properties/child/errorMessage", keyword: "errorMessage", params: Object, message: "childProp custom length error ***222"}
{instancePath: "/parent", schemaPath: "#/errorMessage", keyword: "errorMessage", params: Object, message: "parent custom length error ***111"}
@Arkni
Copy link

Arkni commented May 18, 2021

Hi,

Had the same issue a few days ago. The way I fixed it is by declaring the error message at the property level:

{
  "properties"​: {​
    "​arrayProp"​: {​
      "​type"​: "string"​,​
      "minLength"​: 5​,
      "errorMessage": {
        "type": "my custom message here",
        "_": "my other custom message here"
      }
    }
  }​
}

The solution is not optimal but it does the job for now.

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants