View Source Spark.Options.Validator (spark v2.2.36)

Defines a validator module for an option schema.

Validators create structs with keys for each option in their schema, and an optimized validate, and validate! function on that struct.

Upgrading from options lists

You can pass the option define_deprecated_access?: true to use Spark.Options.Validator, which will make it such that options[:foo] will still work, but will emit a deprecation warning. This cane help with smoother upgrades.

Example

Given a module like the following:

defmodule MyOptions do
  use Spark.Options.Validator, schema: [
    foo: [
      type: :string,
      required: true
    ],
    bar: [
      type: :string
    ],
    baz: [
      type: :integer,
      default: 10
    ]
  ]
end

You can use it like so:

@doc """
Does a thing

## Options

#{MyOptions.docs()}
"""
@doc spark_opts: [{1, MyOptions.schema()}]
def your_function(arg, opts \ []) do
  options = Spark.Options.validate!(opts)

  options.foo
  options.bar
end