Skip to content

useDefault option not working #2114

Open
@frrodriguez

Description

What version of Ajv are you using? Does the issue happen if you use the latest version?
8.11.0
Ajv options object

{
  useDefaults: "empty", // 'empty' or true
  allErrors: true,
  coerceTypes: true,
  parseDate: true,
}

JSON Schema

{
    "type": "object",
    "properties": {
      "disabled": {
        "type": "boolean",
        "default": false,
        "description": "Disable real redis connection for use a mock"
      },
      "clientOptions": {
        "type": "object",
        "properties": {
          "host": { "type": "string", "default": "localhost" },
          "port": { "type": "number" },
          "auth_pass": { "type": "string" }
        },
        "required": ["host"],
        "description": "Client options for redis connection"
      }
    },
    "oneOf": [
      { "properties": { "disabled": { "const": true } } },
      { "required": ["clientOptions"] }
    ],
    "required": ["disabled"]
  }

Sample data

Sample 1 (OK, just for test)

{ "disabled": false }

Sample 2 (ISSUED)

{  }

Your code

const Ajv = require("ajv");

const validator = new Ajv(options);

// Validation 1
const result1 = validator.validate(schema, data1); // (SAMPLE 1) Expected false and its ok

// Validation 2 Here its the problem
const result2 = validator.validate(schema, data2); // (SAMPLE 2) Expected false but returns true ??

Validation result, data AFTER validation, error messages

SAMPLE 1 OK!
// valid: false
[
  {
    instancePath: '/disabled',
    schemaPath: '#/oneOf/0/properties/disabled/const',
    keyword: 'const',
    params: { allowedValue: true },
    message: 'must be equal to constant'
  },
  {
    instancePath: '',
    schemaPath: '#/oneOf/1/required',
    keyword: 'required',
    params: { missingProperty: 'clientOptions' },
    message: "must have required property 'clientOptions'"
  },
  {
    instancePath: '',
    schemaPath: '#/oneOf',
    keyword: 'oneOf',
    params: { passingSchemas: null },
    message: 'must match exactly one schema in oneOf'
  }
]

SAMPLE 2 UNEXPECTED!!
// valid: true
nulll

What results did you expect?
With the useDefault property specified, i expect the behaviour for Sample1 and Sample2 would be the same and both fails cause takes the same false value <failing schema cause clientOptions its required by oneOf>
Expect same result when disabled its false (Sample1 its explicit and Sample2 its by default)

Are you going to resolve the issue?
Not sure

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions