From 756859529cafa3019b9dfab50b73b8e08b2f98d0 Mon Sep 17 00:00:00 2001 From: david Date: Fri, 20 Apr 2018 08:47:29 +0200 Subject: [PATCH] Added propertyNames validator to draft6 --- CHANGELOG.md | 2 ++ lib/json-schema/attributes/propertynames.rb | 23 +++++++++++++++++++++ lib/json-schema/validators/draft6.rb | 3 ++- test/support/test_suite_ignored_tests.yml | 1 - 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 lib/json-schema/attributes/propertynames.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a9a46b6..007e43db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Please keep to the changelog format described on [keepachangelog.com](http://kee This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +- Draft6 validator now supports `propertyNames` validation. ## [2.8.1] - 2018-10-14 diff --git a/lib/json-schema/attributes/propertynames.rb b/lib/json-schema/attributes/propertynames.rb new file mode 100644 index 00000000..d71ee180 --- /dev/null +++ b/lib/json-schema/attributes/propertynames.rb @@ -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 diff --git a/lib/json-schema/validators/draft6.rb b/lib/json-schema/validators/draft6.rb index cd748dfe..5e2e7b69 100644 --- a/lib/json-schema/validators/draft6.rb +++ b/lib/json-schema/validators/draft6.rb @@ -34,7 +34,8 @@ def initialize "additionalItems" => JSON::Schema::AdditionalItemsAttribute, "dependencies" => JSON::Schema::DependenciesV4Attribute, "extends" => JSON::Schema::ExtendsAttribute, - "$ref" => JSON::Schema::RefAttribute + "$ref" => JSON::Schema::RefAttribute, + "propertyNames" => JSON::Schema::PropertyNames } @default_formats = { 'date-time' => DateTimeV4Format, diff --git a/test/support/test_suite_ignored_tests.yml b/test/support/test_suite_ignored_tests.yml index 02efe74c..ee5569f8 100644 --- a/test/support/test_suite_ignored_tests.yml +++ b/test/support/test_suite_ignored_tests.yml @@ -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