Sha256: 959a8caaf666077339c7775d7787d3704f0af845cf02678a3ee0a300e635fd1e

Contents?: true

Size: 927 Bytes

Versions: 7

Compression:

Stored size: 927 Bytes

Contents

# FAQ
## Defaults
### Q: I need the child schema to be enriched with the specified defaults is parent key is absent.
```rb
schema do
  field(:top_level).type(:string).required.default('top_level')
  field(:nested).type(:object).required.schema do
    field(:start_date).type(:datetime).required.default(->(a,b,c) { Time.now })
    field(:ends_after).type(:integer).required.default(5)
  end
end
# usage
TestSchema.schema.resolve({}).output # => {:top_level=>"top_level", :nested=>nil, :configurations=>nil}
TestSchema.schema.resolve({nested: {}}).output # => {:top_level=>"top_level", :nested=>{:start_date=>#<DateTime: 2020-08-31T15:36:43+02:00 ((2459093j,49003s,0n),+7200s,2299161j)>, :ends_after=>5}, :configurations=>nil}
# I want resolving on {} to include the :nested structure.
```

### A: Set `.default({})` to your `:nested` field.
> Fields from nested schema are invoked only when the object for the schema exists.


Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
paradocs-1.1.6 docs/faq.md
paradocs-1.1.5 docs/faq.md
paradocs-1.1.4 docs/faq.md
paradocs-1.1.3 docs/faq.md
paradocs-1.1.2 docs/faq.md
paradocs-1.1.1 docs/faq.md
paradocs-1.1.0 docs/faq.md