Module: RSchema
- Defined in:
- lib/rschema.rb,
lib/rschema/version.rb,
lib/rschema/rails_interop.rb
Defined Under Namespace
Modules: BooleanSchema, CoercionMapper, DSL, RailsInterop
Classes: EnumSchema, ErrorDetails, GenericHashSchema, GenericSetSchema, MaybeSchema, OptionalHashKey, PredicateSchema
Constant Summary
collapse
- InvalidSchemaError =
Class.new(StandardError)
- ValidationError =
Class.new(StandardError)
- VERSION =
'1.0.1'
Class Method Summary
collapse
Class Method Details
.coerce(schema, value) ⇒ Object
45
46
47
|
# File 'lib/rschema.rb', line 45
def self.coerce(schema, value)
walk(schema, value, CoercionMapper)
end
|
.coerce!(schema, value) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/rschema.rb', line 49
def self.coerce!(schema, value)
result, error = walk(schema, value, CoercionMapper)
if error.nil?
result
else
raise(ValidationError, error)
end
end
|
.schema(&block) ⇒ Object
23
24
25
|
# File 'lib/rschema.rb', line 23
def self.schema(&block)
DSL.instance_exec(&block)
end
|
.validate(schema, value) ⇒ Object
41
42
43
|
# File 'lib/rschema.rb', line 41
def self.validate(schema, value)
validation_error(schema, value).nil?
end
|
.validate!(schema, value) ⇒ Object
32
33
34
35
36
37
38
39
|
# File 'lib/rschema.rb', line 32
def self.validate!(schema, value)
result, error = walk(schema, value)
if error.nil?
result
else
raise(ValidationError, error)
end
end
|
.validation_error(schema, value) ⇒ Object
27
28
29
30
|
# File 'lib/rschema.rb', line 27
def self.validation_error(schema, value)
_, error = walk(schema, value)
error
end
|
.walk(schema, value, mapper = nil) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/rschema.rb', line 58
def self.walk(schema, value, mapper = nil)
raise(InvalidSchemaError, schema) unless schema.respond_to?(:schema_walk)
value = mapper.prewalk(schema, value) if mapper
value = schema.schema_walk(value, mapper)
value = mapper.postwalk(schema, value) if mapper
if value.is_a?(RSchema::ErrorDetails)
[nil, value]
else
[value, nil]
end
end
|