Skip to content

better-ajv-errors shows only one missing required field error instead of all missing fields #216

@liran100

Description

@liran100

Hi all!
When validating JSON data against a schema with multiple required fields, if the data is missing more than one required field, better-ajv-errors returns only the first missing field error. (ajv validate return both of them)
I expect it to show errors for all missing required fields.

I think this is a critical issue for users relying on comprehensive validation feedback, and I would be glad if someone could help resolve it.

I add a small example:

schema.json
{ "type": "object", "properties": { "updated": { "type": "string", "format": "date-time", "title": "Updated", "description": "The date and time (timestamp) when the vulnerability record was last updated." }, "updated2": { "type": "string", "format": "date-time", "title": "Updated", "description": "The date and time (timestamp) when the vulnerability record was last updated." } }, "required": [ "updated", "updated2" ], "additionalProperties": false }

index.js

const fs = require('fs');
const Ajv = require('ajv');
const betterAjvErrors = require('better-ajv-errors').default;

const schema = JSON.parse(fs.readFileSync('./schema.json', 'utf-8'));

// Example data to validate
const dataSamples = [
  { updated: "2023-06-05T15:30:00Z", updated2: "2023-06-05T15:30:00Z" },             // valid
  { notUpdated: "2023-06-05T15:30:00Z" },          // missing required "updated"
];

const ajv = new Ajv({ allErrors: true, strict: false });
require('ajv-formats')(ajv);

const validate = ajv.compile(schema);

dataSamples.forEach((data, index) => {
  const valid = validate(data);
  console.log(`\nSample #${index + 1}:`, data);
  if (!valid) {
    console.log('Validation errors from ajv:\n',validate.errors);
    const output = betterAjvErrors(schema, data, validate.errors, { format: 'cli' });
    console.log('Validation errors:\n', output);
  } else {
    console.log('Validation passed!');
  }
});

screenshot output - only the first missing required field error is shown.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions