Skip to content
This repository was archived by the owner on Dec 21, 2017. It is now read-only.

Multilanguage error messages

fogine edited this page Jul 18, 2016 · 2 revisions

You'll need idealy some sort of translation module, like i18n which will hold actual texts (messages) in various languages.

Then all what you need to do is overwrite default error message option (which defaults to Invalid data for %p, got: "%v") with a function which is expected to return actual error message:

var lang = require("i18n");
var inspector = require('json-inspector');

//validator definition
var validator = inspector.define('name-of-validator', {
    $is: String,
    $hasLengthOf: {min: 1, max: 255}
}, {
    message: function(assertionType, context) {// overwrite the default value with a function definition
        return lang.__(assertionType, context);
    },
});

If you're using Express, you can set the message option value when injecting json-inspector into the express app, like so:

app.use(validatorInjectorMiddleware({
    message: function(assertionType, context) {
        return lang.__(assertionType, context);
    }
}));

The function, you provide, is given an assertionType which is string path to the message and context object with assertion specific information about expected values.
The following is list of assertionType => message definitions you can use for the English:

{
    "$is"           : "The {{attr}} must be of type {{type}}.",
    "$isNull"       : "The {{attr}} may be {{type}}.",
    "$isInt"        : "The {{attr}} must be an integer.",
    "$isFloat"      : "The {{attr}} must be a number.",
    "$isAfter"      : "The {{attr}} must be a date after {{date}}.",
    "alpha"         : "The {{attr}} may only contain {{locale}} letters.",
    "alpha_num"     : "The {{attr}} may only contain {{locale}} letters and numbers.",
    "$isAscii"      : "The {{attr}} may only contain ASCII characters.",
    "$isBase64"     : "The {{attr}} must be base64 encoded string.",
    "$isBefore"     : "The {{attr}} must be a date before {{date}}.",
    "$isBoolean"    : "The {{attr}} may only contain Boolean values.",
    "$isCreditcard" : "The {{attr}} must be valid CreditCard number.",
    "$isCurrency"   : "The {{attr}} is not valid Currency format.",
    "$isDataURI"    : "The {{attr}} is not valid Data URI format.",
    "$isDate"       : "The {{attr}} is not a valid date.",
    "$isDecimal"    : "The {{attr}} must be decimal number.",
    "$isDivisibleBy": "The {{attr}} must be divisible by {{number}}.",
    "$isEmail"      : "The {{attr}} must be a valid email address.",
    "$isFQDN"       : "The {{attr}} is not valid FQDN format.",
    "$isFullWidth"  : "The {{attr}} may only contain Full-width chars.",
    "$isHalfWidth"  : "The {{attr}} may only contain Half-width chars.",
    "$isHexColor"   : "The {{attr}} must be valid Haxadecimal Color.",
    "$isHexadecimal": "The {{attr}} must be valid Haxadecimal Number.",
    "$isIP"         : "The {{attr}} must be valid IP address.",
    "$isISBN"       : "The {{attr}} must be valid ISBN, version {{version}}.",
    "$isJSON"       : "The {{attr}} must be valid JSON string.",
    "$isNumeric"    : "The {{attr}} may only contain digits.",
    "$isURL"        : "The {{attr}} format is invalid.",
    "$isUUID"       : "The {{attr}} must be valid UUID of version {{version}}",
    "$matches"      : "The {{attr}} format is invalid",
    "$isEmpty"      : "The {{attr}} may be empty string or null value",
    "$gt"           : "The {{attr}} must be greater than {{min}}",
    "$gte"          : "The {{attr}} must be greater than or equal {{min}}",
    "$lt"           : "The {{attr}} must be lower than {{max}}",
    "$lte"          : "The {{attr}} must be lower than or equal {{max}}",
    "$eq"           : "The {{attr}} must be equal to {{value}}",
    "$ne"           : "The {{attr}} must NOT be equal to {{value}}",
    "$like"         : "The {{attr}} format is invalid",
    "$like_any"     : "The {{attr}} format is invalid",
    "$iLike"        : "The {{attr}} format is invalid",
    "$iLike_any"    : "The {{attr}} format is invalid",
    "$in"           : "The {{attr}} value is invalid",
    "not": {
        "$in"        : "The {{attr}} value is invalid",
        "$like"      : "The {{attr}} format is invalid",
        "$like_any"  : "The {{attr}} format is invalid",
        "$iLike"     : "The {{attr}} format is invalid",
        "$iLike_any" : "The {{attr}} format is invalid",
        "between": {
            "number": "The {{attr}} must NOT be between {{min}} and {{max}}."
        }
    },
    "between"    :  {
        "byte"   : "The {{attr}} must be between {{min}} and {{max}} bytes in length.",
        "number" : "The {{attr}} must be between {{min}} and {{max}}.",
        "file"   : "The {{attr}} must be between {{min}} and {{max}} kilobytes.",
        "string" : "The {{attr}} must be between {{min}} and {{max}} characters.",
        "array"  : "The {{attr}} must have between {{min}} and {{max}} items."
    },
    "max": {
        "byte"   : "The {{attr}} may not be greater than {{max}} bytes in length.",
        "number" : "The {{attr}} may not be greater than {{max}}.",
        "file"   : "The {{attr}} may not be greater than {{max}} kilobytes.",
        "string" : "The {{attr}} may not be greater than {{max}} characters.",
        "array"  : "The {{attr}} may not have more than {{max}} items."
    },
    "min": {
        "byte"   : "The {{attr}} must be at least {{min}} bytes in length.",
        "number" : "The {{attr}} must be at least {{min}}.",
        "file"   : "The {{attr}} must be at least {{min}} kilobytes.",
        "string" : "The {{attr}} must be at least {{min}} characters.",
        "array"  : "The {{attr}} must have at least {{min}} items."
    }
}
Clone this wiki locally