UNPKG

1.5 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.MaxLength = exports.maxLength = exports.MAX_LENGTH = void 0;
7const ValidateBy_1 = require("../common/ValidateBy");
8const isLength_1 = __importDefault(require("validator/lib/isLength"));
9exports.MAX_LENGTH = 'maxLength';
10/**
11 * Checks if the string's length is not more than given number. Note: this function takes into account surrogate pairs.
12 * If given value is not a string, then it returns false.
13 */
14function maxLength(value, max) {
15 return typeof value === 'string' && (0, isLength_1.default)(value, { min: 0, max });
16}
17exports.maxLength = maxLength;
18/**
19 * Checks if the string's length is not more than given number. Note: this function takes into account surrogate pairs.
20 * If given value is not a string, then it returns false.
21 */
22function MaxLength(max, validationOptions) {
23 return (0, ValidateBy_1.ValidateBy)({
24 name: exports.MAX_LENGTH,
25 constraints: [max],
26 validator: {
27 validate: (value, args) => maxLength(value, args.constraints[0]),
28 defaultMessage: (0, ValidateBy_1.buildMessage)(eachPrefix => eachPrefix + '$property must be shorter than or equal to $constraint1 characters', validationOptions),
29 },
30 }, validationOptions);
31}
32exports.MaxLength = MaxLength;
33//# sourceMappingURL=MaxLength.js.map
\No newline at end of file