Class: MediaTypes::Scheme
- Inherits:
-
Object
- Object
- MediaTypes::Scheme
- Defined in:
- lib/media_types/scheme.rb,
lib/media_types/scheme/links.rb,
lib/media_types/scheme/any_of.rb,
lib/media_types/scheme/allow_nil.rb,
lib/media_types/scheme/attribute.rb,
lib/media_types/scheme/not_strict.rb,
lib/media_types/scheme/missing_validation.rb,
lib/media_types/scheme/validation_options.rb,
lib/media_types/scheme/enumeration_context.rb,
lib/media_types/scheme/enumeration_of_type.rb
Overview
Media Type Schemes can validate content to a media type, by itself. Used by the validations dsl.
Defined Under Namespace
Classes: Attribute, CaseEqualityWithList, CaseEqualityWithNil, CollectionTypeError, EmptyOutputError, EnumerationContext, EnumerationOfType, ExhaustedOutputError, Links, MissingValidation, NotStrict, StrictValidationError, ValidationError, ValidationOptions
Instance Method Summary collapse
-
#AllowNil(klazz) ⇒ CaseEqualityWithNil
noinspection RubyInstanceMethodNamingConvention.
-
#any(scheme = nil, force: ::Hash, allow_empty: false, &block) ⇒ Object
Allow for any key.
-
#AnyOf(*klazzes) ⇒ CaseEqualityWithList
noinspection RubyInstanceMethodNamingConvention.
-
#attribute(key, type = String, **opts, &block) ⇒ Object
Adds an attribute to the schema If a
blockis given, uses that to test against instead oftype. -
#collection(key, scheme = nil, allow_empty: false, force: Array, &block) ⇒ Object
Expect a collection such as an array or hash.
-
#initialize(allow_empty: false, force: nil) ⇒ Scheme
constructor
Creates a new scheme.
-
#link(*args, **opts, &block) ⇒ Object
Expect a link.
-
#not_strict ⇒ Object
Allow for extra keys in the schema/collection even when passing strict: true to #validate!.
-
#valid?(output, **opts) ⇒ TrueClass, FalseClass
Checks if the
outputis valid. -
#validate(output, options = nil, **opts) ⇒ TrueClass
Validates the
outputand raises on certain validation errors. - #validate!(output, call_options, **_opts) ⇒ Object
Constructor Details
#initialize(allow_empty: false, force: nil) ⇒ Scheme
Creates a new scheme
60 61 62 63 64 65 66 |
# File 'lib/media_types/scheme.rb', line 60 def initialize(allow_empty: false, force: nil) self.validations = {} self.allow_empty = allow_empty self.force = force validations.default = MissingValidation.new end |
Instance Method Details
#AllowNil(klazz) ⇒ CaseEqualityWithNil
noinspection RubyInstanceMethodNamingConvention
Allows the wrapped klazz to be nil
21 22 23 |
# File 'lib/media_types/scheme/allow_nil.rb', line 21 def AllowNil(klazz) # rubocop:disable Naming/MethodName CaseEqualityWithNil.new(klazz) end |
#any(scheme = nil, force: ::Hash, allow_empty: false, &block) ⇒ Object
Allow for any key.
The +&block+ defines the Schema for each value.
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/media_types/scheme.rb', line 210 def any(scheme = nil, force: ::Hash, allow_empty: false, &block) unless block_given? return validations.default = scheme end scheme = Scheme.new(allow_empty: allow_empty, force: force) scheme.instance_exec(&block) validations.default = scheme end |
#AnyOf(*klazzes) ⇒ CaseEqualityWithList
noinspection RubyInstanceMethodNamingConvention
Allows it to be any of the wrapped klazzes
21 22 23 |
# File 'lib/media_types/scheme/any_of.rb', line 21 def AnyOf(*klazzes) # rubocop:disable Naming/MethodName CaseEqualityWithList.new(klazzes) end |
#attribute(key, type = String, **opts, &block) ⇒ Object
Adds an attribute to the schema
If a +block+ is given, uses that to test against instead of +type+
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/media_types/scheme.rb', line 171 def attribute(key, type = String, **opts, &block) if block_given? return collection(key, force: ::Hash, **opts, &block) end if type.is_a?(Scheme) return validations[key] = type end validations[key] = Attribute.new(type, **opts, &block) end |
#collection(key, scheme = nil, allow_empty: false, force: Array, &block) ⇒ Object
Expect a collection such as an array or hash.
The +block+ defines the Schema for each item in that collection.
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/media_types/scheme.rb', line 286 def collection(key, scheme = nil, allow_empty: false, force: Array, &block) unless block_given? if scheme.is_a?(Scheme) return validations[key] = scheme end return validations[key] = EnumerationOfType.new( scheme, enumeration_type: force, allow_empty: allow_empty ) end scheme = Scheme.new(allow_empty: allow_empty, force: force) scheme.instance_exec(&block) validations[key] = scheme end |
#link(*args, **opts, &block) ⇒ Object
Expect a link
339 340 341 342 343 344 345 |
# File 'lib/media_types/scheme.rb', line 339 def link(*args, **opts, &block) validations.fetch(:_links) do Links.new.tap do |links| validations[:_links] = links end end.link(*args, **opts, &block) end |
#not_strict ⇒ Object
Allow for extra keys in the schema/collection even when passing strict: true to #validate!
242 243 244 |
# File 'lib/media_types/scheme.rb', line 242 def not_strict validations.default = NotStrict.new end |
#valid?(output, **opts) ⇒ TrueClass, FalseClass
Checks if the output is valid
78 79 80 81 82 83 84 |
# File 'lib/media_types/scheme.rb', line 78 def valid?(output, **opts) validate(output, **opts) rescue ExhaustedOutputError !opts.fetch(:exhaustive) { true } rescue ValidationError false end |
#validate(output, options = nil, **opts) ⇒ TrueClass
Validates the output and raises on certain validation errors
104 105 106 107 108 109 110 |
# File 'lib/media_types/scheme.rb', line 104 def validate(output, = nil, **opts) ||= ValidationOptions.new(**opts) catch(:end) do validate!(output, , context: nil) end end |
#validate!(output, call_options, **_opts) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/media_types/scheme.rb', line 115 def validate!(output, , **_opts) empty_guard!(output, ) exhaustive_guard!(validations.keys, ) do |mark| all?(output, ) do |key, value, options:, context:| mark.call(key) validations[key].validate!( value, .trace(key), context: context ) end end end |