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

ajv-ftl-i18n

v0.1.1

Published

Transpile Fluent (ftl) files into ajv error translations.

Downloads

167,571

Readme

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}"
    }
  }
}