Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using if/then/else fails validation #2417

Open
tet-lenovo opened this issue Apr 17, 2024 · 1 comment
Open

Using if/then/else fails validation #2417

tet-lenovo opened this issue Apr 17, 2024 · 1 comment

Comments

@tet-lenovo
Copy link

What version of Ajv are you using? Does the issue happen if you use the latest version?
8.12.0

Ajv options object

{
    coerceTypes: "array",
    useDefaults: true,
    removeAdditional: "all",
    allErrors: false,
    $data: true,
    strictTypes: true,
    verbose: true
}

JSON Schema

{
        type: "object",
        properties: {
            minValue: {
                type: "integer",
                minimum: -1,
                isLessThan: { $data: "1/maxValue" }
            },
            maxValue: {
                type: "integer",
                minimum: -1
            }
        },
        required: ["minValue", "maxValue"],
        if: {
            properties: {
                maxValue: { const: -1 }
            }
        },
        then: {
            properties: {
                minValue: {
                    type: "integer",
                    minimum: -1
                }
            }
        },
        $id: "some-id",
        $schema: "http://json-schema.org/draft-07/schema"
    }

Sample data

const testData = {
    minValue: 2,
    maxValue: -1
}

Your code

validate(testData);

Validation result, data AFTER validation, error messages

    <ref *2> [Function: validate18] {
      errors: [
        {
          instancePath: '',
          schemaPath: '#/required',
          keyword: 'required',
          params: { missingProperty: 'minValue' },
          message: "must have required property 'minValue'",
          schema: [ 'minValue', 'maxValue' ],
          parentSchema: {
            type: 'object',
            properties: {
              minValue: {
                type: 'integer',
                minimum: -1,
                isLessThan: { '$data': '1/maxValue' }
              },
              maxValue: { type: 'integer', minimum: -1 }
            },
            required: [ 'minValue', 'maxValue' ],
            if: { properties: { maxValue: { const: -1 } } },
            then: { properties: { minValue: { type: 'integer', minimum: -1 } } },
            '$id': '0ad1ddfd-84e0-4e24-b793-eae349bb9d6c',
            '$schema': 'http://json-schema.org/draft-07/schema'
          },
          data: {}
        }
      ],
      schema: {
        type: 'object',
        properties: {
          minValue: {
            type: 'integer',
            minimum: -1,
            isLessThan: { '$data': '1/maxValue' }
          },
          maxValue: { type: 'integer', minimum: -1 }
        },
        required: [ 'minValue', 'maxValue' ],
        if: { properties: { maxValue: { const: -1 } } },
        then: { properties: { minValue: { type: 'integer', minimum: -1 } } },
        '$id': '0ad1ddfd-84e0-4e24-b793-eae349bb9d6c',
        '$schema': 'http://json-schema.org/draft-07/schema'
      },
      schemaEnv: <ref *1> SchemaEnv {
        refs: {},
        dynamicAnchors: {},
        schema: {
          type: 'object',
          properties: {
            minValue: {
              type: 'integer',
              minimum: -1,
              isLessThan: { '$data': '1/maxValue' }
            },
            maxValue: { type: 'integer', minimum: -1 }
          },
          required: [ 'minValue', 'maxValue' ],
          if: { properties: { maxValue: { const: -1 } } },
          then: { properties: { minValue: { type: 'integer', minimum: -1 } } },
          '$id': '0ad1ddfd-84e0-4e24-b793-eae349bb9d6c',
          '$schema': 'http://json-schema.org/draft-07/schema'
        },
        schemaId: '$id',
        root: [Circular *1],
        baseId: '0ad1ddfd-84e0-4e24-b793-eae349bb9d6c',
        schemaPath: undefined,
        localRefs: {},
        meta: undefined,
        '$async': undefined,
        validateName: ValueScopeName {
          str: 'validate18',
          prefix: 'validate',
          value: { ref: [Circular *2] },
          scopePath: _Code { _items: [ '.', Name { str: 'validate' }, '[', 11, ']' ] }
        },
        validate: [Circular *2]
      }
    }

What results did you expect?
If maxValue is -1, minValue can be anything greater than -1. For any other value of maxValue, maxValue has to be greater than minValue. If I remove the if/then/else from my schema, everything validates correctly. Not sure if I am doing something silly with my schema or if this is a bug.

Are you going to resolve the issue?
Unfortunately, not sure that I can.

@jasoniangreen
Copy link
Collaborator

jasoniangreen commented May 5, 2024

Hi @tet-lenovo, thanks for bringing this to our attention. I am having some trouble replicating the issue. Could you provide a runkit example, you can use this template if it helps.

https://runkit.com/esp/ajv-issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants