redhat-developer / redhat-developer/vscode-yaml

Matches multiple schemas when only one must validate

Open
#625 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
829
Forks
260
Avg merge
5h 43m
Merged PRs (30d)
1

Description

Question

I have complex json-schema file for validating configuration files for ansible. Below is short part with problem that I have. I'm not clear why "test 2" is not working (not properly matched against json-schema).
Based on this doc (https://json-schema.org/understanding-json-schema/reference/combining.html), oneOf=XOR, anyOf=OR, but my tests are not working.

I'm using vscode-yaml v0.23 (because v1.0.0 has an issue with autocompletion)

json-schema file:

{
  "oneOf": [{ "$ref": "#/$defs/objectA" }, { "$ref": "#/$defs/objectB" }],

  "$defs": {
    "objectA": {
      "type": "object",
      "title": "Object A",
      "properties": {
        "objA": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": { "$ref": "#/$defs/name" },
              "color": { "type": "string", "description": "color" }
            },
            "required": ["name"],
            "additionalProperties": false
          }
        }
      }
    },
    "objectB": {
      "type": "object",
      "title": "Object B",
      "properties": {
        "objB": {
          "type": "object",
          "properties": {
            "name": { "$ref": "#/$defs/name" },
            "size": { "type": "string", "description": "size" }
          },
          "required": ["name", "size"],
          "additionalProperties": false
        }
      }
    },
    "name": {
      "$comment": "this is common property for objA and objB",
      "name": { "type": "string", "description": "name" }
    }
  }
}

First test

test 1

this file is not valid.
error: "Matches multiple schemas when only one must validate."
reason: (from my point of view) because match rule is set to "oneOf" and in this configuration is matched multiple rules.

objA:
  - name: testA1
    color: red
  - name: testA2

objB:
  size: middle
  name: nameB

test 2

error: the same error as before

But it's not clear for me... match rule is 'oneOf', yaml config is changed that only one valid configuration is there (for object A) but validation ends with error. The question is, why is matched multiple schemas, when only one (object A) is configured (and matched from my point of view).

objA:
  - name: testA1
    color: red
  - name: testA2

Another test

When I change oneOf to anyOf:
"anyOf": [{ "$ref": "#/$defs/objectA" }, { "$ref": "#/$defs/objectB" }]

This config file IS valid (it's wrong, becasue 'objB' is missing required parameter 'name'):

objA:
  - name: testA1
    color: red
  - name: testA2

objB:
  size: middle

Can anybody explain me where is the problem?

thx, martin

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the supplied schema and YAML examples using vscode-yaml v0.23, beginning with test 1 and test 2. Compare how oneOf, anyOf, and the referenced definitions are evaluated; done means determining whether the validator has a defect and documenting a precise, reproducible result or required project change.

Written by the indexing model from the issue text.

Assessment

Tech stack
json, typescript, yaml
Domain
testing-qa, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.