[go: up one dir, main page]

Skip to content

i18n support mixins for Joi javascript object validator

Notifications You must be signed in to change notification settings

sini-company/joi-i18n

Repository files navigation

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.

About

i18n support mixins for Joi javascript object validator

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published