fastest-validator icon indicating copy to clipboard operation
fastest-validator copied to clipboard

Incorrect validation multi array of objects

Open PetrChalov opened this issue 1 year ago • 1 comments
trafficstars

Hi! I encountered incorrect validation of an array of objects.

Code:

const Validator = require('fastest-validator');
const v = new Validator();

const schema = {
  $$root: true,
  type: 'array',
  items: {
    type: 'multi',
    rules: [
      {
        type: 'object',
        props: {
          data: {
            type: 'object',
            props: { position: { type: 'number' } },
          },
          meta: {
            type: 'object',
            props: { op: { type: 'equal', value: 'up' } },
          },
        },
      },
      {
        type: 'object',
        props: {
          data: {
            type: 'object',
            props: { value: { type: 'string' } },
          },
          meta: {
            type: 'object',
            props: { op: { type: 'equal', value: 'down' } },
          },
        },
      },
    ],
  },
};

const check = v.compile(schema);

const data = [
  {
    data: { position: 1 },
    meta: { op: 'down' },
  },
  {
    data: { value: 'val' },
    meta: { op: 'down' },
  },
];

console.log(check(data));
// Print
// [
//   {
//     type: 'equalValue',
//     message: "The '[0].meta.op' field value must be equal to 'up'.",
//     field: '[0].meta.op',
//     expected: 'up',
//     actual: 'down'
//   },
//   {
//     type: 'required',
//     message: "The '[0].data.value' field is required.",
//     field: '[0].data.value',
//     actual: undefined
//   }
// ]

Is there any way to make this work?

PetrChalov avatar Aug 14 '24 13:08 PetrChalov