Socket
Socket
Sign inDemoInstall

ajv-ftl-i18n

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ajv-ftl-i18n

Transpile Fluent (ftl) files into ajv error translations.


Version published
Weekly downloads
37K
increased by6.88%
Maintainers
1
Weekly downloads
 
Created
Source

ajv-ftl-i18n

Internationalized error messages for Ajv - superfast JSON validator for JSON Schema and JSON Type Definition. A drop in replacement for ajv-i18n with some extra features.

Features

  • Based off of Fluent (.ftl)
  • Allows overriding messages
  • Supports multi-lingual errorMessages
  • Supports English (en), Arabic (ar), Catalan (ca), Czech (cs), German (de), Spanish (es), Finnish (fi), French (fr), Hungarian (hu), Indonesian (id), Italian (it), Japanese (ja), Korean (ko), Norwegian bokmål (nb), Dutch (nl), Polish (pl), Português - Brasil (pt-BR), Russian (ru), Slovak (sk), Swedish (sv), Thai (th), Chinese (zh), Chinese - Taiwan (zh-TW). PR to correct or add new locales welcome.

AJV Differences

  • ar will return Arabic numerals, based on locale
  • en uses lower case NOT
  • fr uses «,» instead of "
  • Fixed quote/space inconsistencies between locales
  • Added in computer translation when missing
  • Merged schema and jtd together

Install

npm i -D ajv-ftl-i18n

Usage

import _ajv from 'ajv/dist/2020.js'
import localize from 'ajv-ftl-i18n'
import errorMessage from 'ajv-errors'

const Ajv = _ajv.default // esm workaround

const ajv = Ajv({
  allErrors: true,
  messages: true // must be `true` for `errorMessages` support
})
errorMessage(ajv)
const validate = ajv.compile(schema)
const valid = validate(data)

if (!valid) {
  localize.fr(validate.errors)
  // string with all errors and data paths
  console.log(ajv.errorsText(validate.errors, { separator: '\n' }))
}

Transpile

Used to transpile custom ftl files into ajv localizations.

CLI

Usage: ajv-ftl [options] <input>

Transpile Fluent (.ftl) files to JavaScript (.js or .mjs) for ajv

Arguments:
  input                  Path to the Fluent file to compile

Options:
  --locale <locale...>   What locale(s) to be used. Multiple can be set to allow for fallback. i.e. en-CA
  -o, --output <output>  Path to store the resulting JavaScript file. Will be in ESM.
  -h, --help             display help for command

NodeJS

import { readFile, writeFile } from 'node:fs/promises'
import { transpile } from 'ajv-ftl-i18n'

const ftl = await readFile('./path/to/en.ftl', { encoding: 'utf8' })
const js = transpile(ftl, { locale: 'en-CA' })
await writeFile('./path/to/en.mjs', js, 'utf8')

errorMessage

All functionality of ajv-errors are supported. To not introduce new complexity to the errorMessage structure, you'll need to pull your errorMessages into a centralized ftl file for the schema.

${messageId}[, ${key}:${jsonPointer}][...]

Replace with message identifier in Fluent file

schema.ftl

...
dimensions = The dimensions are invalid

schema.json

{
  "type": "object",
  "properties": {
    "height": {
      "type": "number",
      "minimum": 4
    },
    "width": {
      "type": "number",
      "minimum": 4
    }
  },
  "errorMessage": "dimensions"
}

Use JSON-pointers

schema.ftl

...
dimensions = The dimensions ({$h}x{$w}) are invalid

schema.json

{
  "type": "object",
  "properties": {
    "height": {
      "type": "number",
      "minimum": 4
    },
    "width": {
      "type": "number",
      "minimum": 4
    }
  },
  "errorMessage": {
    "properties": {
      "height": "dimensions, h:${/height}, w:${/width}"
    }
  }
}

Keywords

FAQs

Package last updated on 04 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc