Sha256: 20952709c3100e916599b52300d674f45bf8339e8057fe49c482c4f752241012

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

module JSON
  class Schema
    class PropertiesAttribute < Attribute
      def self.validate(current_schema, data, fragments, processor, validator, options = {})
        if data.is_a?(Hash)
          current_schema.schema['properties'].each do |property,property_schema|
            if !data.has_key?(property.to_s) &&
               !data.has_key?(property.to_sym) &&
               property_schema['default'] &&
               !property_schema['readonly'] &&
               options[:insert_defaults]
              default = property_schema['default']
              data[property.to_s] = (default.is_a?(Hash) ? default.clone : default)
            end

            if property_schema['required'] && !data.has_key?(property.to_s) && !data.has_key?(property.to_sym)
              message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
              validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
            end

            if data.has_key?(property.to_s) || data.has_key?(property.to_sym)
              schema = JSON::Schema.new(property_schema,current_schema.uri,validator)
              fragments << property.to_s
              schema.validate(data[property.to_s],fragments,processor,options)
              fragments.pop
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
json-schema-2.1.9 lib/json-schema/attributes/properties.rb
json-schema-2.1.8 lib/json-schema/attributes/properties.rb
json-schema-2.1.7 lib/json-schema/attributes/properties.rb
json-schema-2.1.6 lib/json-schema/attributes/properties.rb