add option to return original value instead of Url {}

master
f0x 2 years ago
parent 93d2f29e52
commit 6cebd97a92

@ -5,6 +5,9 @@ const isURL = require("./");
console.log(validateValue("https://example.com/", [ isURL() ])); // Url { ... }
// validate without returning parsed Url {}
console.log(validateValue("https://example.com/", [ isURL({parse: false}) ]))
console.log(validateValue("bar", [ isURL() ])); /*
AggregrateValidationError: One or more validation errors occurred:
- At (root): Must be a valid URL

@ -1,10 +1,21 @@
"use strict";
const defaultValue = require("default-value");
const url = require("url");
const ValidationError = require("@validatem/error");
const isString = require("@validatem/is-string");
module.exports = function (protocols) {
module.exports = function (options = {}) {
let parse = defaultValue(options.parse, true);
let protocols = options.protocols;
if (Array.isArray(options)) { // keep compatibility with <=0.2.0
protocols = options;
options = {};
}
if (protocols != null && !Array.isArray(protocols)) {
throw new Error(`Permitted protocol list must be an array`);
}
@ -35,7 +46,11 @@ module.exports = function (protocols) {
return new ValidationError(`Must be a URL with one of the following protocols: ${validProtocolList} - but got ${parsedProtocol.toUpperCase()} instead`, { code: "validatem.is-url.wrong-protocol" });
} else {
return parsed;
if (parse) {
return parsed;
} else {
return value;
}
}
}
];

@ -12,7 +12,8 @@
"license": "WTFPL OR CC0-1.0",
"dependencies": {
"@validatem/error": "^1.0.0",
"@validatem/is-string": "^0.1.1"
"@validatem/is-string": "^0.1.1",
"default-value": "^1.0.0"
},
"devDependencies": {
"@validatem/core": "^0.3.1"

Loading…
Cancel
Save