Skip to content

Commit 8571aa3

Browse files
committed
feat(ulid): ULID validation
1 parent b958bd7 commit 8571aa3

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ Validator | Description
169169
**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].<br/><br/> `options` is an object which can contain the keys `hourFormat` or `mode`.<br/><br/>`hourFormat` is a key and defaults to `'hour24'`.<br/><br/>`mode` is a key and defaults to `'default'`. <br/><br/>`hourFomat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format. <br/><br/>`mode` can contain the values `'default'` or `'withSeconds'`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format.
170170
**isTaxID(str, locale)** | check if the string is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`.<br/><br/>Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-US', 'es-AR', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV', 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]`.
171171
**isURL(str [, options])** | check if the string is a URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.<br/><br/>`require_protocol` - if set to true isURL will return false if protocol is not present in the URL.<br/>`require_valid_protocol` - isURL will check if the URL's protocol is present in the protocols option.<br/>`protocols` - valid protocols can be modified with this option.<br/>`require_host` - if set to false isURL will not check if host is present in the URL.<br/>`require_port` - if set to true isURL will check if port is present in the URL.<br/>`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.<br/>`allow_fragments` - if set to false isURL will return false if fragments are present.<br/>`allow_query_components` - if set to false isURL will return false if query components are present.<br/>`validate_length` - if set to false isURL will skip string length validation (2083 characters is IE max URL length).
172+
**isULID(str)** | check if the string is a [ULID](https://github.com/ulid/spec).
172173
**isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4 or 5).
173174
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.
174175
**isVAT(str, countryCode)** | check if the string is a [valid VAT number][VAT Number] if validation is available for the given country code matching [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2]. <br/><br/>`countryCode` is one of `['AL', 'AR', 'AT', 'AU', 'BE', 'BG', 'BO', 'BR', 'BY', 'CA', 'CH', 'CL', 'CO', 'CR', 'CY', 'CZ', 'DE', 'DK', 'DO', 'EC', 'EE', 'EL', 'ES', 'FI', 'FR', 'GB', 'GT', 'HN', 'HR', 'HU', 'ID', 'IE', 'IL', 'IN', 'IS', 'IT', 'KZ', 'LT', 'LU', 'LV', 'MK', 'MT', 'MX', 'NG', 'NI', 'NL', 'NO', 'NZ', 'PA', 'PE', 'PH', 'PL', 'PT', 'PY', 'RO', 'RS', 'RU', 'SA', 'SE', 'SI', 'SK', 'SM', 'SV', 'TR', 'UA', 'UY', 'UZ', 'VE']`.

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import isEmpty from './lib/isEmpty';
6262
import isLength from './lib/isLength';
6363
import isByteLength from './lib/isByteLength';
6464

65+
import isULID from './lib/isULID';
6566
import isUUID from './lib/isUUID';
6667
import isMongoId from './lib/isMongoId';
6768

@@ -182,6 +183,7 @@ const validator = {
182183
isLength,
183184
isLocale,
184185
isByteLength,
186+
isULID,
185187
isUUID,
186188
isMongoId,
187189
isAfter,

src/lib/isULID.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import assertString from './util/assertString';
2+
3+
export default function isULID(str) {
4+
assertString(str);
5+
return /^[0-7][0-9A-HJKMNP-TV-Z]{25}}$/i.test(str);
6+
}

test/validators.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5014,6 +5014,29 @@ describe('Validators', () => {
50145014
});
50155015
});
50165016

5017+
it('should validate ULIDs', () => {
5018+
test({
5019+
validator: 'isULID',
5020+
valid: [
5021+
'01HBGW8CWQ5Q6DTT7XP89VV4KT',
5022+
'01HBGW8CWR8MZQMBG6FA2QHMDD',
5023+
'01HBGW8CWS3MEEK12Y9G7SVW4V',
5024+
'01hbgw8cws1tq2njavy9amb0wx',
5025+
'01HBGW8cwS43H4jkQ0A4ZRJ7QV',
5026+
],
5027+
invalid: [
5028+
'',
5029+
'01HBGW-CWS3MEEK1#Y9G7SVW4V',
5030+
'91HBGW8CWS3MEEK12Y9G7SVW4V',
5031+
'81HBGW8CWS3MEEK12Y9G7SVW4V',
5032+
'934859',
5033+
'01HBGW8CWS3MEEK12Y9G7SVW4VXXX',
5034+
'01UBGW8IWS3MOEK12Y9G7SVW4V',
5035+
'01HBGW8CuS43H4JKQ0A4ZRJ7QV',
5036+
],
5037+
});
5038+
});
5039+
50175040
it('should validate UUIDs', () => {
50185041
test({
50195042
validator: 'isUUID',

0 commit comments

Comments
 (0)