joi
semver
>=17.0.0 <19.0.0postconditions6functions5last verified2026-04-17coverage score83%Postconditions — what we check
- validate · validate-returns-errorerrorWhendata fails validation against the schemaReturns{error: ValidationError, value: any} where error contains validation failure detailsRequired handlingCaller MUST check result.error property before using result.value. Without checking error, invalid data will pass through silently, leading to data corruption, business logic errors, or security vulnerabilities. Use pattern: const { error, value } = schema.validate(data); if (error) { /* handle */ }costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1]
- validateAsync · validateasync-rejectserrorWhendata fails validation against the schemaThrows
Promise rejection with ValidationErrorRequired handlingCaller MUST wrap validateAsync() in try-catch or use .catch() handler. Without error handling, validation failures cause unhandled promise rejections that crash the application. Use pattern: try { const value = await schema.validateAsync(data); } catch (error) { /* handle */ }costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[2] - assert · assert-throwserrorWhendata fails validation against the schemaThrows
ValidationErrorRequired handlingCaller MUST wrap Joi.assert() in try-catch block. Without error handling, validation failures throw uncaught exceptions that crash the application. Use pattern: try { Joi.assert(value, schema); } catch (error) { /* handle */ }costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[3] - attempt · attempt-throwserrorWhendata fails validation against the schemaThrows
ValidationErrorRequired handlingCaller MUST wrap Joi.attempt() in try-catch block. Without error handling, validation failures throw uncaught exceptions that crash the application. Use pattern: try { const validated = Joi.attempt(value, schema); } catch (error) { /* handle */ }costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[4] - compile · compile-invalid-schema-throwswarningWhenschema argument is undefined, an empty array, or contains non-plain objectsThrows
AssertError (extends Error) from @hapi/hoek — message: 'Invalid undefined schema' or 'Invalid empty array schema' or 'Schema can only contain plain objects'Required handlingCaller MUST wrap Joi.compile() in try-catch when schema is built dynamically (e.g. from user input, config files, or database). Static schema definitions compiled at module load are safe if the schema literal is correct. Without error handling, invalid schema definitions throw at runtime and crash the request handler or service startup. Use pattern: try { const schema = Joi.compile(rawSchema); } catch (err) { /* handle */ }costlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - compile · compile-version-mismatch-throwswarningWhenschema was compiled with a different version of joi (legacy: false, default)Throws
AssertError — message: 'Cannot mix different versions of joi schemas: <version> <version>'Required handlingWhen using Joi.compile() with dynamically loaded schemas from external sources (plugins, serialized schemas), version mismatches throw AssertError. Use Joi.compile(schema, { legacy: true }) to allow older schema versions, or ensure all schemas are compiled with the same joi version.costlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
Sources
Every postcondition cites at least one of these. Numbered to match the footnotes above.
- [1]joi.dev/apihttps://joi.dev/api/#anyvalidatevalue-options
- [2]joi.dev/apihttps://joi.dev/api/#anyvalidateasyncvalue-options
- [3]joi.dev/apihttps://joi.dev/api/#assertvalue-schema-message-options
- [4]joi.dev/apihttps://joi.dev/api/#attemptvalue-schema-message-options
- [5]raw.githubusercontent.com/hapijs/joihttps://raw.githubusercontent.com/hapijs/joi/master/API.md
- [6]github.com/hapijs/joihttps://github.com/hapijs/joi/blob/master/lib/compile.js
Need a different package?
Request a profile