Skip to content

Server errors

The submit flow of useValidation understands the error types of @basmilius/http-client. When the function passed to validated() throws, the error is routed as follows:

  • ValidationError: the nested error tree is flattened into dot-notation keys (profile.zip) and every backend constraint code is mapped to a validator.* message key. Each error lands on the ValidationField whose validation-key matches.
  • RequestError: the error description is stored under the exported GLOBAL_ERROR_KEY symbol and shown by ValidationNotice.
  • Anything else is re-thrown so your global error handling can pick it up.

Server errors live next to client-side errors instead of replacing them: re-validation on the client never wipes a server error. A field-level server error is cleared as soon as the value of that field changes; all server errors are cleared when a new submit starts or reset() is called.

Constraint mapping

Backend constraint codes are translated through a constraint map. The default map understands both short codes (missing, max_length) and their http_validation_constraint_* prefixed variants:

Constraint codeMessage key
address_not_validvalidator.addressNotValid
credentialsvalidator.credentials
emailvalidator.email
email_takenvalidator.emailTaken
is_phone_numbervalidator.phoneNumber
maxvalidator.maxValue
max_lengthvalidator.maxLength
minvalidator.minValue
min_lengthvalidator.minLength
missingvalidator.required
same_asvalidator.sameAs
validator_errorsvalidator.notice

Unknown codes are passed to the translate function as-is, so project-specific codes can be handled by extending the map through createValidation:

ts
app.use(createValidation({
    constraints: {
        http_validation_constraint_iban: 'validator.iban'
    },
    t: (key, params) => i18n.global.t(key, params ?? {})
}));