joi-bcp47

BCP47 language tag validation for Joi.

Usage no npm install needed!

<script type="module">
  import joiBcp47 from 'https://cdn.skypack.dev/joi-bcp47';
</script>

README

joi-bcp47

Build Status Coverage Status Known Vulnerabilities npm version

Provides a Joi rule to validate and parse BCP47 language tags (eg. en-US, ur-PK, zh-Hant-TW).

Note: Only supports Joi 16.x or higher. Does not validate the components of the BCP47 tag (eg. language or region codes)

Installation:

npm: npm install joi-bcp47

yarn: yarn add joi-bcp47

Usage

import BaseJoi from 'joi';
import JoiBcp47 from 'joi-bcp47';

const Joi = BaseJoi.extend(JoiBcp47);

Joi.bcp47().validate('en-US');
// returns {error: null, value: 'en-US'}

Rules

parse - Parse the BCP47 tag into an object

When added to the validation chain returns a bcp47 parse object which parses the BCP47 tag out to its individual components.

Joi.bcp47().parse().validate('hy-Latn-IT-arevela');
/*
    returns {
        error: null,
        value: {
            langtag: {
                language: {
                    language: "hy",
                    extlang: []
                },
                script: "Latn",
                region: "IT",
                variant: ["arevela"],
                extension: [],
                privateuse: []
            },
            privateuse: [],
            grandfathered: {
                irregular: null,
                regular: null
            }
        }
    }
*/