Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
icu-validator
Advanced tools
Check if a text conforms to ICU format - supports individual string, JSON files or folders contaning JSON files
Validate a data source if all strings in the data source conforms to ICU standard. Supports -
Install using yarn -
yarn add icu-validator -D
or using npm
npm install icu-validator --save-dev
// Using ESM
import { validate, validateFile, validateDirectory } from 'icu-validator';
// Using CJS
const { validate, validateFile, validateDirectory } = require('icu-validator');
const { validate } = require('icu-validator');
const validationResult = validate('Hello {{name}}, how are you?');
/**
* {
* "__icu_validator_error":true,
* "result": {
* "errorMessage":"MALFORMED_ARGUMENT",
* "originalMessage":"Hello {{name}}, how are you?",
* "location": {
* "start": { "offset":6,"line":1,"column":7 },
* "end": { "offset":7,"line":1,"column":8 }
* }
* }
* }
**/
const validationResult = validate('Hello {name}, how are you?');
/**
* { "__icu_validator_error": false }
**/
You can pretty print the errors by passing a flag prettyPrint: true
const validationResult = validate('Hello {{name}}, how are you?', { prettyPrint: true });
const obj = {
"welcome": "Hello {name}, how are you?",
"examples": {
"pluralization": "Hi {name}, you have {{numProducts, plural, =0 {no items} =1 {one item} other {# items}} in cart"
}
};
validate(obj, { prettyPrint: true });
/**
Invalid ICU string :- Hi {name}, you have {{numProducts, plural, =0 {no items} =1 {one item} other {# items}} in cart
Object path :- examples.pluralization
Error :- MALFORMED_ARGUMENT
Location :- {"start":{"offset":20,"line":1,"column":21},"end":{"offset":21,"line":1,"column":22}}
**/
Validate method does accept a file or directory path as first argument. But one very important thing to keep in mind - the validate method is completely sync, so all file & directory read operations will be sync.
It is recommended to use dedicated validateFile
and validateDirectory
async methods.
validateFile(filePath).then((result) => {
console.log(JSON.stringify(result));
});
validateDirectory(directoryPath).then((result) => {
console.log(JSON.stringify(result));
});
Try prettyPrint: true
if you want to show the errors on screen
validate
, validateFile
and validateDirectory
take a 2nd argument - options
to customize the output format and validation rules/
validate(source: string | object, options: object)
validateFile(filePath: string, options: object)
validateDirectory(directoryPath: string, options: object)
Options include -
prettyPrint
- default (false
)Print the validation result and errors in console.
verbose
- default (false
)Provide complete execution logs when printing the results.
ignoreTransTag
- default (false
)If you are using react-i18next
with ICU, you will encounter situations where you need to externalize HTML elements. For react-i18next, the recommended method is Trans component. The externalized string resembles a format - Please click <0>here</0>
. These are usually termed as invalid strings. Use this option if you want to ignore such tags from validation rule.
parseOptions
Under the hood, icu-validator
uses formatjs
parser. You can customize the validation rules by customizing the parser -
ignoreTag
Whether to treat HTML/XML tags as string literal
requiresOtherClause
Should select
, selectordinal
, and plural
arguments always include the other
case clause
shouldParseSkeletons
Whether to parse number/datetime skeleton into Intl.NumberFormatOptions and Intl.DateTimeFormatOptions, respectively.
captureLocation
Capture location info in AST
locale
Instance of Intl.Locale to resolve locale-dependent skeleton
FAQs
Check if a text conforms to ICU format - supports individual string, JSON files or folders contaning JSON files
The npm package icu-validator receives a total of 33,353 weekly downloads. As such, icu-validator popularity was classified as popular.
We found that icu-validator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.