Class: AnyVali::OptionalSchema
- Defined in:
- lib/anyvali/schemas/optional_schema.rb
Constant Summary
Constants inherited from Schema
Schema::RESERVED_METADATA_KEYS
Instance Attribute Summary collapse
-
#inner_schema ⇒ Object
readonly
Returns the value of attribute inner_schema.
Attributes inherited from Schema
#coerce_config, #constraints, #custom_validators, #default_value, #has_default, #kind, #metadata
Instance Method Summary collapse
-
#initialize(schema:, **kwargs) ⇒ OptionalSchema
constructor
A new instance of OptionalSchema.
- #safe_parse(input, path: [], context: nil) ⇒ Object
- #to_node ⇒ Object
Methods inherited from Schema
#coerce, #default, #describe, #export, #parse, #portable?, #refine, type_name, #with_metadata
Constructor Details
#initialize(schema:, **kwargs) ⇒ OptionalSchema
Returns a new instance of OptionalSchema.
7 8 9 10 |
# File 'lib/anyvali/schemas/optional_schema.rb', line 7 def initialize(schema:, **kwargs) @inner_schema = schema super(kind: "optional", **kwargs) end |
Instance Attribute Details
#inner_schema ⇒ Object (readonly)
Returns the value of attribute inner_schema.
5 6 7 |
# File 'lib/anyvali/schemas/optional_schema.rb', line 5 def inner_schema @inner_schema end |
Instance Method Details
#safe_parse(input, path: [], context: nil) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/anyvali/schemas/optional_schema.rb', line 18 def safe_parse(input, path: [], context: nil) # When used in objects, absence is handled by ObjectSchema # If called directly with a value, delegate to inner schema context ||= ValidationContext.new @inner_schema.safe_parse(input, path: path, context: context) end |
#to_node ⇒ Object
12 13 14 15 16 |
# File 'lib/anyvali/schemas/optional_schema.rb', line 12 def to_node node = super node["schema"] = @inner_schema.to_node node end |