Skip to content

Commit

Permalink
Merge pull request #407 from nlpgo/master
Browse files Browse the repository at this point in the history
Add propertyNames validator to draft6
  • Loading branch information
bastelfreak authored Feb 1, 2023
2 parents 8f24e05 + fac9e86 commit 5ae8c63
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
23 changes: 23 additions & 0 deletions lib/json-schema/attributes/propertynames.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'json-schema/attribute'

module JSON
class Schema
class PropertyNames < Attribute
def self.validate(current_schema, data, fragments, processor, validator, options = {})
return unless data.is_a?(Hash)

propnames = current_schema.schema['propertyNames']

if propnames.is_a?(Hash)
schema = JSON::Schema.new(propnames, current_schema.uri, validator)
data.each_key do |key|
schema.validate(key, fragments + [key], processor, options)
end
elsif propnames == false && data.any?
message = "The property '#{build_fragment(fragments)}' contains additional properties #{data.keys.inspect} outside of the schema when none are allowed"
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/json-schema/validators/draft6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def initialize
"dependencies" => JSON::Schema::DependenciesV4Attribute,
"extends" => JSON::Schema::ExtendsAttribute,
"const" => JSON::Schema::ConstAttribute,
"$ref" => JSON::Schema::RefAttribute
"$ref" => JSON::Schema::RefAttribute,
"propertyNames" => JSON::Schema::PropertyNames
}
@default_formats = {
'date-time' => DateTimeV4Format,
Expand Down
24 changes: 24 additions & 0 deletions test/draft6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,28 @@ def test_const_attribute
data = {:a => 6, :b => "foo"}
refute_valid schema, data
end

def test_property_names
schema = {
"type" => "object",
"propertyNames" => {"const" => "foo"}
}

data = {"foo" => "value"}
assert_valid schema, data

data = {"bar" => "value"}
refute_valid schema, data

schema = {
"type" => "object",
"propertyNames" => false
}

data = {}
assert_valid schema, data

data = {"foo" => "value"}
refute_valid schema, data
end
end
60 changes: 59 additions & 1 deletion test/support/test_suite_ignored_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ draft6:
- properties with boolean schema/only 'false' property present is invalid
- properties with boolean schema/no property present is valid
- properties with boolean schema/both properties present is invalid
propertyNames: :all
ref:
- ref overrides any sibling keywords/remote ref valid, maxItems ignored
- ref overrides any sibling keywords/ref valid, maxItems ignored
Expand All @@ -107,3 +106,62 @@ draft6:
- ECMA 262 regex non-compliance/ECMA 262 has no support for \Z anchor from .NET
zeroTerminatedFloats:
- some languages do not distinguish between different types of numeric value/a float is not an integer even without fractional part
draft7:
optional:
format:
date: :all
date-time: :all
email: :all
hostname: :all
idn-email: :all
idn-hostname: :all
ipv4: :all
ipv6: :all
iri: :all
iri-reference: :all
json-pointer: :all
regex: :all
relative-json-pointer: :all
time: :all
uri: :all
uri-reference: :all
uri-template: :all
bignum: :all
content: :all
ecmascript-regex: :all
zeroTerminatedFloats: :all
additionalItems: :all
additionalProperties: :all
allOf: :all
anyOf: :all
boolean_schema: :all
const: :all
contains: :all
default: :all
definitions: :all
dependencies: :all
enum: :all
exclusiveMaximum: :all
exclusiveMinimum: :all
if-then-else: :all
items: :all
maximum: :all
maxItems: :all
maxLength: :all
maxProperties: :all
minimum: :all
minItems: :all
minLength: :all
minProperties: :all
multipleOf: :all
not: :all
oneOf: :all
pattern: :all
patternProperties: :all
properties: :all
propertyNames: :all
ref: :all
refRemote: :all
required: :all
type: :all
uniqueItems: :all

0 comments on commit 5ae8c63

Please sign in to comment.