Sha256: ba848b1f1cda632b4cb2066b8ae6adfe40d9d341c44d4e570ac723955f2deed8

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

# Subchemas
> When your schema can change on-the-fly.

## Subschemas and mutations.
Sometimes depending on the data the structure may vary. Most frequently used option is to use `:declared` policy (a.k.a. conditional, but below is another option:

- Mutations are blocks that are assigned to a field and called during the validation (in #resolve), block receives all the related data and should return a subschema name.
- Subschemas are conditional schemas declared inside schemas. They doesn't exist until mutation block is called and decides to invoke a subschema.
```ruby
person_schema = Paradocs::Schema.new do
  field(:role).type(:string).options(["admin", "user"]).mutates_schema! do |value, key, payload, env|
    value == :admin ? :admin_schema : :user_schema
  end

  subschema(:admin_schema) do
    field(:permissions).present.type(:string).options(["superuser"])
    field(:admin_field)
  end
  subschema(:user_schema) do
    field(:permissions).present.type(:string).options(["readonly"])
    field(:user_field)
  end
end

results = person_schema.resolve(name: "John", age: 20, role: :admin, permissions: "superuser")
results.output # => {name: "John", age: 20, role: :admin, permissions: "superuser", admin_field: nil}
results = person_schema.resolve(name: "John", age: 20, role: :admin, permissions: "readonly")
results.errors => {"$.permissions"=>["must be one of superuser, but got readonly"]}
```

Version data entries

7 entries across 7 versions & 1 rubygems

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