Class: AnyVali::OptionalSchema

Inherits:
Schema
  • Object
show all
Defined in:
lib/anyvali/schemas/optional_schema.rb

Constant Summary

Constants inherited from Schema

Schema::RESERVED_METADATA_KEYS

Instance Attribute Summary collapse

Attributes inherited from Schema

#coerce_config, #constraints, #custom_validators, #default_value, #has_default, #kind, #metadata

Instance Method Summary collapse

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_schemaObject (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_nodeObject



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