UNPKG

flagpole

Version:

Simple and fast DOM integration, headless or headful browser, and REST API testing framework.

256 lines 9.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AssertionIs = void 0; const validator_1 = require("validator"); const constants_1 = require("./constants"); class AssertionIs { constructor(_assertion) { this._assertion = _assertion; } get not() { this._assertion.not; return this; } get optional() { this._assertion.optional; return this; } type(type) { return this._assertion.type.equals(type); } array() { return this._assertion.type.equals("array"); } object() { return this._assertion.type.equals("object"); } null() { return this._assertion.type.equals("null"); } undefined() { return this._assertion.type.equals("undefined"); } number() { return this._assertion.type.equals("number"); } string() { return this._assertion.type.equals("string"); } greaterThan(value) { return this._assertion.greaterThan(value); } lessThan(value) { return this._assertion.lessThan(value); } lessThanOrEquals(value) { return this._assertion.lessThanOrEquals(value); } greaterThanOrEquals(value) { return this._assertion.greaterThanOrEquals(value); } boolean() { return this._validate(validator_1.default.isBoolean, "a boolean"); } email() { return this._validate(validator_1.default.isEmail, "an email address"); } alpha() { return this._validate(validator_1.default.isAlpha, "an alpha string"); } alphaNumeric() { return this._validate(validator_1.default.isAlphanumeric, "alpha-numeric"); } ascii() { return this._validate(validator_1.default.isAscii, "an ASCII string"); } creditCard() { return this._validate(validator_1.default.isCreditCard, "a credit card number"); } currency() { return this._validate(validator_1.default.isCurrency, "currency"); } decimal() { return this._validate(validator_1.default.isDecimal, "a decimal"); } float() { return this._validate(validator_1.default.isFloat, "a float"); } ip() { return this._validate(validator_1.default.isIP, "an IP address"); } ipRange() { return this._validate(validator_1.default.isIPRange, "an IP range"); } isin() { return this._validate(validator_1.default.isISIN, "an ISIN (stock/security identifier)"); } isbn() { return this._validate(validator_1.default.isISBN, "an ISBN"); } isrc() { return this._validate(validator_1.default.isISRC, "an ISRC (international standard recording code)"); } issn() { return this._validate(validator_1.default.isISSN, "an ISSN (international standard serial number)"); } bic() { return this._validate(validator_1.default.isBIC, "an BIC (bank identification code) or SWIFT code"); } btcAddress() { return this._validate(validator_1.default.isBtcAddress, "a Bitcoin address"); } ean() { return this._validate(validator_1.default.isEAN, "a European Article Number"); } iban() { return this._validate(validator_1.default.isIBAN, "an International Bank Account Number"); } integer() { return this._validate(validator_1.default.isInt, "an integer"); } json() { return this._validate(validator_1.default.isJSON, "a valid JSON string"); } jwt() { return this._validate(validator_1.default.isJWT, "a JWT"); } numeric() { return this._validate(validator_1.default.isNumeric, "numeric"); } postalCode(locale) { return this._validateWithOpts(validator_1.default.isPostalCode, locale, locale ? `a ${locale} postal code` : "a postal code"); } url() { return this._validate(validator_1.default.isURL, "a URL"); } mobilePhone(locale) { return this._validateWithOpts(validator_1.default.isMobilePhone, locale, locale ? `a ${locale} mobile phone number` : "mobile phone number"); } base32() { return this._validate(validator_1.default.isBase32, "base32 encoded"); } base64() { return this._validate(validator_1.default.isBase64, "base32 encoded"); } beforeDate(date) { return this._validateWithOpts(validator_1.default.isBefore, date, date ? `before ${date}` : `before ${Date.toString}`); } sameOrAfterDate(date) { return this._validateWithOpts((str) => str == date || validator_1.default.isAfter(str, date), date, date ? `same or after ${date}` : `same or after ${Date.toString}`); } sameOrBeforeDate(date) { return this._validateWithOpts((str) => str == date || validator_1.default.isBefore(str, date), date, date ? `same or before ${date}` : `same or before ${Date.toString}`); } afterDate(date) { return this._validateWithOpts(validator_1.default.isAfter, date, date ? `after ${date}` : `after ${Date.toString}`); } dataUri() { return this._validate(validator_1.default.isDataURI, "data URI"); } empty() { return this._validate(validator_1.default.isEmpty, "empty"); } fqdn() { return this._validate(validator_1.default.isFQDN, "fully-qualified domain name"); } hash() { return this._validate(validator_1.default.isHash, "a hash"); } hexColor() { return this._validate(validator_1.default.isHexColor, "a hexadecimal color"); } hexadecimal() { return this._validate(validator_1.default.isHexadecimal, "a hexadecimal number"); } hsl() { return this._validate(validator_1.default.isHSL, "an HSL(A) (hue, saturation, lightness, optional alpha)"); } in(values) { return this._validateWithOpts(validator_1.default.isIn, values, `included in: ${values.join(", ")}`); } latLong() { return this._validate(validator_1.default.isLatLong, "latitude and longitude"); } lowercase() { return this._validate(validator_1.default.isLowercase, "lowercase"); } md5() { return this._validate(validator_1.default.isMD5, "MD5"); } mimeType() { return this._validate(validator_1.default.isMimeType, "mime-type"); } octal() { return this._validate(validator_1.default.isOctal, "octal"); } port() { return this._validate(validator_1.default.isPort, "a valid port"); } rgbColor() { return this._validate(validator_1.default.isRgbColor, "an RGB color"); } slug() { return this._validate(validator_1.default.isSlug, "a slug string"); } strongPassword() { return this._validate(validator_1.default.isStrongPassword, "a strong password"); } uuid() { return this._validate(validator_1.default.isUUID, "a UUID"); } uppercase() { return this._validate(validator_1.default.isUppercase, "uppercase"); } date() { return this._validate(validator_1.default.isDate, "a date"); } timezone() { return this._validate((text) => { if (!Intl || !Intl.DateTimeFormat().resolvedOptions().timeZone) { throw "Time zones are not available in this environment"; } try { Intl.DateTimeFormat(undefined, { timeZone: text }); return true; } catch (ex) { return false; } }, "a timezone"); } regionCode(countries) { let codes = []; if (countries === null || countries === void 0 ? void 0 : countries.includes("US")) { codes = [...codes, ...constants_1.usStateCodes]; } if (countries === null || countries === void 0 ? void 0 : countries.includes("CA")) { codes = [...codes, ...constants_1.canadaProvinceCodes]; } return this._validate((state) => codes.length > 0 ? codes.includes(state) : state.length == 2, "a region code"); } countryCode(format) { const codes = format == "iso-alpha-2" ? constants_1.countryCodes2 : format == "iso-alpha-3" ? constants_1.countryCodes3 : []; return this._validate((code) => codes.length > 0 ? codes.includes(code) : code.length == 3 || code.length == 2, "a country code"); } _setValidationMessage(thisThing) { this._assertion.setDefaultMessages(`${this._assertion.subject} is not ${thisThing}.`, `${this._assertion.subject} is ${thisThing}.`); } _validate(func, thisThing) { const text = this._assertion.text; this._setValidationMessage(thisThing); return this._assertion.execute(func(text), text); } _validateWithOpts(func, opts, thisThing) { const text = this._assertion.text; this._setValidationMessage(thisThing); return this._assertion.execute(func(text, opts), text); } } exports.AssertionIs = AssertionIs; //# sourceMappingURL=assertion-is.js.map