Skip to content

Default values over merged schemas #37

Open
@burak-koksal-jotform

Description

@burak-koksal-jotform

Hello,
I'm trying to build a mechanism to versioning default values for my app over ajv backed separated JSON files.

TL; DR:

I couldnt generate default values from a merged schema. Please elaborate on what i'm missing or doing wrong.

Details:

You can find the sandbox here

My concerns are:

  • Use default values and validate the data only related props according to the version
  • Use v0 as a base JSON and struct upcoming versions via $merge/$patch keywords (Dont repeat unchanged props and definitions)
  • Use a separeted common definitions JSON file to feed the base or merged/patched schemas.

A simple example goes like this:

/schemas/v0/App.json:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://some.domain.com/schemas/v0/App.json#",
  "title": "App Schema v0",
  "description": "App Properties",
  "type": "object",
  "additionalProperties": false,
  "definitions": {
    "yesNo": {
      "$ref": "https://some.domain.com/schemas/commonDefinitions.json#/definitions/yesNo"
    },
    "description": {
      "$ref": "https://some.domain.com/schemas/commonDefinitions.json#/definitions/description"
    },
    "color": {
      "$ref": "https://some.domain.com/schemas/commonDefinitions.json#/definitions/color"
    },
    "URL": {
      "$ref": "https://some.domain.com/schemas/commonDefinitions.json#/definitions/color"
    }
  },
  "properties": {
    "appVersion": {
      "type": "string",
      "const": "0",
      "default": "0"
    },
    "appBgColor": {
      "$ref": "#/definitions/color",
      "description": "App bg color",
      "default": "#ffffff"
    },
    "name": {
      "type": "string",
      "default": "New App"
    },
    "description": {
      "$ref": "#/definitions/description",
      "default": "my app"
    },
    "logoURL": {
      "$ref": "#/definitions/URL",
      "description": "Logo asset URL",
      "default": "https://source.unsplash.com/random/50x50"
    }
  },
  "required": ["appVersion"]
}

/schemas/v1/App.json:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://some.domain.com/schemas/v1/App.json#",
  "$merge": {
    "source": {
      "$ref": "https://some.domain.com/schemas/v0/App.json#"
    },
    "with": {
      "properties": {
        "appVersion": {
          "const": "1",
          "default": "1"
        },
        "showItemIcons": {
          "$ref": "#/definitions/yesNo",
          "default": "Nope"
        }
      }
    }
  }
}

/schemas/commonDefinitions.json:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://some.domain.com/schemas/commonDefinitions.json#",
  "title": "App Schema Common Definitions",
  "description": "Common Definitions",
  "type": "object",
  "additionalProperties": false,
  "definitions": {
    "yesNo": {
      "type": "string",
      "enum": ["Yes", "No"]
    },
    "alignment": {
      "type": "string",
      "enum": ["left", "center", "right"]
    },
    "description": {
      "type": "string",
      "maxLength": 5
    },
    "color": {
      "type": "string"
    },
    "URL": {
      "type": "string"
    },
    "empty": {
      "type": "string",
      "maxLength": 0
    }
  }
}

I couldn't make ajv to produce correct data with defaults on v1/App.json:

{}

However it's ok for v0/App.json:

{"appVersion":"0","appBgColor":"#ffffff","name":"New App","description":"my app","logoURL":"https://source.unsplash.com/random/50x50"}

Additional problems:

  • It seems validation ignore default values. See v0/App.json#properties.description shouldn't exceed 5 length value. But validator tells it's valid for 'my app'.
  • I couldn't make codesandbox work by referencing relatively within schemas. Is it ok to access other schemas as:
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://www.jotform.com/portal/schemas/v1/App.json#",
  "$merge": {
    "source": {
      "$ref": "../v0/App.json"
    },
    "with": {
      ......
    }
}
  • And finally please see that i need 'yesNo' definition starting from v1. However it seems it needs to be defined in also v0. I got MissingRefErrors if i dont include it to v0. What is the correct way to extend definitions and required?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions