Skip to content

Commit 9ade03b

Browse files
fasenderosandreafassina
authored andcommitted
feat(isFQDN): add allow_wildcard option (#1647)
Co-authored-by: Andrea Fassina <[email protected]>
1 parent 53ec2ad commit 9ade03b

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Validator | Description
107107
**isEmpty(str [, options])** | check if the string has a length of zero.<br/><br/>`options` is an object which defaults to `{ ignore_whitespace:false }`.
108108
**isEthereumAddress(str)** | check if the string is an [Ethereum](https://ethereum.org/) address using basic regex. Does not validate address checksums.
109109
**isFloat(str [, options])** | check if the string is a float.<br/><br/>`options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`) it also has `locale` as an option.<br/><br/>`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.<br/><br/>`locale` determine the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-CA', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. Locale list is `validator.isFloatLocales`.
110-
**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).<br/><br/>`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false , allow_numeric_tld: false }`.
110+
**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).<br/><br/>`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false }`. If `allow_wildcard` is set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`).
111111
**isFullWidth(str)** | check if the string contains any full-width chars.
112112
**isHalfWidth(str)** | check if the string contains any half-width chars.
113113
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']`

src/lib/isFQDN.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const default_fqdn_options = {
66
allow_underscores: false,
77
allow_trailing_dot: false,
88
allow_numeric_tld: false,
9+
allow_wildcard: false,
910
};
1011

1112
export default function isFQDN(str, options) {
@@ -16,6 +17,12 @@ export default function isFQDN(str, options) {
1617
if (options.allow_trailing_dot && str[str.length - 1] === '.') {
1718
str = str.substring(0, str.length - 1);
1819
}
20+
21+
/* Remove the optional wildcard before checking validity */
22+
if (options.allow_wildcard === true && str.indexOf('*.') === 0) {
23+
str = str.substring(2);
24+
}
25+
1926
const parts = str.split('.');
2027
const tld = parts[parts.length - 1];
2128

test/validators.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,18 @@ describe('Validators', () => {
11181118
],
11191119
});
11201120
});
1121+
it('should validate FQDN with wildcard option', () => {
1122+
test({
1123+
validator: 'isFQDN',
1124+
args: [
1125+
{ allow_wildcard: true },
1126+
],
1127+
valid: [
1128+
'*.example.com',
1129+
'*.shop.example.com',
1130+
],
1131+
});
1132+
});
11211133

11221134
it('should validate alpha strings', () => {
11231135
test({

0 commit comments

Comments
 (0)