vscode-yaml icon indicating copy to clipboard operation
vscode-yaml copied to clipboard

if-then on const booleans not working correctly

Open JeffreyCA opened this issue 4 months ago • 0 comments

Describe the bug

JSON Schema 1

With this simple example, if the hasPrice boolean is set to true, then price should be a required property. If hasPrice is explicitly set to false or is not specified, price should optional.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Schema",
  "description": "description",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "item": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "hasPrice": {
          "type": "boolean",
          "description": "Whether item costs",
          "default": false
        },
        "price": {
          "type": "number",
          "description": "Price",
          "minimum": 0
        },
        "name": {
          "type": "string",
          "description": "Product name"
        }
      },
      "if": {
        "properties": {
          "hasPrice": { "const": true }
        },
        "required": ["hasPrice"]
      },
      "then": {
        "required": ["price"]
      }
    }
  },
  "required": [
    "item"
  ]
}

Given name only

# yaml-language-server: $schema=/path/to/schema.json
item:
  name: abc

Expected: Should not trigger validation error. Observed: No validation error. ✅

Given name and hasPrice false

# yaml-language-server: $schema=/path/to/schema.json
item:
  name: abc
  hasPrice: false

Expected: Should not trigger validation error. Observed: No validation error. ✅

Given name and hasPrice true

# yaml-language-server: $schema=/path/to/schema.json
item:
  name: abc
  hasPrice: true

Expected: Should trigger validation error: Missing property "price". Observed: Does not trigger validation error. ❌

JSON Schema 2

In this example, if noPrice is explicitly set to false, or is unspecified, then price should be required:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Schema",
  "description": "description",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "item": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "noPrice": {
          "type": "boolean",
          "description": "Whether no cost",
          "default": false
        },
        "price": {
          "type": "number",
          "description": "Price",
          "minimum": 0
        },
        "name": {
          "type": "string",
          "description": "Product name"
        }
      },
      "if": {
        "properties": {
          "noPrice": { "const": false }
        }
      },
      "then": {
        "required": ["price"]
      }
    }
  },
  "required": [
    "item"
  ]
}

Given name only

# yaml-language-server: $schema=/path/to/schema.json
item:
  name: abc

Expected: Should trigger validation error: Missing property "price". Observed: Validation error triggered ✅

Given name and noPrice false

# yaml-language-server: $schema=/path/to/schema.json
item:
  name: abc
  noPrice: false

Expected: Should trigger validation error: Missing property "price". Observed: No validation error. ❌

Given name and noPrice true

# yaml-language-server: $schema=/path/to/schema.json
item:
  name: abc
  noPrice: true

Expected: Should not trigger validation error. Observed: No validation error. ✅

Environment

  • [x] Windows
  • [ ] Mac
  • [ ] Linux
  • [ ] other (please specify)

Extension version

1.18.0

Also tested older versions and same issue occurs.

JeffreyCA avatar Jun 06 '25 01:06 JeffreyCA