Class: Schemacop::Schema
- Inherits:
-
Object
- Object
- Schemacop::Schema
- Defined in:
- lib/schemacop/schema.rb
Instance Method Summary collapse
-
#initialize(*args, &block) ⇒ Schema
constructor
Create a new Schema.
-
#invalid?(data) ⇒ Boolean
Query data validity.
-
#valid?(data) ⇒ Boolean
Query data validity.
-
#validate(data) ⇒ Schemacop::Collector
Validate data for the defined Schema.
-
#validate!(data) ⇒ Object
Validate data for the defined Schema.
Constructor Details
#initialize(*args, &block) ⇒ Schema
Create a new Schema
For detailed usage, please refer to README.md in the root of this project.
12 13 14 15 16 |
# File 'lib/schemacop/schema.rb', line 12 def initialize(*args, &block) @root = HashValidator.new do req :root, *args, &block end end |
Instance Method Details
#invalid?(data) ⇒ Boolean
Query data validity
30 31 32 |
# File 'lib/schemacop/schema.rb', line 30 def invalid?(data) !valid?(data) end |
#valid?(data) ⇒ Boolean
Query data validity
22 23 24 |
# File 'lib/schemacop/schema.rb', line 22 def valid?(data) validate(data).valid? end |
#validate(data) ⇒ Schemacop::Collector
Validate data for the defined Schema
39 40 41 42 43 |
# File 'lib/schemacop/schema.rb', line 39 def validate(data) collector = Collector.new @root.fields[:root].validate({ root: data }, collector) return collector end |
#validate!(data) ⇒ Object
Validate data for the defined Schema
51 52 53 54 55 56 57 58 59 |
# File 'lib/schemacop/schema.rb', line 51 def validate!(data) collector = validate(data) unless collector.valid? fail Exceptions::ValidationError, collector. end return nil end |