Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values over merged schemas #37

Open
burak-koksal-jotform opened this issue Dec 13, 2020 · 0 comments
Open

Default values over merged schemas #37

burak-koksal-jotform opened this issue Dec 13, 2020 · 0 comments

Comments

@burak-koksal-jotform
Copy link

burak-koksal-jotform commented Dec 13, 2020

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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant