ritchieanesco/json-schema-yup-transform

if/then validation help

Closed this issue · 2 comments

Great library and excellent docs!

I have a validation passes validation when using a plain json schema validator: https://www.jsonschemavalidator.net/s/XQqK0y9U

But I've set up a codesandbox with essentially the same schema but I get false when running isValid on my yupschema: https://codesandbox.io/s/reverent-sun-ckfc9

Perhaps theres something very simple I'm missing here. Would love any and all pointers.

Including my schema here for reference but we can also see it in either link above:
Schema

{
  "$id": "http://example.com/example.json",
  "$schema": "http://json-schema.org/draft-07/schema",
  "default": {},
  "title": "STEP EDITOR",
  "description": "The Overview is used to name and describe a Step when viewed as part of a Path.",
  "type": "object",
  "examples": [
    {
      "title": "Step Title",
      "summary": "Step Summary",
      "description": "Step Description",
      "objectives": [
        "home",
        "green"
      ],
      "tags": [
        {
          "slug": "music-production",
          "name": "Music Production",
          "customOption": true
        },
        {
          "slug": "games-production",
          "name": "Games Production"
        }
      ]
    }
  ],
  "required": [
    "language"
  ],
  "properties": {
    "language": {
      "$id": "#/properties/language",
      "type": "string",
      "title": "Choose Default Language",
      "description": "Choose Default Language",
      "default": "en-us",
      "anyOf": [
        {
          "type": "string",
          "title": "English",
          "enum": [
            "en-us"
          ]
        },
        {
          "type": "string",
          "title": "Japanese",
          "enum": [
            "ja"
          ]
        }
      ],
      "examples": [
        "en-us"
      ]
    },
    "title": {
      "$id": "#/properties/title",
      "type": "string",
      "title": "Step Title",
      "description": "Enter Step Title",
      "default": "",
      "tooltip": "Step title has a 100 character limit. It displays in various locations as part of a Path it is added to.",
      "examples": [
        "Step Title"
      ],
      "maxLength": 100
    },
    "summary": {
      "$id": "#/properties/summary",
      "type": "string",
      "title": "Step Summary",
      "description": "Enter Step Summary",
      "default": "",
      "tooltip": "Step Summary has a 300 character limit. It displays in various locations as part of a Path it is added to and provides a short and concise summary.",
      "examples": [
        "Step Summary"
      ],
      "maxLength": 300
    },
    "description": {
      "$id": "#/properties/description",
      "type": "string",
      "title": "Description",
      "description": "Enter Step Description for Logged in Students (Optional)",
      "default": "",
      "tooltip": "Step description can be seen by instructors and curriculum creators. It is used to provide a more detailed description than the short summary.",
      "examples": [
        "Step Description"
      ],
      "maxLength": 300
    },
    "objectives": {
      "$id": "#/properties/objectives",
      "default": [
        ""
      ],
      "type": "array",
      "title": "Objectives",
      "description": "Enter Step Objectives for Logged in Students (Optional)",
      "tooltip": "Step objectives can be seen by instructors and curriculum creators and is used to outline the goals.",
      "additionalItems": true,
      "minItems": 0,
      "items": {
        "$id": "#/properties/objectives/items",
        "title": "Enter Step Objectives for Logged in Students (Optional)",
        "type": "string"
      }
    },
    "tags": {
      "$id": "#/properties/tags",
      "default": [],
      "description": "Choose Tags",
      "examples": [
        [
          {
            "slug": "music-production",
            "name": "Music Production",
            "customOption": true
          },
          {
            "slug": "games-production",
            "name": "Games Production"
          }
        ]
      ],
      "title": "Choose Tags",
      "minItems": 1,
      "type": "array",
      "additionalItems": true,
      "items": {
        "$id": "#/properties/tags/items",
        "type": "object",
        "examples": [
          {
            "slug": "music-production",
            "name": "Music Production",
            "customOption": true
          }
        ],
        "required": [
          "slug",
          "name"
        ],
        "properties": {
          "slug": {
            "$id": "#/properties/tags/items/slug",
            "type": "string",
            "title": "Slug",
            "examples": [
              "music-production"
            ]
          },
          "name": {
            "$id": "#/properties/tags/items/name",
            "type": "string",
            "title": "Name",
            "examples": [
              "Music Production"
            ]
          },
          "customOption": {
            "$id": "#/properties/tags/items/customOption",
            "type": "boolean",
            "title": "customOption",
            "examples": [
              true
            ]
          }
        },
        "additionalProperties": true
      }
    }
  },
  "additionalProperties": false,
  "if": {
    "properties": {
      "language": {
        "const": "en-us"
      }
    }
  },
  "then": {
    "properties": {
      "title": {
        "$id": "#/properties/title",
        "type": "string",
        "title": "Step Title",
        "description": "Enter Step Title",
        "default": "",
        "tooltip": "Step title has a 100 character limit. It displays in various locations as part of a Path it is added to.",
        "examples": [
          "Step Title"
        ],
        "maxLength": 100
      },
      "summary": {
        "$id": "#/properties/summary",
        "type": "string",
        "title": "Step Summary",
        "description": "Enter Step Summary",
        "default": "",
        "tooltip": "Step Summary has a 300 character limit. It displays in various locations as part of a Path it is added to and provides a short and concise summary.",
        "examples": [
          "Step Summary"
        ],
        "maxLength": 300
      },
      "tags": {
        "$id": "#/properties/tags",
        "default": [],
        "description": "Choose Tags",
        "examples": [
          [
            {
              "slug": "music-production",
              "name": "Music Production",
              "customOption": true
            },
            {
              "slug": "games-production",
              "name": "Games Production"
            }
          ]
        ],
        "title": "Choose Tags",
        "minItems": 1,
        "type": "array",
        "additionalItems": true,
        "items": {
          "$id": "#/properties/tags/items",
          "type": "object",
          "examples": [
            {
              "slug": "music-production",
              "name": "Music Production",
              "customOption": true
            }
          ],
          "required": [
            "slug",
            "name"
          ],
          "properties": {
            "slug": {
              "$id": "#/properties/tags/items/slug",
              "type": "string",
              "title": "Slug",
              "examples": [
                "music-production"
              ]
            },
            "name": {
              "$id": "#/properties/tags/items/name",
              "type": "string",
              "title": "Name",
              "examples": [
                "Music Production"
              ]
            },
            "customOption": {
              "$id": "#/properties/tags/items/customOption",
              "type": "boolean",
              "title": "customOption",
              "examples": [
                true
              ]
            }
          },
          "additionalProperties": true
        }
      }
    },
    "required": [
      "title",
      "summary",
      "tags"
    ]
  }
}

payload to validate:

{ "language": "ja" }

I've resolved. No need to include properties inside at the base level that are added to the then

Bad:

...
properties:
  name:
    type: "string"
  description:  # don't include the description property if it will be in the then below
    type: "string"
  required: [ "name" ]
  if:
    properties:
      name:
        const: "fido"
  then: 
    properties:
      description: 
        type: "string"
    required: [ "description" ]

good:

...
properties:
  name:
    type: "string"
  required: [ "name" ]
  if:
    properties:
      name:
        const: "fido"
  then: 
    properties:
      description: 
        type: "string"
    required: [ "description" ]

Thanks for using the library!