13.1.4 • Published 4 years ago

joi-i18n v13.1.4

Weekly downloads
418
License
ISC
Repository
-
Last release
4 years ago

joi-i18n

An i18n support mixins for Joi object validator.

Build Status Current Version

Usage

import 'joi-i18n';
import * as Joi from 'joi';

// or just use this module as Joi
// import * as Joi from 'joi-i18n';

// or use require()
// const Joi = require('joi-i18n');

// add locale data
Joi.addLocaleData('en_US', {
  any: {
    // using joi's template syntax
    required: `!!oh no, "{{key}}" is required!!!`
  },
  object: {
    // using it's own joi error item formatter
    allowUnknown: (error) => `"${error.context.key}" is not allowed here!!`
  }
})

// prepare schema
const schema = Joi
  .object({ required: Joi.any().required() })
  .options({ abortEarly: false });

// validate object
const value = { unknown: 'unknown', required: undefined };
const { error } = schema.validate(value, { locale: 'en_US' });

// error.details:
// [
//   {
//     message: 'oh no, "required" is required!!!',
//     path: ['required'],
//     type: 'any.required',
//     context: { key: 'required' }
//   },
//   {
//     message: '"unknown" is not allowed here!!',
//     path: ['value'],
//     type: 'object.allowUnknown',
//     context: { child: 'unknown', key: 'value' }
//   }
// ]

Methods

Joi.validate(value, schema, [options, [callback])

Same with original Joi.validate except:

  • options an optional object with same signature of original with an additional key:

Joi.addLocaleData(locale, language)

Registers a new locale data where:

  • locale a string represents a locale for given data
  • language language configuration object that gets passed to the Joi's validate options. (See Joi#validate or joi/lib/language.js for more information.
    • it supports two type for descriptor value:
      • string that uses Joi's template syntax
      • formatter function that receives Joi's ValidationError item

Joi.getLocaleData([locale])

Returns a registered locale data where:

  • [locale] an optional string represents a locale to retrieve

Joi.setDefaultLocale(locale)

A static method that will set default locale for every validate options where:

Joi.getDefaultLocale()

Returns a string represents registered default locale

Joi.formatErrorDetails(error, [locale])

Returns a joi validation error item with locale formatted details where:

  • error a Joi validation error object
  • [locale] an optional string represents a locale to format

Description

It overrides two internal methods of Joi's Any class prototype which are:

_valiateWithOptions(value, options, callback): the original validate() implementation with followings modifications:

  • set options.language property with provided locale via Joi.addLocale(locale, language)
  • set schema.error(err) handler to format each single error items before return result.

checkOptions(options): the original options argument validator

  • Overrides internal validator to allow additional { locale: string } property

NOTE Since above two functions are designed for internal use, they might be changed. So be careful to match joi's version with this module.

13.1.4

4 years ago

13.1.3

6 years ago

13.1.2

6 years ago

13.1.1

6 years ago

13.1.0

6 years ago

13.0.0

6 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago