fluent-json-schema icon indicating copy to clipboard operation
fluent-json-schema copied to clipboard

Conditional property is not allowed when using additionalProperties(false)

Open JorisCodePeople opened this issue 2 years ago • 0 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Fastify version

?

Plugin version

3.1.0

Node.js version

16.17.1

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

When I declare a conditional property on an object that has additionalProperties(false), that property is not allowed in a JSON file using the resulting schema.

Steps to Reproduce

Fluent schema:

const S = require('fluent-json-schema')

const testSchema = S.object()
  .additionalProperties(false)
  .prop('$schema', S.string().required())
  .prop('type', S.string().required())
  .allOf([S.ifThen(S.object().prop('type', S.const('type1')), S.object().prop('value', S.string().required()))])

resulting schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "type": {
            "const": "type1"
          }
        }
      },
      "then": {
        "properties": {
          "value": {
            "type": "string"
          }
        },
        "required": [
          "value"
        ]
      }
    }
  ],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "type": {
      "type": "string"
    }
  },
  "required": [
    "$schema",
    "type"
  ]
}

JSON using the schema:

{
  "$schema": "../schemas/test.schema.json",
  "type": "type1",
  "value": "something"
}

Expected Behavior

When type === "type1", i'd expect value to be an allowed (even required) property. It is not. However, when starting to write the property name, it is suggested by VSCode as an attribute.

JorisCodePeople avatar Oct 10 '22 12:10 JorisCodePeople