diff --git a/example.js b/example.js index 77ff335..227e072 100644 --- a/example.js +++ b/example.js @@ -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 diff --git a/index.js b/index.js index 83b47de..12b19e2 100644 --- a/index.js +++ b/index.js @@ -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; + } } } ]; diff --git a/package.json b/package.json index 18eafbc..edbc81f 100644 --- a/package.json +++ b/package.json @@ -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"