Skip to content

Incorrect validation multi array of objects #347

@PetrChalov

Description

@PetrChalov

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions