Skip to content

Commit

Permalink
Move to address. Closes #2200
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Nov 24, 2019
1 parent d5de359 commit 5e0f43b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 302 deletions.
69 changes: 15 additions & 54 deletions lib/types/string/index.js → lib/types/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
const Assert = require('@hapi/hoek/lib/assert');
const Domain = require('@hapi/address/lib/domain');
const Email = require('@hapi/address/lib/email');
const Ip = require('@hapi/address/lib/ip');
const EscapeRegex = require('@hapi/hoek/lib/escapeRegex');
const Tlds = require('@hapi/address/lib/tlds');
const Uri = require('@hapi/address/lib/uri');

const Any = require('../any');
const Common = require('../../common');

const Ip = require('./ip');
const Uri = require('./uri');
const Any = require('./any');
const Common = require('../common');


const internals = {
Expand All @@ -30,7 +29,7 @@ const internals = {
dataUriRegex: /^data:[\w+.-]+\/[\w+.-]+;((charset=[\w-]+|base64),)?(.*)$/,
hexRegex: /^[a-f0-9]+$/i,
hostRegex: /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/,
ipRegex: Ip.createIpRegex(['ipv4', 'ipv6', 'ipvfuture'], 'optional'),
ipRegex: Ip.regex({ version: ['ipv4', 'ipv6', 'ipvfuture'], cidr: 'optional' }).regex,
isoDurationRegex: /^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/,

guidBrackets: {
Expand Down Expand Up @@ -407,56 +406,18 @@ module.exports = Any.extend({

Common.assertOptions(options, ['cidr', 'version']);

options = Object.assign({}, options); // Shallow cloned

let regex = internals.ipRegex;
if (options.cidr) {
Assert(typeof options.cidr === 'string', 'cidr must be a string');
options.cidr = options.cidr.toLowerCase();

Assert(internals.cidrPresences.includes(options.cidr), 'cidr must be one of ' + internals.cidrPresences.join(', '));

if (!options.version &&
options.cidr !== 'optional') {

regex = Ip.createIpRegex(['ipv4', 'ipv6', 'ipvfuture'], options.cidr);
}
}
else {
options.cidr = 'optional';
}

let versions;
if (options.version) {
if (!Array.isArray(options.version)) {
options.version = [options.version];
}

Assert(options.version.length >= 1, 'version must have at least 1 version specified');

versions = [];
for (let i = 0; i < options.version.length; ++i) {
let version = options.version[i];
Assert(typeof version === 'string', 'version at position ' + i + ' must be a string');
version = version.toLowerCase();
Assert(Ip.versions[version], 'version at position ' + i + ' must be one of ' + Object.keys(Ip.versions).join(', '));
versions.push(version);
}

versions = Array.from(new Set(versions));
regex = Ip.createIpRegex(versions, options.cidr);
}

return this.$_addRule({ name: 'ip', args: { options }, versions, regex });
const { cidr, versions, regex } = Ip.regex(options);
const version = options.version ? versions : undefined;
return this.$_addRule({ name: 'ip', args: { options: { cidr, version } }, regex });
},
validate(value, helpers, { options }, { versions, regex }) {
validate(value, helpers, { options }, { regex }) {

if (regex.test(value)) {
return value;
}

if (versions) {
return helpers.error('string.ipVersion', { value, cidr: options.cidr, version: versions });
if (options.version) {
return helpers.error('string.ipVersion', { value, cidr: options.cidr, version: options.version });
}

return helpers.error('string.ip', { value, cidr: options.cidr });
Expand Down Expand Up @@ -670,11 +631,11 @@ module.exports = Any.extend({
Common.assertOptions(options.domain, ['allowUnicode', 'minDomainSegments', 'tlds']);
}

const regex = Uri.createRegex(options);
const { regex, scheme } = Uri.regex(options);
const domain = options.domain ? internals.addressOptions(options.domain) : null;
return this.$_addRule({ name: 'uri', args: { options }, regex, domain });
return this.$_addRule({ name: 'uri', args: { options }, regex, domain, scheme });
},
validate(value, helpers, { options }, { regex, domain }) {
validate(value, helpers, { options }, { regex, domain, scheme }) {

if (['http:/', 'https:/'].includes(value)) { // scheme:/ is technically valid but makes no sense
return helpers.error('string.uri');
Expand All @@ -696,7 +657,7 @@ module.exports = Any.extend({
}

if (options.scheme) {
return helpers.error('string.uriCustomScheme', { scheme: regex.scheme, value });
return helpers.error('string.uriCustomScheme', { scheme, value });
}

return helpers.error('string.uri');
Expand Down
39 changes: 0 additions & 39 deletions lib/types/string/ip.js

This file was deleted.

186 changes: 0 additions & 186 deletions lib/types/string/uri.js

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
"validation"
],
"dependencies": {
"@hapi/address": "^2.1.2",
"@hapi/address": "^3.2.2",
"@hapi/formula": "^1.2.0",
"@hapi/hoek": "^8.2.4",
"@hapi/pinpoint": "^1.0.2",
"@hapi/topo": "^3.1.3"
},
"devDependencies": {
"@hapi/bourne": "1.x.x",
"@hapi/code": "6.x.x",
"@hapi/lab": "20.x.x",
"@hapi/code": "7.x.x",
"@hapi/lab": "21.x.x",
"@hapi/joi-legacy-test": "npm:@hapi/joi@15.x.x"
},
"scripts": {
Expand Down
Loading

0 comments on commit 5e0f43b

Please sign in to comment.