Skip to content

Commit

Permalink
Added support for the provisional draft6 of json-schema
Browse files Browse the repository at this point in the history
  • Loading branch information
iainbeeston committed Jan 30, 2017
1 parent 56fe076 commit 89fd412
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 7 deletions.
56 changes: 56 additions & 0 deletions lib/json-schema/validators/draft6.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'json-schema/schema/validator'

module JSON
class Schema

class Draft6 < Validator
def initialize
super
@attributes = {
"type" => JSON::Schema::TypeV4Attribute,
"allOf" => JSON::Schema::AllOfAttribute,
"anyOf" => JSON::Schema::AnyOfAttribute,
"oneOf" => JSON::Schema::OneOfAttribute,
"not" => JSON::Schema::NotAttribute,
"disallow" => JSON::Schema::DisallowAttribute,
"format" => JSON::Schema::FormatAttribute,
"maximum" => JSON::Schema::MaximumAttribute,
"minimum" => JSON::Schema::MinimumAttribute,
"minItems" => JSON::Schema::MinItemsAttribute,
"maxItems" => JSON::Schema::MaxItemsAttribute,
"minProperties" => JSON::Schema::MinPropertiesAttribute,
"maxProperties" => JSON::Schema::MaxPropertiesAttribute,
"uniqueItems" => JSON::Schema::UniqueItemsAttribute,
"minLength" => JSON::Schema::MinLengthAttribute,
"maxLength" => JSON::Schema::MaxLengthAttribute,
"multipleOf" => JSON::Schema::MultipleOfAttribute,
"enum" => JSON::Schema::EnumAttribute,
"properties" => JSON::Schema::PropertiesV4Attribute,
"required" => JSON::Schema::RequiredAttribute,
"pattern" => JSON::Schema::PatternAttribute,
"patternProperties" => JSON::Schema::PatternPropertiesAttribute,
"additionalProperties" => JSON::Schema::AdditionalPropertiesAttribute,
"items" => JSON::Schema::ItemsAttribute,
"additionalItems" => JSON::Schema::AdditionalItemsAttribute,
"dependencies" => JSON::Schema::DependenciesV4Attribute,
"extends" => JSON::Schema::ExtendsAttribute,
"$ref" => JSON::Schema::RefAttribute
}
@default_formats = {
'date-time' => DateTimeV4Format,
'ipv4' => IP4Format,
'ipv6' => IP6Format,
'uri' => UriFormat
}
@formats = @default_formats.clone
@uri = JSON::Util::URI.parse("http://json-schema.org/draft-06/schema#")
@names = ["draft6", "http://json-schema.org/draft-06/schema#"]
@metaschema_name = "draft-06.json"
end

JSON::Validator.register_validator(self.new)
JSON::Validator.register_default_validator(self.new)
end

end
end
13 changes: 13 additions & 0 deletions lib/json-schema/validators/hyper-draft6.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JSON
class Schema

class HyperDraft6 < Draft6
def initialize
super
@uri = JSON::Util::URI.parse("http://json-schema.org/draft-06/hyper-schema#")
end

JSON::Validator.register_validator(self.new)
end
end
end
150 changes: 150 additions & 0 deletions resources/draft-06.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"id": "http://json-schema.org/draft-06/schema#",
"$schema": "http://json-schema.org/draft-06/schema#",
"description": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
"positiveInteger": {
"type": "integer",
"minimum": 0
},
"positiveIntegerDefault0": {
"allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
},
"simpleTypes": {
"enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
},
"stringArray": {
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"uniqueItems": true
}
},
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uri"
},
"$schema": {
"type": "string",
"format": "uri"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": {},
"multipleOf": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "boolean",
"default": false
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "boolean",
"default": false
},
"maxLength": { "$ref": "#/definitions/positiveInteger" },
"minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": {
"anyOf": [
{ "type": "boolean" },
{ "$ref": "#" }
],
"default": {}
},
"items": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/schemaArray" }
],
"default": {}
},
"maxItems": { "$ref": "#/definitions/positiveInteger" },
"minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
"uniqueItems": {
"type": "boolean",
"default": false
},
"maxProperties": { "$ref": "#/definitions/positiveInteger" },
"minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
"required": { "$ref": "#/definitions/stringArray" },
"additionalProperties": {
"anyOf": [
{ "type": "boolean" },
{ "$ref": "#" }
],
"default": {}
},
"definitions": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/stringArray" }
]
}
},
"enum": {
"type": "array",
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{ "$ref": "#/definitions/simpleTypes" },
{
"type": "array",
"items": { "$ref": "#/definitions/simpleTypes" },
"minItems": 1,
"uniqueItems": true
}
]
},
"allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" },
"not": { "$ref": "#" }
},
"dependencies": {
"exclusiveMaximum": [ "maximum" ],
"exclusiveMinimum": [ "minimum" ]
},
"default": {}
}
17 changes: 10 additions & 7 deletions test/custom_format_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@

class CustomFormatTest < Minitest::Test
def setup
@all_versions = ['draft1', 'draft2', 'draft3', 'draft4', nil]
@all_versions = ['draft1', 'draft2', 'draft3', 'draft4', 'draft6', nil]
@format_proc = lambda { |value| raise JSON::Schema::CustomFormatError.new("must be 42") unless value == "42" }
@schema_4 = {
"$schema" => "http://json-schema.org/draft-04/schema#",
@schema_6 = {
"$schema" => "http://json-schema.org/draft-06/schema#",
"properties" => {
"a" => {
"type" => "string",
"format" => "custom",
},
}
}
@schema_3 = @schema_4.clone
@schema_4 = @schema_6.clone
@schema_4["$schema"] = "http://json-schema.org/draft-04/schema#"
@schema_3 = @schema_6.clone
@schema_3["$schema"] = "http://json-schema.org/draft-03/schema#"
@schema_2 = @schema_4.clone
@schema_2 = @schema_6.clone
@schema_2["$schema"] = "http://json-schema.org/draft-02/schema#"
@schema_1 = @schema_4.clone
@schema_1 = @schema_6.clone
@schema_1["$schema"] = "http://json-schema.org/draft-01/schema#"
@default = @schema_4.clone
@default = @schema_6.clone
@default.delete("$schema")
@schemas = {
"draft1" => @schema_1,
"draft2" => @schema_2,
"draft3" => @schema_3,
"draft4" => @schema_4,
"draft6" => @schema_6,
nil => @default,
}
JSON::Validator.restore_default_formats
Expand Down

0 comments on commit 89fd412

Please sign in to comment.