ExParamsSchema.Schema.Defaults (ex_params_schema v0.1.1)

Copy Markdown View Source

Casts field defaults and validates them against the generated JSON Schema.

This internal module is used while compiling schemas.

Summary

Functions

Casts a default value to the field type and validates it with the corresponding JSON Schema.

Functions

normalize!(fields, resolved)

Casts a default value to the field type and validates it with the corresponding JSON Schema.

iex> fields = ExParamsSchema.Definition.compile_fields!([
...>   {:count, :integer, [default: "2", minimum: 1]}
...> ])
iex> {json_schema, _errors} = ExParamsSchema.Schema.JsonSchema.build(fields, false)
iex> resolved = ExJsonSchema.Schema.resolve(json_schema)
iex> [field] = ExParamsSchema.Schema.Defaults.normalize!(fields, resolved)
iex> Keyword.fetch!(field.options, :default)
2

Values that cannot be cast or do not satisfy constraints raise ArgumentError.