npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

joi-i18n

v13.1.4

Published

An i18n support mixins for [Joi](https://github.com/hapijs/joi) object validator.

Downloads

1,009

Readme

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.