lib/meta/json_schema/schemas/properties.rb in meta-api-0.0.6 vs lib/meta/json_schema/schemas/properties.rb in meta-api-0.0.7

- old
+ new

@@ -90,19 +90,24 @@ # 第二步,递归过滤每一个属性 object = {} errors = {} filtered_properties.each do |name, property_schema| - value = resolve_property_value(object_value, name, property_schema, stage) + value = resolve_property_value(object_value, name, property_schema) begin object[name] = property_schema.filter(value, **user_options, object_value: object_value) rescue JsonSchema::ValidationErrors => e errors.merge! e.prepend_root(name).errors end end.to_h + # 第三步,检测是否有剩余的属性 + if user_options[:extra_properties] == :raise_error && !(object_value.keys.map(&:to_sym) - properties.keys).empty? + raise JsonSchema::ValidationError, '遇到多余的属性' + end + if errors.empty? object else raise JsonSchema::ValidationErrors.new(errors) end @@ -120,10 +125,14 @@ end # 程序中有些地方用到了这三个方法 def_delegators :@properties, :empty?, :key?, :[] + def merge(properties) + self.class.new(@properties.merge(properties.instance_eval { @properties })) + end + def self.build_property(*args) StagingProperty.build(*args) end private @@ -141,10 +150,10 @@ properties.transform_values do |property| property.stage(stage).schema end end - def resolve_property_value(object_value, name, property_schema, stage) + def resolve_property_value(object_value, name, property_schema) if property_schema.value? nil elsif object_value.is_a?(Hash) || object_value.is_a?(ObjectWrapper) object_value.key?(name.to_s) ? object_value[name.to_s] : object_value[name.to_sym] else