sp_error (spectra v0.11.1)

View Source

Constructor functions for structured validation errors.

Each function builds an #sp_error{} record describing why a value failed to match an expected type. Errors carry a location path (built up by append_location/2 as the traversal unwinds) and a ctx map with the offending type and value.

Summary

Functions

Prepends FieldName to the error's location path.

Creates an error for a required field that was absent in the input.

Creates an error when no branch of a union type matched, bundling the per-branch errors.

Creates an error when an exact typed map field had no matching keys in the data.

Creates a type-mismatch error with no extra context.

Creates a type-mismatch error, merging Ctx into the context map.

Functions

append_location(Err, FieldName)

-spec append_location(#sp_error{location :: [string() | atom() | integer()],
                                type ::
                                    decode_error | type_mismatch | no_match | missing_data |
                                    not_matched_fields,
                                ctx ::
                                    #{type =>
                                          spectra:sp_type_or_ref() |
                                          spectra:map_field() |
                                          spectra:record_field(),
                                      value => dynamic(),
                                      errors => [{spectra:sp_type(), [#sp_error{}]}],
                                      message => string(),
                                      type_args => [{atom(), spectra:sp_type()}],
                                      err_type => atom(),
                                      err_reason => term()}},
                      string() | atom() | integer()) ->
                         #sp_error{location :: [string() | atom() | integer()],
                                   type ::
                                       decode_error | type_mismatch | no_match | missing_data |
                                       not_matched_fields,
                                   ctx ::
                                       #{type =>
                                             spectra:sp_type_or_ref() |
                                             spectra:map_field() |
                                             spectra:record_field(),
                                         value => dynamic(),
                                         errors => [{spectra:sp_type(), [#sp_error{}]}],
                                         message => string(),
                                         type_args => [{atom(), spectra:sp_type()}],
                                         err_type => atom(),
                                         err_reason => term()}}.

Prepends FieldName to the error's location path.

Called as the traversal unwinds so the final location reads outermost-to-innermost (e.g. [user, address, street]).

missing_data(Type, Value, Location)

-spec missing_data(spectra:sp_type_or_ref() | spectra:map_field() | spectra:record_field(),
                   dynamic(),
                   [string() | atom() | integer()]) ->
                      #sp_error{location :: [string() | atom() | integer()],
                                type ::
                                    decode_error | type_mismatch | no_match | missing_data |
                                    not_matched_fields,
                                ctx ::
                                    #{type =>
                                          spectra:sp_type_or_ref() |
                                          spectra:map_field() |
                                          spectra:record_field(),
                                      value => dynamic(),
                                      errors => [{spectra:sp_type(), [#sp_error{}]}],
                                      message => string(),
                                      type_args => [{atom(), spectra:sp_type()}],
                                      err_type => atom(),
                                      err_reason => term()}}.

Creates an error for a required field that was absent in the input.

no_match(Type, Value, Errors)

-spec no_match(spectra:sp_type_or_ref() | spectra:map_field() | spectra:record_field(),
               dynamic(),
               [{spectra:sp_type(),
                 [#sp_error{location :: [string() | atom() | integer()],
                            type ::
                                decode_error | type_mismatch | no_match | missing_data |
                                not_matched_fields,
                            ctx ::
                                #{type =>
                                      spectra:sp_type_or_ref() |
                                      spectra:map_field() |
                                      spectra:record_field(),
                                  value => dynamic(),
                                  errors => [{spectra:sp_type(), [#sp_error{}]}],
                                  message => string(),
                                  type_args => [{atom(), spectra:sp_type()}],
                                  err_type => atom(),
                                  err_reason => term()}}]}]) ->
                  #sp_error{location :: [string() | atom() | integer()],
                            type ::
                                decode_error | type_mismatch | no_match | missing_data |
                                not_matched_fields,
                            ctx ::
                                #{type =>
                                      spectra:sp_type_or_ref() |
                                      spectra:map_field() |
                                      spectra:record_field(),
                                  value => dynamic(),
                                  errors => [{spectra:sp_type(), [#sp_error{}]}],
                                  message => string(),
                                  type_args => [{atom(), spectra:sp_type()}],
                                  err_type => atom(),
                                  err_reason => term()}}.

Creates an error when no branch of a union type matched, bundling the per-branch errors.

not_matched_fields(Type, Value)

-spec not_matched_fields(spectra:sp_type_or_ref() | spectra:map_field() | spectra:record_field(),
                         dynamic()) ->
                            #sp_error{location :: [string() | atom() | integer()],
                                      type ::
                                          decode_error | type_mismatch | no_match | missing_data |
                                          not_matched_fields,
                                      ctx ::
                                          #{type =>
                                                spectra:sp_type_or_ref() |
                                                spectra:map_field() |
                                                spectra:record_field(),
                                            value => dynamic(),
                                            errors => [{spectra:sp_type(), [#sp_error{}]}],
                                            message => string(),
                                            type_args => [{atom(), spectra:sp_type()}],
                                            err_type => atom(),
                                            err_reason => term()}}.

Creates an error when an exact typed map field had no matching keys in the data.

type_mismatch(Type, Value)

-spec type_mismatch(spectra:sp_type_or_ref() | spectra:map_field() | spectra:record_field(), dynamic()) ->
                       #sp_error{location :: [string() | atom() | integer()],
                                 type ::
                                     decode_error | type_mismatch | no_match | missing_data |
                                     not_matched_fields,
                                 ctx ::
                                     #{type =>
                                           spectra:sp_type_or_ref() |
                                           spectra:map_field() |
                                           spectra:record_field(),
                                       value => dynamic(),
                                       errors => [{spectra:sp_type(), [#sp_error{}]}],
                                       message => string(),
                                       type_args => [{atom(), spectra:sp_type()}],
                                       err_type => atom(),
                                       err_reason => term()}}.

Creates a type-mismatch error with no extra context.

type_mismatch(Type, Value, Ctx)

-spec type_mismatch(spectra:sp_type_or_ref() | spectra:map_field() | spectra:record_field(),
                    dynamic(),
                    map()) ->
                       #sp_error{location :: [string() | atom() | integer()],
                                 type ::
                                     decode_error | type_mismatch | no_match | missing_data |
                                     not_matched_fields,
                                 ctx ::
                                     #{type =>
                                           spectra:sp_type_or_ref() |
                                           spectra:map_field() |
                                           spectra:record_field(),
                                       value => dynamic(),
                                       errors => [{spectra:sp_type(), [#sp_error{}]}],
                                       message => string(),
                                       type_args => [{atom(), spectra:sp_type()}],
                                       err_type => atom(),
                                       err_reason => term()}}.

Creates a type-mismatch error, merging Ctx into the context map.