@vuelidate/validators
Advanced tools
Comparing version 2.0.0-alpha.0 to 2.0.0-alpha.1
@@ -6,2 +6,21 @@ # Change Log | ||
# [2.0.0-alpha.1](https://github.com/vuelidate/vuelidate/compare/@vuelidate/validators@2.0.0-alpha.0...@vuelidate/validators@2.0.0-alpha.1) (2020-08-08) | ||
### Bug Fixes | ||
* **validators:** add unwrap calls to the core helpers like "req", "len", "regex", along with some validators ([b6ec948](https://github.com/vuelidate/vuelidate/commit/b6ec948e629e169f78d2679cb50162aeb3631f2d)) | ||
### Features | ||
* **validators:** add promise as property resolver ([aab11d1](https://github.com/vuelidate/vuelidate/commit/aab11d16a804550f7c3bc3acebd2af0f850486ca)) | ||
* **validators:** expose an isTruthy helper ([dc2235e](https://github.com/vuelidate/vuelidate/commit/dc2235e612e8663b56488eb79eeb7fe99c72b6bc)) | ||
* add $dirty cache WIP ([5725a38](https://github.com/vuelidate/vuelidate/commit/5725a38da12848fc699c719dafa06706107f0374)) | ||
* add validate function and other improvements ([#663](https://github.com/vuelidate/vuelidate/issues/663)) ([0d1ca73](https://github.com/vuelidate/vuelidate/commit/0d1ca73ca5f7574e15256cf8bfa94ea6170dc2dc)) | ||
# [1.0.0-alpha.2](https://github.com/vuelidate/vuelidate/compare/@vuelidate/validators@1.0.0-alpha.1...@vuelidate/validators@1.0.0-alpha.2) (2019-11-05) | ||
@@ -8,0 +27,0 @@ |
@@ -1,6 +0,25 @@ | ||
import { isRef } from '@vue/composition-api'; | ||
import { unref } from 'vue'; | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function isFunction(val) { | ||
return typeof val === 'function'; | ||
} | ||
function isObject(o) { | ||
return o !== null && _typeof(o) === 'object' && !Array.isArray(o); | ||
} | ||
/** | ||
@@ -13,19 +32,12 @@ * Unwraps a ref, returning its value | ||
function unwrap(val) { | ||
return isRef(val) ? val.value : val; | ||
return unref(val); | ||
} | ||
/** | ||
* @typedef ValidatorObject | ||
* @property {Function} $validator | ||
* @property {Function|String} $message | ||
* @property {Object|Array} $params | ||
*/ | ||
/** | ||
* Returns a standard ValidatorObject | ||
* Wraps a plain function into a ValidatorObject | ||
* @param {ValidatorObject|Function} validator | ||
* @return {ValidatorObject} | ||
* @param {NormalizedValidator|Function} validator | ||
* @return {NormalizedValidator} | ||
*/ | ||
function getValidatorObj(validator) { | ||
function normalizeValidatorObject(validator) { | ||
return isFunction(validator.$validator) ? validator : { | ||
@@ -35,2 +47,10 @@ $validator: validator | ||
} | ||
function isPromise(object) { | ||
return isObject(object) && isFunction(object.then); | ||
} | ||
function withAsync(validator) { | ||
var normalized = normalizeValidatorObject(validator); | ||
normalized.$async = true; | ||
return normalized; | ||
} | ||
@@ -40,8 +60,10 @@ /** | ||
* @param {Object} $params | ||
* @param {ValidatorObject|Function} $validator | ||
* @return {ValidatorObject} | ||
* @param {NormalizedValidator|Function} $validator | ||
* @return {NormalizedValidator} | ||
*/ | ||
function withParams($params, $validator) { | ||
var validatorObj = getValidatorObj($validator); | ||
if (!isObject($params)) throw new Error("[@vuelidate/validators]: First parameter to \"withParams\" should be an object, provided ".concat(_typeof($params))); | ||
if (!isObject($validator) && !isFunction($validator)) throw new Error("[@vuelidate/validators]: Validator must be a function or object with $validator parameter"); | ||
var validatorObj = normalizeValidatorObject($validator); | ||
validatorObj.$params = Object.assign({}, validatorObj.$params, {}, $params); | ||
@@ -59,8 +81,10 @@ return validatorObj; | ||
* Attaches a message to a validator | ||
* @param {Function | Object} $validator | ||
* @param {(MessageCallback | String)} $message | ||
* @param {MessageCallback | String} $message | ||
* @param {NormalizedValidator|Function} $validator | ||
*/ | ||
function withMessage($validator, $message) { | ||
var validatorObj = getValidatorObj($validator); | ||
function withMessage($message, $validator) { | ||
if (!isFunction($message) && typeof unwrap($message) !== 'string') throw new Error("[@vuelidate/validators]: First parameter to \"withMessage\" should be string or a function returning a string, provided ".concat(_typeof($message))); | ||
if (!isObject($validator) && !isFunction($validator)) throw new Error("[@vuelidate/validators]: Validator must be a function or object with $validator parameter"); | ||
var validatorObj = normalizeValidatorObject($validator); | ||
validatorObj.$message = $message; | ||
@@ -70,18 +94,4 @@ return validatorObj; | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
// "required" core, used in almost every validator to allow empty values | ||
var req = function req(value) { | ||
value = unwrap(value); | ||
if (Array.isArray(value)) return !!value.length; | ||
@@ -119,2 +129,3 @@ | ||
var len = function len(value) { | ||
value = unwrap(value); | ||
if (Array.isArray(value)) return value.length; | ||
@@ -136,2 +147,3 @@ | ||
return function (value) { | ||
value = unwrap(value); | ||
return !req(value) || expr.test(value); | ||
@@ -149,3 +161,5 @@ }; | ||
len: len, | ||
regex: regex | ||
regex: regex, | ||
unwrap: unwrap, | ||
withAsync: withAsync | ||
}); | ||
@@ -321,5 +335,27 @@ | ||
var validate = function validate(prop, val) { | ||
return prop ? req(val) : true; | ||
}; | ||
/** | ||
* Returns required if the passed property is truthy | ||
* @param {Boolean | String | function(): (Boolean | Promise<boolean>)} prop | ||
* @return {function(*): (Boolean | Promise<Boolean>)} | ||
*/ | ||
var requiredIf = (function (prop) { | ||
return function (value) { | ||
return req(prop) ? req(value) : true; | ||
if (typeof prop !== 'function') { | ||
return validate(prop, value); | ||
} | ||
var result = prop(); | ||
if (isPromise(result)) { | ||
return result.then(function (response) { | ||
return validate(response, value); | ||
}); | ||
} | ||
return validate(result, value); | ||
}; | ||
@@ -335,5 +371,27 @@ }); | ||
var validate$1 = function validate(prop, val) { | ||
return !prop ? req(val) : true; | ||
}; | ||
/** | ||
* Returns required if the passed property is truthy | ||
* @param {Boolean | String | function(): (Boolean | Promise<boolean>)} prop | ||
* @return {function(*): (Boolean | Promise<Boolean>)} | ||
*/ | ||
var requiredUnless = (function (prop) { | ||
return function (value) { | ||
return !req(prop) ? req(value) : true; | ||
if (typeof prop !== 'function') { | ||
return validate$1(prop, value); | ||
} | ||
var result = prop(); | ||
if (isPromise(result)) { | ||
return result.then(function (response) { | ||
return validate$1(response, value); | ||
}); | ||
} | ||
return validate$1(result, value); | ||
}; | ||
@@ -351,3 +409,3 @@ }); | ||
return function (value) { | ||
return value === unwrap(equalTo); | ||
return unwrap(value) === unwrap(equalTo); | ||
}; | ||
@@ -354,0 +412,0 @@ }); |
@@ -5,7 +5,26 @@ 'use strict'; | ||
var compositionApi = require('@vue/composition-api'); | ||
var vue = require('vue'); | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function isFunction(val) { | ||
return typeof val === 'function'; | ||
} | ||
function isObject(o) { | ||
return o !== null && _typeof(o) === 'object' && !Array.isArray(o); | ||
} | ||
/** | ||
@@ -18,19 +37,12 @@ * Unwraps a ref, returning its value | ||
function unwrap(val) { | ||
return compositionApi.isRef(val) ? val.value : val; | ||
return vue.unref(val); | ||
} | ||
/** | ||
* @typedef ValidatorObject | ||
* @property {Function} $validator | ||
* @property {Function|String} $message | ||
* @property {Object|Array} $params | ||
*/ | ||
/** | ||
* Returns a standard ValidatorObject | ||
* Wraps a plain function into a ValidatorObject | ||
* @param {ValidatorObject|Function} validator | ||
* @return {ValidatorObject} | ||
* @param {NormalizedValidator|Function} validator | ||
* @return {NormalizedValidator} | ||
*/ | ||
function getValidatorObj(validator) { | ||
function normalizeValidatorObject(validator) { | ||
return isFunction(validator.$validator) ? validator : { | ||
@@ -40,2 +52,10 @@ $validator: validator | ||
} | ||
function isPromise(object) { | ||
return isObject(object) && isFunction(object.then); | ||
} | ||
function withAsync(validator) { | ||
var normalized = normalizeValidatorObject(validator); | ||
normalized.$async = true; | ||
return normalized; | ||
} | ||
@@ -45,8 +65,10 @@ /** | ||
* @param {Object} $params | ||
* @param {ValidatorObject|Function} $validator | ||
* @return {ValidatorObject} | ||
* @param {NormalizedValidator|Function} $validator | ||
* @return {NormalizedValidator} | ||
*/ | ||
function withParams($params, $validator) { | ||
var validatorObj = getValidatorObj($validator); | ||
if (!isObject($params)) throw new Error("[@vuelidate/validators]: First parameter to \"withParams\" should be an object, provided ".concat(_typeof($params))); | ||
if (!isObject($validator) && !isFunction($validator)) throw new Error("[@vuelidate/validators]: Validator must be a function or object with $validator parameter"); | ||
var validatorObj = normalizeValidatorObject($validator); | ||
validatorObj.$params = Object.assign({}, validatorObj.$params, {}, $params); | ||
@@ -64,8 +86,10 @@ return validatorObj; | ||
* Attaches a message to a validator | ||
* @param {Function | Object} $validator | ||
* @param {(MessageCallback | String)} $message | ||
* @param {MessageCallback | String} $message | ||
* @param {NormalizedValidator|Function} $validator | ||
*/ | ||
function withMessage($validator, $message) { | ||
var validatorObj = getValidatorObj($validator); | ||
function withMessage($message, $validator) { | ||
if (!isFunction($message) && typeof unwrap($message) !== 'string') throw new Error("[@vuelidate/validators]: First parameter to \"withMessage\" should be string or a function returning a string, provided ".concat(_typeof($message))); | ||
if (!isObject($validator) && !isFunction($validator)) throw new Error("[@vuelidate/validators]: Validator must be a function or object with $validator parameter"); | ||
var validatorObj = normalizeValidatorObject($validator); | ||
validatorObj.$message = $message; | ||
@@ -75,18 +99,4 @@ return validatorObj; | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
// "required" core, used in almost every validator to allow empty values | ||
var req = function req(value) { | ||
value = unwrap(value); | ||
if (Array.isArray(value)) return !!value.length; | ||
@@ -124,2 +134,3 @@ | ||
var len = function len(value) { | ||
value = unwrap(value); | ||
if (Array.isArray(value)) return value.length; | ||
@@ -141,2 +152,3 @@ | ||
return function (value) { | ||
value = unwrap(value); | ||
return !req(value) || expr.test(value); | ||
@@ -154,3 +166,5 @@ }; | ||
len: len, | ||
regex: regex | ||
regex: regex, | ||
unwrap: unwrap, | ||
withAsync: withAsync | ||
}); | ||
@@ -326,5 +340,27 @@ | ||
var validate = function validate(prop, val) { | ||
return prop ? req(val) : true; | ||
}; | ||
/** | ||
* Returns required if the passed property is truthy | ||
* @param {Boolean | String | function(): (Boolean | Promise<boolean>)} prop | ||
* @return {function(*): (Boolean | Promise<Boolean>)} | ||
*/ | ||
var requiredIf = (function (prop) { | ||
return function (value) { | ||
return req(prop) ? req(value) : true; | ||
if (typeof prop !== 'function') { | ||
return validate(prop, value); | ||
} | ||
var result = prop(); | ||
if (isPromise(result)) { | ||
return result.then(function (response) { | ||
return validate(response, value); | ||
}); | ||
} | ||
return validate(result, value); | ||
}; | ||
@@ -340,5 +376,27 @@ }); | ||
var validate$1 = function validate(prop, val) { | ||
return !prop ? req(val) : true; | ||
}; | ||
/** | ||
* Returns required if the passed property is truthy | ||
* @param {Boolean | String | function(): (Boolean | Promise<boolean>)} prop | ||
* @return {function(*): (Boolean | Promise<Boolean>)} | ||
*/ | ||
var requiredUnless = (function (prop) { | ||
return function (value) { | ||
return !req(prop) ? req(value) : true; | ||
if (typeof prop !== 'function') { | ||
return validate$1(prop, value); | ||
} | ||
var result = prop(); | ||
if (isPromise(result)) { | ||
return result.then(function (response) { | ||
return validate$1(response, value); | ||
}); | ||
} | ||
return validate$1(result, value); | ||
}; | ||
@@ -356,3 +414,3 @@ }); | ||
return function (value) { | ||
return value === unwrap(equalTo); | ||
return unwrap(value) === unwrap(equalTo); | ||
}; | ||
@@ -359,0 +417,0 @@ }); |
@@ -1,6 +0,25 @@ | ||
import { isRef } from '@vue/composition-api'; | ||
import { unref } from 'vue'; | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function isFunction(val) { | ||
return typeof val === 'function'; | ||
} | ||
function isObject(o) { | ||
return o !== null && _typeof(o) === 'object' && !Array.isArray(o); | ||
} | ||
/** | ||
@@ -13,19 +32,12 @@ * Unwraps a ref, returning its value | ||
function unwrap(val) { | ||
return isRef(val) ? val.value : val; | ||
return unref(val); | ||
} | ||
/** | ||
* @typedef ValidatorObject | ||
* @property {Function} $validator | ||
* @property {Function|String} $message | ||
* @property {Object|Array} $params | ||
*/ | ||
/** | ||
* Returns a standard ValidatorObject | ||
* Wraps a plain function into a ValidatorObject | ||
* @param {ValidatorObject|Function} validator | ||
* @return {ValidatorObject} | ||
* @param {NormalizedValidator|Function} validator | ||
* @return {NormalizedValidator} | ||
*/ | ||
function getValidatorObj(validator) { | ||
function normalizeValidatorObject(validator) { | ||
return isFunction(validator.$validator) ? validator : { | ||
@@ -35,2 +47,10 @@ $validator: validator | ||
} | ||
function isPromise(object) { | ||
return isObject(object) && isFunction(object.then); | ||
} | ||
function withAsync(validator) { | ||
var normalized = normalizeValidatorObject(validator); | ||
normalized.$async = true; | ||
return normalized; | ||
} | ||
@@ -40,8 +60,10 @@ /** | ||
* @param {Object} $params | ||
* @param {ValidatorObject|Function} $validator | ||
* @return {ValidatorObject} | ||
* @param {NormalizedValidator|Function} $validator | ||
* @return {NormalizedValidator} | ||
*/ | ||
function withParams($params, $validator) { | ||
var validatorObj = getValidatorObj($validator); | ||
if (!isObject($params)) throw new Error("[@vuelidate/validators]: First parameter to \"withParams\" should be an object, provided ".concat(_typeof($params))); | ||
if (!isObject($validator) && !isFunction($validator)) throw new Error("[@vuelidate/validators]: Validator must be a function or object with $validator parameter"); | ||
var validatorObj = normalizeValidatorObject($validator); | ||
validatorObj.$params = Object.assign({}, validatorObj.$params, {}, $params); | ||
@@ -59,8 +81,10 @@ return validatorObj; | ||
* Attaches a message to a validator | ||
* @param {Function | Object} $validator | ||
* @param {(MessageCallback | String)} $message | ||
* @param {MessageCallback | String} $message | ||
* @param {NormalizedValidator|Function} $validator | ||
*/ | ||
function withMessage($validator, $message) { | ||
var validatorObj = getValidatorObj($validator); | ||
function withMessage($message, $validator) { | ||
if (!isFunction($message) && typeof unwrap($message) !== 'string') throw new Error("[@vuelidate/validators]: First parameter to \"withMessage\" should be string or a function returning a string, provided ".concat(_typeof($message))); | ||
if (!isObject($validator) && !isFunction($validator)) throw new Error("[@vuelidate/validators]: Validator must be a function or object with $validator parameter"); | ||
var validatorObj = normalizeValidatorObject($validator); | ||
validatorObj.$message = $message; | ||
@@ -70,18 +94,4 @@ return validatorObj; | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
// "required" core, used in almost every validator to allow empty values | ||
var req = function req(value) { | ||
value = unwrap(value); | ||
if (Array.isArray(value)) return !!value.length; | ||
@@ -119,2 +129,3 @@ | ||
var len = function len(value) { | ||
value = unwrap(value); | ||
if (Array.isArray(value)) return value.length; | ||
@@ -136,2 +147,3 @@ | ||
return function (value) { | ||
value = unwrap(value); | ||
return !req(value) || expr.test(value); | ||
@@ -149,3 +161,5 @@ }; | ||
len: len, | ||
regex: regex | ||
regex: regex, | ||
unwrap: unwrap, | ||
withAsync: withAsync | ||
}); | ||
@@ -240,11 +254,55 @@ | ||
var validate = function validate(prop, val) { | ||
return prop ? req(val) : true; | ||
}; | ||
/** | ||
* Returns required if the passed property is truthy | ||
* @param {Boolean | String | function(): (Boolean | Promise<boolean>)} prop | ||
* @return {function(*): (Boolean | Promise<Boolean>)} | ||
*/ | ||
var requiredIf = (function (prop) { | ||
return function (value) { | ||
return req(prop) ? req(value) : true; | ||
if (typeof prop !== 'function') { | ||
return validate(prop, value); | ||
} | ||
var result = prop(); | ||
if (isPromise(result)) { | ||
return result.then(function (response) { | ||
return validate(response, value); | ||
}); | ||
} | ||
return validate(result, value); | ||
}; | ||
}); | ||
var validate$1 = function validate(prop, val) { | ||
return !prop ? req(val) : true; | ||
}; | ||
/** | ||
* Returns required if the passed property is truthy | ||
* @param {Boolean | String | function(): (Boolean | Promise<boolean>)} prop | ||
* @return {function(*): (Boolean | Promise<Boolean>)} | ||
*/ | ||
var requiredUnless = (function (prop) { | ||
return function (value) { | ||
return !req(prop) ? req(value) : true; | ||
if (typeof prop !== 'function') { | ||
return validate$1(prop, value); | ||
} | ||
var result = prop(); | ||
if (isPromise(result)) { | ||
return result.then(function (response) { | ||
return validate$1(response, value); | ||
}); | ||
} | ||
return validate$1(result, value); | ||
}; | ||
@@ -255,3 +313,3 @@ }); | ||
return function (value) { | ||
return value === unwrap(equalTo); | ||
return unwrap(value) === unwrap(equalTo); | ||
}; | ||
@@ -258,0 +316,0 @@ }); |
134
dist/raw.js
@@ -5,7 +5,26 @@ 'use strict'; | ||
var compositionApi = require('@vue/composition-api'); | ||
var vue = require('vue'); | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function isFunction(val) { | ||
return typeof val === 'function'; | ||
} | ||
function isObject(o) { | ||
return o !== null && _typeof(o) === 'object' && !Array.isArray(o); | ||
} | ||
/** | ||
@@ -18,19 +37,12 @@ * Unwraps a ref, returning its value | ||
function unwrap(val) { | ||
return compositionApi.isRef(val) ? val.value : val; | ||
return vue.unref(val); | ||
} | ||
/** | ||
* @typedef ValidatorObject | ||
* @property {Function} $validator | ||
* @property {Function|String} $message | ||
* @property {Object|Array} $params | ||
*/ | ||
/** | ||
* Returns a standard ValidatorObject | ||
* Wraps a plain function into a ValidatorObject | ||
* @param {ValidatorObject|Function} validator | ||
* @return {ValidatorObject} | ||
* @param {NormalizedValidator|Function} validator | ||
* @return {NormalizedValidator} | ||
*/ | ||
function getValidatorObj(validator) { | ||
function normalizeValidatorObject(validator) { | ||
return isFunction(validator.$validator) ? validator : { | ||
@@ -40,2 +52,10 @@ $validator: validator | ||
} | ||
function isPromise(object) { | ||
return isObject(object) && isFunction(object.then); | ||
} | ||
function withAsync(validator) { | ||
var normalized = normalizeValidatorObject(validator); | ||
normalized.$async = true; | ||
return normalized; | ||
} | ||
@@ -45,8 +65,10 @@ /** | ||
* @param {Object} $params | ||
* @param {ValidatorObject|Function} $validator | ||
* @return {ValidatorObject} | ||
* @param {NormalizedValidator|Function} $validator | ||
* @return {NormalizedValidator} | ||
*/ | ||
function withParams($params, $validator) { | ||
var validatorObj = getValidatorObj($validator); | ||
if (!isObject($params)) throw new Error("[@vuelidate/validators]: First parameter to \"withParams\" should be an object, provided ".concat(_typeof($params))); | ||
if (!isObject($validator) && !isFunction($validator)) throw new Error("[@vuelidate/validators]: Validator must be a function or object with $validator parameter"); | ||
var validatorObj = normalizeValidatorObject($validator); | ||
validatorObj.$params = Object.assign({}, validatorObj.$params, {}, $params); | ||
@@ -64,8 +86,10 @@ return validatorObj; | ||
* Attaches a message to a validator | ||
* @param {Function | Object} $validator | ||
* @param {(MessageCallback | String)} $message | ||
* @param {MessageCallback | String} $message | ||
* @param {NormalizedValidator|Function} $validator | ||
*/ | ||
function withMessage($validator, $message) { | ||
var validatorObj = getValidatorObj($validator); | ||
function withMessage($message, $validator) { | ||
if (!isFunction($message) && typeof unwrap($message) !== 'string') throw new Error("[@vuelidate/validators]: First parameter to \"withMessage\" should be string or a function returning a string, provided ".concat(_typeof($message))); | ||
if (!isObject($validator) && !isFunction($validator)) throw new Error("[@vuelidate/validators]: Validator must be a function or object with $validator parameter"); | ||
var validatorObj = normalizeValidatorObject($validator); | ||
validatorObj.$message = $message; | ||
@@ -75,18 +99,4 @@ return validatorObj; | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
// "required" core, used in almost every validator to allow empty values | ||
var req = function req(value) { | ||
value = unwrap(value); | ||
if (Array.isArray(value)) return !!value.length; | ||
@@ -124,2 +134,3 @@ | ||
var len = function len(value) { | ||
value = unwrap(value); | ||
if (Array.isArray(value)) return value.length; | ||
@@ -141,2 +152,3 @@ | ||
return function (value) { | ||
value = unwrap(value); | ||
return !req(value) || expr.test(value); | ||
@@ -154,3 +166,5 @@ }; | ||
len: len, | ||
regex: regex | ||
regex: regex, | ||
unwrap: unwrap, | ||
withAsync: withAsync | ||
}); | ||
@@ -245,11 +259,55 @@ | ||
var validate = function validate(prop, val) { | ||
return prop ? req(val) : true; | ||
}; | ||
/** | ||
* Returns required if the passed property is truthy | ||
* @param {Boolean | String | function(): (Boolean | Promise<boolean>)} prop | ||
* @return {function(*): (Boolean | Promise<Boolean>)} | ||
*/ | ||
var requiredIf = (function (prop) { | ||
return function (value) { | ||
return req(prop) ? req(value) : true; | ||
if (typeof prop !== 'function') { | ||
return validate(prop, value); | ||
} | ||
var result = prop(); | ||
if (isPromise(result)) { | ||
return result.then(function (response) { | ||
return validate(response, value); | ||
}); | ||
} | ||
return validate(result, value); | ||
}; | ||
}); | ||
var validate$1 = function validate(prop, val) { | ||
return !prop ? req(val) : true; | ||
}; | ||
/** | ||
* Returns required if the passed property is truthy | ||
* @param {Boolean | String | function(): (Boolean | Promise<boolean>)} prop | ||
* @return {function(*): (Boolean | Promise<Boolean>)} | ||
*/ | ||
var requiredUnless = (function (prop) { | ||
return function (value) { | ||
return !req(prop) ? req(value) : true; | ||
if (typeof prop !== 'function') { | ||
return validate$1(prop, value); | ||
} | ||
var result = prop(); | ||
if (isPromise(result)) { | ||
return result.then(function (response) { | ||
return validate$1(response, value); | ||
}); | ||
} | ||
return validate$1(result, value); | ||
}; | ||
@@ -260,3 +318,3 @@ }); | ||
return function (value) { | ||
return value === unwrap(equalTo); | ||
return unwrap(value) === unwrap(equalTo); | ||
}; | ||
@@ -263,0 +321,0 @@ }); |
{ | ||
"name": "@vuelidate/validators", | ||
"version": "2.0.0-alpha.0", | ||
"version": "2.0.0-alpha.1", | ||
"description": "Validators for Vuelidate", | ||
@@ -11,2 +11,3 @@ "main": "dist/index.js", | ||
"build": "bili src src/raw --format esm --format cjs", | ||
"dev": "yarn build --watch", | ||
"test:unit": "jest", | ||
@@ -18,2 +19,3 @@ "lint": "eslint src" | ||
"eslint-plugin-jest": "^23.0.2", | ||
"flush-promises": "^1.0.2", | ||
"jest": "^24.9.0" | ||
@@ -27,3 +29,3 @@ }, | ||
}, | ||
"gitHead": "9d70a3f28f6040e06301758363bb2e57dbb18b97" | ||
"gitHead": "ca47a83c8ec456140ed18a3353272a882d2d221d" | ||
} |
@@ -6,1 +6,2 @@ import withParams from './utils/withParams' | ||
export { withParams, withMessage, req, len, regex } | ||
export { unwrap, withAsync } from './utils/common' |
@@ -1,2 +0,2 @@ | ||
import alpha from '@vuelidate/validators/src/raw/alpha' | ||
import alpha from '../alpha' | ||
@@ -3,0 +3,0 @@ describe('alpha validator', () => { |
@@ -1,2 +0,2 @@ | ||
import alphaNum from '@vuelidate/validators/src/raw/alphaNum' | ||
import alphaNum from '../alphaNum' | ||
@@ -3,0 +3,0 @@ describe('alphaNum validator', () => { |
@@ -32,6 +32,6 @@ import and from '../and' | ||
it('should pass values and model to function', () => { | ||
const spy = sinon.spy() | ||
const spy = jest.fn() | ||
and(spy)(1, 2) | ||
expect(spy).to.have.been.calledWith(1, 2) | ||
expect(spy).toHaveBeenCalledWith(1, 2) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
import decimal from '@vuelidate/validators/src/raw/decimal' | ||
import decimal from '../decimal' | ||
@@ -3,0 +3,0 @@ describe('decimal validator', () => { |
@@ -5,2 +5,4 @@ import requiredIf from '../requiredIf' | ||
const F = () => false | ||
const promiseT = () => Promise.resolve(true) | ||
const promiseF = () => Promise.resolve(false) | ||
@@ -16,9 +18,23 @@ describe('requiredIf validator', () => { | ||
it('should not validate empty string when prop condition is met', () => { | ||
expect(requiredIf('prop')('', { prop: true })).toBe(false) | ||
it('should not validate empty string when simple boolean condition is met', () => { | ||
expect(requiredIf('prop')('')).toBe(false) | ||
}) | ||
it('should validate empty string when prop condition not met', () => { | ||
expect(requiredIf('prop')('', { prop: false })).toBe(true) | ||
it('should validate empty string when simple boolean condition not met', () => { | ||
expect(requiredIf('')('')).toBe(true) | ||
}) | ||
it('should return a promise when passed a promise condition', () => { | ||
const response = requiredIf(promiseT)('') | ||
expect(response).toHaveProperty('then') // is a promise | ||
}) | ||
it('should validate value if condition is a truthy promise', async () => { | ||
expect(await requiredIf(promiseT)('')).toBe(false) | ||
expect(await requiredIf(promiseT)('someValue')).toBe(true) | ||
}) | ||
it('should NOT validate value if condition is a falsy promise', async () => { | ||
expect(await requiredIf(promiseF)('')).toBe(true) | ||
}) | ||
}) |
@@ -7,17 +7,11 @@ import requiredUnless from '../requiredUnless' | ||
describe('requiredUnless validator', () => { | ||
it('should not validate empty string when functional condition is not met', () => { | ||
it('should not validate if prop is falsy', () => { | ||
expect(requiredUnless(F)('')).toBe(false) | ||
expect(requiredUnless(F)('truthy value')).toBe(true) | ||
}) | ||
it('should validate empty string when functional condition met', () => { | ||
it('should not validate when prop condition is truthy', () => { | ||
expect(requiredUnless(T)('')).toBe(true) | ||
expect(requiredUnless(T)('truthy value')).toBe(true) | ||
}) | ||
it('should not validate empty string when prop condition is not met', () => { | ||
expect(requiredUnless('prop')('', { prop: false })).toBe(false) | ||
}) | ||
it('should validate empty string when prop condition met', () => { | ||
expect(requiredUnless('prop')('', { prop: true })).toBe(true) | ||
}) | ||
}) |
import sameAs from '../sameAs' | ||
describe('sameAs validator', () => { | ||
const parentVm = { | ||
first: 'hello', | ||
second: 'world', | ||
undef: undefined, | ||
nil: null, | ||
empty: '' | ||
} | ||
it('should not validate different values', () => { | ||
expect(sameAs('first')('world', parentVm)).toBe(false) | ||
expect(sameAs('second')('hello', parentVm)).toBe(false) | ||
expect(sameAs('first')(undefined, parentVm)).toBe(false) | ||
expect(sameAs('first')(null, parentVm)).toBe(false) | ||
expect(sameAs('first')('', parentVm)).toBe(false) | ||
expect(sameAs('undef')('any', parentVm)).toBe(false) | ||
expect(sameAs('nil')('any', parentVm)).toBe(false) | ||
expect(sameAs('empty')('any', parentVm)).toBe(false) | ||
expect(sameAs('empty')('any')).toBe(false) | ||
}) | ||
it('should validate identical values', () => { | ||
expect(sameAs('first')('hello', parentVm)).toBe(true) | ||
expect(sameAs('second')('world', parentVm)).toBe(true) | ||
expect(sameAs('undef')(undefined, parentVm)).toBe(true) | ||
expect(sameAs('nil')(null, parentVm)).toBe(true) | ||
expect(sameAs('empty')('', parentVm)).toBe(true) | ||
expect(sameAs('first')('first')).toBe(true) | ||
}) | ||
it('should allow function expression', () => { | ||
expect(sameAs((p) => p.first)('hello', parentVm)).toBe(true) | ||
}) | ||
}) |
// "required" core, used in almost every validator to allow empty values | ||
import { unwrap } from '../utils/common' | ||
export const req = (value) => { | ||
value = unwrap(value) | ||
if (Array.isArray(value)) return !!value.length | ||
@@ -32,2 +35,3 @@ if (value === undefined || value === null) { | ||
export const len = (value) => { | ||
value = unwrap(value) | ||
if (Array.isArray(value)) return value.length | ||
@@ -45,2 +49,5 @@ if (typeof value === 'object') { | ||
*/ | ||
export const regex = expr => value => !req(value) || expr.test(value) | ||
export const regex = expr => value => { | ||
value = unwrap(value) | ||
return !req(value) || expr.test(value) | ||
} |
import { req } from '../common' | ||
import { isPromise } from '../utils/common' | ||
// TODO: Double check | ||
export default (prop) => | ||
function (value) { | ||
return req(prop) ? req(value) : true | ||
const validate = (prop, val) => prop ? req(val) : true | ||
/** | ||
* Returns required if the passed property is truthy | ||
* @param {Boolean | String | function(): (Boolean | Promise<boolean>)} prop | ||
* @return {function(*): (Boolean | Promise<Boolean>)} | ||
*/ | ||
export default (prop) => (value) => { | ||
if (typeof prop !== 'function') { | ||
return validate(prop, value) | ||
} | ||
const result = prop() | ||
if (isPromise(result)) { | ||
return result.then((response) => { | ||
return validate(response, value) | ||
}) | ||
} | ||
return validate(result, value) | ||
} |
import { req } from '../common' | ||
import { isPromise } from '../utils/common' | ||
// TODO: double check | ||
export default (prop) => | ||
function (value) { | ||
return !req(prop) ? req(value) : true | ||
const validate = (prop, val) => !prop ? req(val) : true | ||
/** | ||
* Returns required if the passed property is truthy | ||
* @param {Boolean | String | function(): (Boolean | Promise<boolean>)} prop | ||
* @return {function(*): (Boolean | Promise<Boolean>)} | ||
*/ | ||
export default (prop) => (value) => { | ||
if (typeof prop !== 'function') { | ||
return validate(prop, value) | ||
} | ||
const result = prop() | ||
if (isPromise(result)) { | ||
return result.then((response) => { | ||
return validate(response, value) | ||
}) | ||
} | ||
return validate(result, value) | ||
} |
import { unwrap } from '../utils/common' | ||
export default equalTo => value => value === unwrap(equalTo) | ||
export default equalTo => value => unwrap(value) === unwrap(equalTo) |
@@ -1,2 +0,2 @@ | ||
import { isRef } from '@vue/composition-api' | ||
import { unref } from 'vue' | ||
@@ -7,2 +7,6 @@ export function isFunction (val) { | ||
export function isObject (o) { | ||
return o !== null && typeof o === 'object' && !Array.isArray(o) | ||
} | ||
/** | ||
@@ -14,19 +18,12 @@ * Unwraps a ref, returning its value | ||
export function unwrap (val) { | ||
return isRef(val) ? val.value : val | ||
return unref(val) | ||
} | ||
/** | ||
* @typedef ValidatorObject | ||
* @property {Function} $validator | ||
* @property {Function|String} $message | ||
* @property {Object|Array} $params | ||
*/ | ||
/** | ||
* Returns a standard ValidatorObject | ||
* Wraps a plain function into a ValidatorObject | ||
* @param {ValidatorObject|Function} validator | ||
* @return {ValidatorObject} | ||
* @param {NormalizedValidator|Function} validator | ||
* @return {NormalizedValidator} | ||
*/ | ||
export function getValidatorObj (validator) { | ||
export function normalizeValidatorObject (validator) { | ||
return isFunction(validator.$validator) | ||
@@ -38,1 +35,21 @@ ? validator | ||
} | ||
/** | ||
* Returns whether a value is truthy ot not. | ||
* @param {Function|*} prop | ||
* @return {boolean} | ||
*/ | ||
export function isTruthy (prop) { | ||
prop = unwrap(prop) | ||
return Boolean(isFunction(prop) ? prop() : prop) | ||
} | ||
export function isPromise (object) { | ||
return isObject(object) && isFunction(object.then) | ||
} | ||
export function withAsync (validator) { | ||
const normalized = normalizeValidatorObject(validator) | ||
normalized.$async = true | ||
return normalized | ||
} |
@@ -1,2 +0,2 @@ | ||
import { getValidatorObj } from './common' | ||
import { normalizeValidatorObject, isFunction, isObject, unwrap } from './common' | ||
@@ -11,7 +11,10 @@ /** | ||
* Attaches a message to a validator | ||
* @param {Function | Object} $validator | ||
* @param {(MessageCallback | String)} $message | ||
* @param {MessageCallback | String} $message | ||
* @param {NormalizedValidator|Function} $validator | ||
*/ | ||
export default function withMessage ($validator, $message) { | ||
const validatorObj = getValidatorObj($validator) | ||
export default function withMessage ($message, $validator) { | ||
if (!isFunction($message) && typeof unwrap($message) !== 'string') throw new Error(`[@vuelidate/validators]: First parameter to "withMessage" should be string or a function returning a string, provided ${typeof $message}`) | ||
if (!isObject($validator) && !isFunction($validator)) throw new Error(`[@vuelidate/validators]: Validator must be a function or object with $validator parameter`) | ||
const validatorObj = normalizeValidatorObject($validator) | ||
validatorObj.$message = $message | ||
@@ -18,0 +21,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { getValidatorObj } from './common' | ||
import { normalizeValidatorObject, isFunction, isObject } from './common' | ||
@@ -6,8 +6,11 @@ /** | ||
* @param {Object} $params | ||
* @param {ValidatorObject|Function} $validator | ||
* @return {ValidatorObject} | ||
* @param {NormalizedValidator|Function} $validator | ||
* @return {NormalizedValidator} | ||
*/ | ||
export default function withParams ($params, $validator) { | ||
const validatorObj = getValidatorObj($validator) | ||
if (!isObject($params)) throw new Error(`[@vuelidate/validators]: First parameter to "withParams" should be an object, provided ${typeof $params}`) | ||
if (!isObject($validator) && !isFunction($validator)) throw new Error(`[@vuelidate/validators]: Validator must be a function or object with $validator parameter`) | ||
const validatorObj = normalizeValidatorObject($validator) | ||
validatorObj.$params = { | ||
@@ -14,0 +17,0 @@ ...validatorObj.$params, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
328548
126
3816
4