1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
require 'json-schema/attribute'
module JSON
class Schema
class LimitAttribute < Attribute
def self.validate(current_schema, data, fragments, processor, validator, options = {})
schema = current_schema.schema
return unless data.is_a?(acceptable_type) && invalid?(schema, value(data))
property = build_fragment(fragments)
description = error_message(schema)
message = format("The property '%s' %s", property, description)
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
end
def self.invalid?(schema, data)
exclusive = exclusive?(schema)
limit = limit(schema)
if limit_name.start_with?('max')
exclusive ? data >= limit : data > limit
else
exclusive ? data <= limit : data < limit
end
end
def self.limit(schema)
schema[limit_name]
end
def self.exclusive?(schema)
false
end
def self.value(data)
data
end
def self.acceptable_type
raise NotImplementedError
end
def self.error_message(schema)
raise NotImplementedError
end
def self.limit_name
raise NotImplementedError
end
end
class MinLengthAttribute < LimitAttribute
def self.acceptable_type
String
end
def self.limit_name
'minLength'
end
def self.error_message(schema)
"was not of a minimum string length of #{limit(schema)}"
end
def self.value(data)
data.length
end
end
class MaxLengthAttribute < MinLengthAttribute
def self.limit_name
'maxLength'
end
def self.error_message(schema)
"was not of a maximum string length of #{limit(schema)}"
end
end
class MinItemsAttribute < LimitAttribute
def self.acceptable_type
Array
end
def self.value(data)
data.length
end
def self.limit_name
'minItems'
end
def self.error_message(schema)
"did not contain a minimum number of items #{limit(schema)}"
end
end
class MaxItemsAttribute < MinItemsAttribute
def self.limit_name
'maxItems'
end
def self.error_message(schema)
"had more items than the allowed #{limit(schema)}"
end
end
class MinPropertiesAttribute < LimitAttribute
def self.acceptable_type
Hash
end
def self.value(data)
data.size
end
def self.limit_name
'minProperties'
end
def self.error_message(schema)
"did not contain a minimum number of properties #{limit(schema)}"
end
end
class MaxPropertiesAttribute < MinPropertiesAttribute
def self.limit_name
'maxProperties'
end
def self.error_message(schema)
"had more properties than the allowed #{limit(schema)}"
end
end
class NumericLimitAttribute < LimitAttribute
def self.acceptable_type
Numeric
end
def self.error_message(schema)
exclusivity = exclusive?(schema) ? 'exclusively' : 'inclusively'
format("did not have a %s value of %s, %s", limit_name, limit(schema), exclusivity)
end
end
class MaximumAttribute < NumericLimitAttribute
def self.limit_name
'maximum'
end
def self.exclusive?(schema)
schema['exclusiveMaximum']
end
end
class MaximumInclusiveAttribute < MaximumAttribute
def self.exclusive?(schema)
schema['maximumCanEqual'] == false
end
end
class MinimumAttribute < NumericLimitAttribute
def self.limit_name
'minimum'
end
def self.exclusive?(schema)
schema['exclusiveMinimum']
end
end
class MinimumInclusiveAttribute < MinimumAttribute
def self.exclusive?(schema)
schema['minimumCanEqual'] == false
end
end
end
end
|