How do I enforce Major Minor Patch versioning in a JSON Schema?
For example, this would validate:
{ "version": "1.0.1" }
This would NOT validate:
{ "version": "1.0" }
Figured it out, example:
"version": {
"type": "string",
"pattern": "^([0-9]+).([0-9]+).([0-9]+)$",
"description": "Version (Major, Minor, Patch) identifier"
},
Related
I am trying to learn json schema, but something isn't working out for me.
I'm trying to run the example from http://json-schema.org/understanding-json-schema/reference/conditionals.html#id4 for dependentSchemas, but it just doesn't validate.
I'm using this schema:
check_schema = {"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"credit_card": { "type": "number" }
},
"required": ["name"],
"dependentSchemas": {
"credit_card": {
"properties": {
"billing_address": { "type": "string" }
},
"required": ["billing_address"]
}
}
}
and this json, that should raise an error since it is missing the key billing_address:
check_dict={
"name": "John Doe",
"credit_card": 5555555555555555
}
but when I use jsonschema.validate(dic_check, schema_check) (with python, jsonschema package version 4.2.1), the validation passes with no issues.
What am I doing wrong here?
If you are using an implementation that doesn't support at least draft2019-09 of the specification, dependentSchemas won't be recognized as a keyword. In earlier versions (draft7 and before), that keyword was known as dependencies, with the same syntax (actually, dependencies was split into two, dependentSchemas and dependentRequired).
The details are described on the page you linked, https://json-schema.org/understanding-json-schema/reference/conditionals.html#dependentschemas.
If you still believe that what you have should work, I suggest you open a bug report on the implementation's issue queue.
I'm using Swashbuckle (5.0.0-rc2, I know there is newer versions but I do not have the control on that point) for multiple ASP.NET Core projects.
For all projects (except one) everything is working fine, the specs generated are OK and the "default" value for "in" parameters only appear when I explicitly put it into the code:
public async Task<ActionResult<object[]>> Search(string searchString, [FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 10)
So the specs will look like:
{
"name": "searchString",
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "pageNumber",
"in": "query",
"schema": {
"type": "integer",
"format": "int32",
"default": 1
}
},
{
"name": "pageSize",
"in": "query",
"schema": {
"type": "integer",
"format": "int32",
"default": 10
}
}
The "default" property is only displayed when it's in the code.
For my other project (so same nuget version and almost same configuration) the behavior is different and I have the "default" property for each parameter but empty. In the code there is no default value:
{
"name": "startDate",
"in": "query",
"required": true,
"schema": {
"type": "string",
"format": "date-time",
"default": ""
}
The issue is when I'm validating the semantic of the specs (which is done when I upload the file to Azure API Manager) it causes some issue: The default value "empty" is of course not valid for a date-time value.
Only difference in swagger configuration is the second project is using a custom way to generated schema ID... But I got the issue with "in" parameter with primitive type so it should not have any influence. Other configurations are exactly the same (we're using an internal nuget package to ensure that).
Any idea?
I am using JSON schema to build a form and I have an object in "definitions" which I am referencing using $ref in two different places on the schema. At one of the instances I need to have one more property added to the referenced object, how can I achieve this?
{
"definitions": {
"settingsProperties": {
"$id": "#/definitions/settingsProperties",
"type": "object",
"properties": {
"thickness": {
"$id": "#/properties/defaultLayerSettings/thickness",
"type": "number",
"title": "Thickness:",
}
}
}
},
"properties": {
"layerSettings": {
"$id": "#/properties/layerSettings",
"type": "array",
"title": "Dynamic Layer Settings:",
"items": {
"title": "Dynamic Settings",
"type": "object",
"$ref": "#/definitions/settingsProperties", PLUS startLayer PROPERTY!!!!!!!!!!!!!!
"required": [
"startLayer"
]
}
}
}
Just add "properties": { "startLayer": { ... } } underneath the required keyword.
Note that if you are using any specification version earlier than draft 2019-09 (the current latest version), you will have to nest the $ref keyword inside an allOf. Additionally, the use of fragments (strings that include #) are not permitted in the $id keyword, although some outdated tools are generating schemas with this structure.
Suppose I have two schema being used to validate a json file.
testSchema.json
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"$schema": { "type": "string" },
"sample": { "type": "number" }
},
"anyOf": [
{ "$ref": "./testSchema2.json" },
{}
]
}
testSchema2.json
{
"$schema": "http://json-schema.org/draft-04/schema",
"type": "object",
"properties": {
"test": { "type": "string" },
"test2": { "type": "number" }
}
}
test.json
{
"$schema": "../testSchema.json",
"sample": 0,
"test": "some text" //this line throws error "Property is not allowed"
}
I'd like for the file to be validated against the included schema's properties and any schema that is referenced's properties. Am I missing something?
Edit: I want to exclude any objects that are not explicitly defined in any of my included/referenced schema.
From JSON Schema draft 2019-09 (after draft-07), this is possible by using the unevaluatedProperties keyword.
additionalProperties cannot "see through" applicator keywords such as "anyOf" and "$ref", and only works based on the properties in the same schema object.
This is not possible with draft-07 or previous.
I have a table of acceptable input combinations for my application:
noises appearance
------ ----------
squeaks fluffy
purrs fluffy
hisses fluffy
peeps feathers
chirps feathers
squeaks feathers
hisses scaly
No other combination of values is acceptable.
How can this be encoded in JSON Schema? The "rest of the schema" would look kind of like this:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"required": ["noise", "appearance"]
"properties": {
"noise": ...,
"appearance": ...
}
}
Currently my application is using Draft 4 because it's what's supported by the last stable version of the jsonschema package.
Given that there are a small and fixed number of options, I think the best thing is to enumerate all the options. The schema will be easier to read and maintain than the alternatives.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"required": ["noise", "appearance"],
"properties": {
... any common properties ...
},
"anyOf": [
{
"properties": {
"noise": { "enum": ["squeaks"] },
"appearance": { "enum": ["fluffy"] }
}
},
{
"properties": {
"noise": { "enum": ["purrs"] },
"appearance": { "enum": ["fluffy"] }
}
},
... other combinations ...
]
}
}