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

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.

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:

# validate options

MyOptions.validate!(foo: "foo")
# %MyOptions{foo: "foo", bar: nil, baz: 10}

# retrieve original schema
MyOptions.schema()
# foo: [type: :string, required: true], bar: [type: :string], baz: [type: :integer, default: 10]