ajv-validator / ajv-validator/ajv-errors

Custom error for minLength on array property is not working

Đang mở
#102 1 bình luận 1 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
TypeScript
Star
289
Fork
21
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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.

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

export function validate(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:
```json
{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"}
```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.