Class: Senko::Schema
- Inherits:
-
Object
- Object
- Senko::Schema
- Defined in:
- lib/senko/schema.rb
Instance Attribute Summary collapse
-
#instructions ⇒ Object
readonly
Returns the value of attribute instructions.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#initialize(source:, instructions:, options:, generated_validator: nil) ⇒ Schema
constructor
A new instance of Schema.
- #valid?(data) ⇒ Boolean
- #valid_json?(json) ⇒ Boolean
- #validate(data, fail_fast: @options[:fail_fast]) ⇒ Object
- #validate!(data) ⇒ Object
- #validate_json(json, fail_fast: @options[:fail_fast]) ⇒ Object
Constructor Details
#initialize(source:, instructions:, options:, generated_validator: nil) ⇒ Schema
Returns a new instance of Schema.
12 13 14 15 16 17 18 19 |
# File 'lib/senko/schema.rb', line 12 def initialize(source:, instructions:, options:, generated_validator: nil) @source = source @instructions = instructions @options = @generated_validator = generated_validator @validator = Validator.new() @track_evaluation = Validator.requires_evaluation_tracking?(instructions) end |
Instance Attribute Details
#instructions ⇒ Object (readonly)
Returns the value of attribute instructions.
10 11 12 |
# File 'lib/senko/schema.rb', line 10 def instructions @instructions end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/senko/schema.rb', line 10 def @options end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
10 11 12 |
# File 'lib/senko/schema.rb', line 10 def source @source end |
Instance Method Details
#valid?(data) ⇒ Boolean
21 22 23 24 25 |
# File 'lib/senko/schema.rb', line 21 def valid?(data) return @generated_validator.call(data) if @generated_validator @validator.valid?(@instructions, data, track_evaluation: @track_evaluation) end |
#valid_json?(json) ⇒ Boolean
31 32 33 |
# File 'lib/senko/schema.rb', line 31 def valid_json?(json) validate_json(json, fail_fast: true).valid? end |
#validate(data, fail_fast: @options[:fail_fast]) ⇒ Object
27 28 29 |
# File 'lib/senko/schema.rb', line 27 def validate(data, fail_fast: @options[:fail_fast]) @validator.validate(@instructions, data, fail_fast: fail_fast) end |
#validate!(data) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/senko/schema.rb', line 51 def validate!(data) result = validate(data, fail_fast: true) raise ValidationError, result unless result.valid? data end |
#validate_json(json, fail_fast: @options[:fail_fast]) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/senko/schema.rb', line 35 def validate_json(json, fail_fast: @options[:fail_fast]) validate(JSON.parse(json), fail_fast: fail_fast) rescue JSON::ParserError => e result = Result.new(fail_fast: fail_fast) result.add_error( Error.new( message: "invalid JSON: #{e.}", instance_location: '', keyword_location: '', keyword: 'parse', data: json ) ) result end |