File: propertynames.rb

package info (click to toggle)
ruby-json-schema 6.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 472 kB
  • sloc: ruby: 2,494; makefile: 4
file content (23 lines) | stat: -rw-r--r-- 872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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