@briza/wegood
Advanced tools
Comparing version 1.0.8 to 1.0.9
# wegood changelog | ||
## 1.0.9 | ||
* Feature: add Today's date argument to date validator. | ||
## 1.0.8 | ||
@@ -4,0 +8,0 @@ |
@@ -59,3 +59,3 @@ function _typeof(obj) { | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(n); | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
@@ -72,5 +72,8 @@ } | ||
function _createForOfIteratorHelper(o) { | ||
function _createForOfIteratorHelper(o, allowArrayLike) { | ||
var it; | ||
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { | ||
if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
@@ -101,4 +104,3 @@ | ||
var it, | ||
normalCompletion = true, | ||
var normalCompletion = true, | ||
didErr = false, | ||
@@ -129,8 +131,5 @@ err; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/** | ||
* Is Null or Undefined predicate. | ||
* @param value Rested value. | ||
* @return | ||
*/ | ||
@@ -143,3 +142,2 @@ function isNullOrUndefined(value) { | ||
* @param value Tested value. | ||
* @return | ||
*/ | ||
@@ -153,3 +151,2 @@ | ||
* @param value | ||
* @return | ||
*/ | ||
@@ -163,3 +160,2 @@ | ||
* @param value | ||
* @return | ||
*/ | ||
@@ -177,2 +173,3 @@ | ||
function isFunction(value) { | ||
// eslint-disable-line @typescript-eslint/ban-types | ||
return typeof value === 'function'; | ||
@@ -183,3 +180,2 @@ } | ||
* @param value | ||
* @return | ||
*/ | ||
@@ -193,7 +189,14 @@ | ||
* @param value tested value | ||
* @return result of the test | ||
*/ | ||
function isObject(value) { | ||
return _typeof(value) === 'object' && value.constructor === Object; | ||
if (value === null || value === undefined) { | ||
return false; | ||
} | ||
if (_typeof(value) !== 'object' || (value === null || value === void 0 ? void 0 : value.constructor) !== Object) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
@@ -208,4 +211,4 @@ | ||
// Boundary type used to determine if the date boundary should be before | ||
// or after the specific date. | ||
var BoundaryType; | ||
@@ -263,2 +266,4 @@ /** | ||
* @param dir Boundary direction, 0 start, 1 end. | ||
* @param todayDate Today's date to be used as reference | ||
* validating ranges. | ||
* @return Function accepting the relative | ||
@@ -269,3 +274,3 @@ * date which should be used in the comparison procedure. | ||
function dateBoundary(filter, dir) { | ||
function dateBoundary(filter, dir, todayDate) { | ||
if (isNullOrUndefined(filter)) { | ||
@@ -334,4 +339,2 @@ return function () { | ||
var todayDate = today(); | ||
if (dir === BoundaryType.Start) { | ||
@@ -375,2 +378,5 @@ return date.getTime() >= todayDate.getTime() + offset * 86400000; | ||
* convert the testing value into date object. | ||
* @param todayDate Optional Today's date to be used as reference | ||
* validating ranges. If not provided, the start of the day in | ||
* the current runtime timezone will be used. | ||
* @return validation function, fn(value) => true|string, | ||
@@ -380,3 +386,5 @@ * returns true when valid, error message otherwise. | ||
function date(errorMsg, start, end, transform) { | ||
function date(errorMsg, start, end, transform, todayDate) { | ||
var _todayDate; | ||
// No boundaries | ||
@@ -390,4 +398,5 @@ if (isNullOrUndefined(start) && isNullOrUndefined(end)) { | ||
var startBoundary = dateBoundary(start, BoundaryType.Start); | ||
var endBoundary = dateBoundary(end, BoundaryType.End); | ||
todayDate = (_todayDate = todayDate) !== null && _todayDate !== void 0 ? _todayDate : today(); | ||
var startBoundary = dateBoundary(start, BoundaryType.Start, todayDate); | ||
var endBoundary = dateBoundary(end, BoundaryType.End, todayDate); | ||
return function (value) { | ||
@@ -431,106 +440,2 @@ if (isNullOrUndefined(value)) { | ||
/** | ||
* Create year boundary based on the boundary type. | ||
* @param filter Year filter. | ||
* @param dir Boundary direction, 0 start, 1 end. | ||
* @return Function accepting the relative year which should be used | ||
* in the comparison procedure. | ||
*/ | ||
function yearBoundary(filter, dir) { | ||
if (isNullOrUndefined(filter)) { | ||
return function () { | ||
return true; | ||
}; | ||
} | ||
var isStringValue = isString(filter); // Boundary offset in years | ||
var offset; // Dynamic offset | ||
var dynamicOffset; | ||
if (isStringValue) { | ||
dynamicOffset = filter.match(/^(-?[1-9]\d*)y$/); | ||
} | ||
if (dynamicOffset && dynamicOffset !== null) { | ||
offset = parseInt(dynamicOffset[1]); // or -1,1 (in past, in future) | ||
} else if (['-1', '1'].includes("".concat(filter))) { | ||
offset = Infinity * parseInt(filter); // Start 1 = tomorrow | ||
if (dir === BoundaryType.Start && offset > 0) { | ||
offset = 1; // 1 year in future, dynamic offset. | ||
} // End -1 = any date in past | ||
if (dir === BoundaryType.End && offset < 0) { | ||
offset = -1; // 1 year in past, dynamic offset. | ||
} // Current year | ||
} else if (filter === 0) { | ||
offset = 0; | ||
} | ||
return function (year) { | ||
var currentYear = today().getFullYear(); | ||
if (offset !== undefined) { | ||
return dir === BoundaryType.Start ? year >= currentYear + offset : year <= currentYear + offset; | ||
} | ||
return dir === BoundaryType.Start ? year >= filter : year <= filter; | ||
}; | ||
} | ||
/** | ||
* Year validation rule. | ||
* @param errorMsg Error message. | ||
* @param start Start year boundary. | ||
* If null or undefined, there is no start boundary. | ||
* @param end End year boundary. | ||
* If null or undefined, there is no end boundary. | ||
* @return validation function, fn(value) => true|string, | ||
* returns true when valid, error message otherwise. | ||
*/ | ||
function year(errorMsg, start, end) { | ||
// No boundaries | ||
if (isNullOrUndefined(start) && isNullOrUndefined(end)) { | ||
console.warn('the year validation rule without start and end has ' + 'no validation effect, it will be always validated as true.'); | ||
return function () { | ||
return true; | ||
}; | ||
} | ||
var startBoundary = yearBoundary(start, BoundaryType.Start); | ||
var endBoundary = yearBoundary(end, BoundaryType.End); | ||
return function (value) { | ||
if (isNullOrUndefined(value)) { | ||
return errorMsg; | ||
} // Get the year | ||
if (isDate(value)) { | ||
value = value.getFullYear(); | ||
} else { | ||
value = parseInt(value, 10); | ||
} | ||
if (value < 0) { | ||
console.warn("the year validation rule, invalid \"".concat(value, "\" year value")); | ||
return errorMsg; | ||
} // Check the value boundaries | ||
if (startBoundary(value) === false || endBoundary(value) === false) { | ||
return errorMsg; | ||
} | ||
return true; | ||
}; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
/** | ||
* Equal validation rule. | ||
@@ -570,3 +475,2 @@ * @param errorMsg Error message. | ||
*/ | ||
function exclude(errorMsg, exclusions) { | ||
@@ -598,3 +502,2 @@ // No exclusions | ||
*/ | ||
function include(errorMsg, inclusions) { | ||
@@ -629,3 +532,2 @@ // No inclusions | ||
*/ | ||
function length(errorMsg, min, max) { | ||
@@ -673,3 +575,2 @@ var minLength = isNumber(min); | ||
*/ | ||
function pattern(errorMsg, pattern) { | ||
@@ -698,2 +599,30 @@ return function (value) { | ||
/** | ||
* Present validation rule. | ||
* @param errorMsg Error message. | ||
* @return validation function, fn(value) => true|string, | ||
* returns true when valid, error message otherwise. | ||
*/ | ||
function present(errorMsg) { | ||
return function (value) { | ||
if (isNullOrUndefined(value)) { | ||
return errorMsg; | ||
} | ||
if (isFunction(value) || isObject(value)) { | ||
throw new Error("present validation rule cannot validate \"".concat(value, "\"")); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.length > 0 ? true : errorMsg; | ||
} | ||
if ((value + '').trim() === '') { | ||
return errorMsg; | ||
} | ||
return true; | ||
}; | ||
} | ||
/** | ||
* Range validation rule. | ||
@@ -708,3 +637,2 @@ * @param errorMsg Error message. | ||
*/ | ||
function range(errorMsg, min, max) { | ||
@@ -748,4 +676,61 @@ var minBoundary = isNumber(min); | ||
/** | ||
* Present validation rule. | ||
* Create year boundary based on the boundary type. | ||
* @param filter Year filter. | ||
* @param dir Boundary direction, 0 start, 1 end. | ||
* @return Function accepting the relative year which should be used | ||
* in the comparison procedure. | ||
*/ | ||
function yearBoundary(filter, dir) { | ||
if (isNullOrUndefined(filter)) { | ||
return function () { | ||
return true; | ||
}; | ||
} | ||
var isStringValue = isString(filter); // Boundary offset in years | ||
var offset; // Dynamic offset | ||
var dynamicOffset; | ||
if (isStringValue) { | ||
dynamicOffset = filter.match(/^(-?[1-9]\d*)y$/); | ||
} | ||
if (dynamicOffset && dynamicOffset !== null) { | ||
offset = parseInt(dynamicOffset[1]); // or -1,1 (in past, in future) | ||
} else if (['-1', '1'].includes("".concat(filter))) { | ||
offset = Infinity * parseInt(filter); // Start 1 = tomorrow | ||
if (dir === BoundaryType.Start && offset > 0) { | ||
offset = 1; // 1 year in future, dynamic offset. | ||
} // End -1 = any date in past | ||
if (dir === BoundaryType.End && offset < 0) { | ||
offset = -1; // 1 year in past, dynamic offset. | ||
} // Current year | ||
} else if (filter === 0) { | ||
offset = 0; | ||
} | ||
return function (year) { | ||
var currentYear = today().getFullYear(); | ||
if (offset !== undefined) { | ||
return dir === BoundaryType.Start ? year >= currentYear + offset : year <= currentYear + offset; | ||
} | ||
return dir === BoundaryType.Start ? year >= filter : year <= filter; | ||
}; | ||
} | ||
/** | ||
* Year validation rule. | ||
* @param errorMsg Error message. | ||
* @param start Start year boundary. | ||
* If null or undefined, there is no start boundary. | ||
* @param end End year boundary. | ||
* If null or undefined, there is no end boundary. | ||
* @return validation function, fn(value) => true|string, | ||
@@ -755,18 +740,34 @@ * returns true when valid, error message otherwise. | ||
function present(errorMsg) { | ||
function year(errorMsg, start, end) { | ||
// No boundaries | ||
if (isNullOrUndefined(start) && isNullOrUndefined(end)) { | ||
console.warn('the year validation rule without start and end has ' + 'no validation effect, it will be always validated as true.'); | ||
return function () { | ||
return true; | ||
}; | ||
} | ||
var startBoundary = yearBoundary(start, BoundaryType.Start); | ||
var endBoundary = yearBoundary(end, BoundaryType.End); | ||
return function (value) { | ||
if (isNullOrUndefined(value)) { | ||
return errorMsg; | ||
} | ||
} // Get the year | ||
if (isFunction(value) || isObject(value)) { | ||
throw new Error("present validation rule cannot validate \"".concat(value, "\"")); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.length > 0 ? true : errorMsg; | ||
if (isDate(value)) { | ||
value = value.getFullYear(); | ||
} else { | ||
value = parseInt(value, 10); | ||
} | ||
if ((value + '').trim() === '') { | ||
if (value < 0) { | ||
console.warn("the year validation rule, invalid \"".concat(value, "\" year value")); | ||
return errorMsg; | ||
} // Check the value boundaries | ||
if (startBoundary(value) === false || endBoundary(value) === false) { | ||
return errorMsg; | ||
} | ||
@@ -773,0 +774,0 @@ |
@@ -63,3 +63,3 @@ 'use strict'; | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(n); | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
@@ -76,5 +76,8 @@ } | ||
function _createForOfIteratorHelper(o) { | ||
function _createForOfIteratorHelper(o, allowArrayLike) { | ||
var it; | ||
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { | ||
if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
@@ -105,4 +108,3 @@ | ||
var it, | ||
normalCompletion = true, | ||
var normalCompletion = true, | ||
didErr = false, | ||
@@ -133,8 +135,5 @@ err; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/** | ||
* Is Null or Undefined predicate. | ||
* @param value Rested value. | ||
* @return | ||
*/ | ||
@@ -147,3 +146,2 @@ function isNullOrUndefined(value) { | ||
* @param value Tested value. | ||
* @return | ||
*/ | ||
@@ -157,3 +155,2 @@ | ||
* @param value | ||
* @return | ||
*/ | ||
@@ -167,3 +164,2 @@ | ||
* @param value | ||
* @return | ||
*/ | ||
@@ -181,2 +177,3 @@ | ||
function isFunction(value) { | ||
// eslint-disable-line @typescript-eslint/ban-types | ||
return typeof value === 'function'; | ||
@@ -187,3 +184,2 @@ } | ||
* @param value | ||
* @return | ||
*/ | ||
@@ -197,7 +193,14 @@ | ||
* @param value tested value | ||
* @return result of the test | ||
*/ | ||
function isObject(value) { | ||
return _typeof(value) === 'object' && value.constructor === Object; | ||
if (value === null || value === undefined) { | ||
return false; | ||
} | ||
if (_typeof(value) !== 'object' || (value === null || value === void 0 ? void 0 : value.constructor) !== Object) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
@@ -212,4 +215,4 @@ | ||
// Boundary type used to determine if the date boundary should be before | ||
// or after the specific date. | ||
var BoundaryType; | ||
@@ -267,2 +270,4 @@ /** | ||
* @param dir Boundary direction, 0 start, 1 end. | ||
* @param todayDate Today's date to be used as reference | ||
* validating ranges. | ||
* @return Function accepting the relative | ||
@@ -273,3 +278,3 @@ * date which should be used in the comparison procedure. | ||
function dateBoundary(filter, dir) { | ||
function dateBoundary(filter, dir, todayDate) { | ||
if (isNullOrUndefined(filter)) { | ||
@@ -338,4 +343,2 @@ return function () { | ||
var todayDate = today(); | ||
if (dir === BoundaryType.Start) { | ||
@@ -379,2 +382,5 @@ return date.getTime() >= todayDate.getTime() + offset * 86400000; | ||
* convert the testing value into date object. | ||
* @param todayDate Optional Today's date to be used as reference | ||
* validating ranges. If not provided, the start of the day in | ||
* the current runtime timezone will be used. | ||
* @return validation function, fn(value) => true|string, | ||
@@ -384,3 +390,5 @@ * returns true when valid, error message otherwise. | ||
function date(errorMsg, start, end, transform) { | ||
function date(errorMsg, start, end, transform, todayDate) { | ||
var _todayDate; | ||
// No boundaries | ||
@@ -394,4 +402,5 @@ if (isNullOrUndefined(start) && isNullOrUndefined(end)) { | ||
var startBoundary = dateBoundary(start, BoundaryType.Start); | ||
var endBoundary = dateBoundary(end, BoundaryType.End); | ||
todayDate = (_todayDate = todayDate) !== null && _todayDate !== void 0 ? _todayDate : today(); | ||
var startBoundary = dateBoundary(start, BoundaryType.Start, todayDate); | ||
var endBoundary = dateBoundary(end, BoundaryType.End, todayDate); | ||
return function (value) { | ||
@@ -435,106 +444,2 @@ if (isNullOrUndefined(value)) { | ||
/** | ||
* Create year boundary based on the boundary type. | ||
* @param filter Year filter. | ||
* @param dir Boundary direction, 0 start, 1 end. | ||
* @return Function accepting the relative year which should be used | ||
* in the comparison procedure. | ||
*/ | ||
function yearBoundary(filter, dir) { | ||
if (isNullOrUndefined(filter)) { | ||
return function () { | ||
return true; | ||
}; | ||
} | ||
var isStringValue = isString(filter); // Boundary offset in years | ||
var offset; // Dynamic offset | ||
var dynamicOffset; | ||
if (isStringValue) { | ||
dynamicOffset = filter.match(/^(-?[1-9]\d*)y$/); | ||
} | ||
if (dynamicOffset && dynamicOffset !== null) { | ||
offset = parseInt(dynamicOffset[1]); // or -1,1 (in past, in future) | ||
} else if (['-1', '1'].includes("".concat(filter))) { | ||
offset = Infinity * parseInt(filter); // Start 1 = tomorrow | ||
if (dir === BoundaryType.Start && offset > 0) { | ||
offset = 1; // 1 year in future, dynamic offset. | ||
} // End -1 = any date in past | ||
if (dir === BoundaryType.End && offset < 0) { | ||
offset = -1; // 1 year in past, dynamic offset. | ||
} // Current year | ||
} else if (filter === 0) { | ||
offset = 0; | ||
} | ||
return function (year) { | ||
var currentYear = today().getFullYear(); | ||
if (offset !== undefined) { | ||
return dir === BoundaryType.Start ? year >= currentYear + offset : year <= currentYear + offset; | ||
} | ||
return dir === BoundaryType.Start ? year >= filter : year <= filter; | ||
}; | ||
} | ||
/** | ||
* Year validation rule. | ||
* @param errorMsg Error message. | ||
* @param start Start year boundary. | ||
* If null or undefined, there is no start boundary. | ||
* @param end End year boundary. | ||
* If null or undefined, there is no end boundary. | ||
* @return validation function, fn(value) => true|string, | ||
* returns true when valid, error message otherwise. | ||
*/ | ||
function year(errorMsg, start, end) { | ||
// No boundaries | ||
if (isNullOrUndefined(start) && isNullOrUndefined(end)) { | ||
console.warn('the year validation rule without start and end has ' + 'no validation effect, it will be always validated as true.'); | ||
return function () { | ||
return true; | ||
}; | ||
} | ||
var startBoundary = yearBoundary(start, BoundaryType.Start); | ||
var endBoundary = yearBoundary(end, BoundaryType.End); | ||
return function (value) { | ||
if (isNullOrUndefined(value)) { | ||
return errorMsg; | ||
} // Get the year | ||
if (isDate(value)) { | ||
value = value.getFullYear(); | ||
} else { | ||
value = parseInt(value, 10); | ||
} | ||
if (value < 0) { | ||
console.warn("the year validation rule, invalid \"".concat(value, "\" year value")); | ||
return errorMsg; | ||
} // Check the value boundaries | ||
if (startBoundary(value) === false || endBoundary(value) === false) { | ||
return errorMsg; | ||
} | ||
return true; | ||
}; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
/** | ||
* Equal validation rule. | ||
@@ -574,3 +479,2 @@ * @param errorMsg Error message. | ||
*/ | ||
function exclude(errorMsg, exclusions) { | ||
@@ -602,3 +506,2 @@ // No exclusions | ||
*/ | ||
function include(errorMsg, inclusions) { | ||
@@ -633,3 +536,2 @@ // No inclusions | ||
*/ | ||
function length(errorMsg, min, max) { | ||
@@ -677,3 +579,2 @@ var minLength = isNumber(min); | ||
*/ | ||
function pattern(errorMsg, pattern) { | ||
@@ -702,2 +603,30 @@ return function (value) { | ||
/** | ||
* Present validation rule. | ||
* @param errorMsg Error message. | ||
* @return validation function, fn(value) => true|string, | ||
* returns true when valid, error message otherwise. | ||
*/ | ||
function present(errorMsg) { | ||
return function (value) { | ||
if (isNullOrUndefined(value)) { | ||
return errorMsg; | ||
} | ||
if (isFunction(value) || isObject(value)) { | ||
throw new Error("present validation rule cannot validate \"".concat(value, "\"")); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.length > 0 ? true : errorMsg; | ||
} | ||
if ((value + '').trim() === '') { | ||
return errorMsg; | ||
} | ||
return true; | ||
}; | ||
} | ||
/** | ||
* Range validation rule. | ||
@@ -712,3 +641,2 @@ * @param errorMsg Error message. | ||
*/ | ||
function range(errorMsg, min, max) { | ||
@@ -752,4 +680,61 @@ var minBoundary = isNumber(min); | ||
/** | ||
* Present validation rule. | ||
* Create year boundary based on the boundary type. | ||
* @param filter Year filter. | ||
* @param dir Boundary direction, 0 start, 1 end. | ||
* @return Function accepting the relative year which should be used | ||
* in the comparison procedure. | ||
*/ | ||
function yearBoundary(filter, dir) { | ||
if (isNullOrUndefined(filter)) { | ||
return function () { | ||
return true; | ||
}; | ||
} | ||
var isStringValue = isString(filter); // Boundary offset in years | ||
var offset; // Dynamic offset | ||
var dynamicOffset; | ||
if (isStringValue) { | ||
dynamicOffset = filter.match(/^(-?[1-9]\d*)y$/); | ||
} | ||
if (dynamicOffset && dynamicOffset !== null) { | ||
offset = parseInt(dynamicOffset[1]); // or -1,1 (in past, in future) | ||
} else if (['-1', '1'].includes("".concat(filter))) { | ||
offset = Infinity * parseInt(filter); // Start 1 = tomorrow | ||
if (dir === BoundaryType.Start && offset > 0) { | ||
offset = 1; // 1 year in future, dynamic offset. | ||
} // End -1 = any date in past | ||
if (dir === BoundaryType.End && offset < 0) { | ||
offset = -1; // 1 year in past, dynamic offset. | ||
} // Current year | ||
} else if (filter === 0) { | ||
offset = 0; | ||
} | ||
return function (year) { | ||
var currentYear = today().getFullYear(); | ||
if (offset !== undefined) { | ||
return dir === BoundaryType.Start ? year >= currentYear + offset : year <= currentYear + offset; | ||
} | ||
return dir === BoundaryType.Start ? year >= filter : year <= filter; | ||
}; | ||
} | ||
/** | ||
* Year validation rule. | ||
* @param errorMsg Error message. | ||
* @param start Start year boundary. | ||
* If null or undefined, there is no start boundary. | ||
* @param end End year boundary. | ||
* If null or undefined, there is no end boundary. | ||
* @return validation function, fn(value) => true|string, | ||
@@ -759,18 +744,34 @@ * returns true when valid, error message otherwise. | ||
function present(errorMsg) { | ||
function year(errorMsg, start, end) { | ||
// No boundaries | ||
if (isNullOrUndefined(start) && isNullOrUndefined(end)) { | ||
console.warn('the year validation rule without start and end has ' + 'no validation effect, it will be always validated as true.'); | ||
return function () { | ||
return true; | ||
}; | ||
} | ||
var startBoundary = yearBoundary(start, BoundaryType.Start); | ||
var endBoundary = yearBoundary(end, BoundaryType.End); | ||
return function (value) { | ||
if (isNullOrUndefined(value)) { | ||
return errorMsg; | ||
} | ||
} // Get the year | ||
if (isFunction(value) || isObject(value)) { | ||
throw new Error("present validation rule cannot validate \"".concat(value, "\"")); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.length > 0 ? true : errorMsg; | ||
if (isDate(value)) { | ||
value = value.getFullYear(); | ||
} else { | ||
value = parseInt(value, 10); | ||
} | ||
if ((value + '').trim() === '') { | ||
if (value < 0) { | ||
console.warn("the year validation rule, invalid \"".concat(value, "\" year value")); | ||
return errorMsg; | ||
} // Check the value boundaries | ||
if (startBoundary(value) === false || endBoundary(value) === false) { | ||
return errorMsg; | ||
} | ||
@@ -777,0 +778,0 @@ |
{ | ||
"name": "@briza/wegood", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "Tiny validation library, so wegood with data.", | ||
@@ -23,5 +23,8 @@ "main": "lib/wegood.js", | ||
"build": "npm run build:types && npm run build:js", | ||
"docs": "typedoc src", | ||
"docs": "typedoc src && git checkout docs/.nojekyll", | ||
"test": "jest", | ||
"prepublishOnly": "npm run test && npm run build" | ||
"lint": "eslint --max-warnings 0 \"src/**/*.{ts,js}\"", | ||
"lint:fix": "eslint --max-warnings 0 \"src/**/*.{ts,js}\" --fix", | ||
"prepublishOnly": "npm run build", | ||
"check-licenses": "license-checker --summary --excludePrivatePackages --onlyAllow 'MIT;MIT OR X11;Apache-2.0;ISC;BSD-3-Clause;BSD-2-Clause;CC-BY-4.0;Public Domain;BSD;CC-BY-3.0;CC0-1.0;Unlicense'" | ||
}, | ||
@@ -41,28 +44,29 @@ "repository": { | ||
"devDependencies": { | ||
"@babel/core": "^7.6.4", | ||
"@babel/plugin-proposal-class-properties": "^7.5.5", | ||
"@babel/preset-env": "^7.6.3", | ||
"@babel/preset-typescript": "^7.6.0", | ||
"@types/jest": "^25.1.0", | ||
"@typescript-eslint/eslint-plugin": "^2.4.0", | ||
"@typescript-eslint/parser": "^2.4.0", | ||
"eslint": "^6.5.1", | ||
"eslint-config-standard": "^14.1.0", | ||
"eslint-import-resolver-typescript": "^2.0.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-node": "^11.0.0", | ||
"@babel/core": "^7.12.3", | ||
"@babel/plugin-proposal-class-properties": "^7.12.1", | ||
"@babel/preset-env": "^7.12.1", | ||
"@babel/preset-typescript": "^7.12.1", | ||
"@rollup/plugin-babel": "^5.2.1", | ||
"@rollup/plugin-commonjs": "^16.0.0", | ||
"@rollup/plugin-node-resolve": "^10.0.0", | ||
"@types/jest": "^26.0.15", | ||
"@typescript-eslint/eslint-plugin": "^4.6.1", | ||
"@typescript-eslint/parser": "^4.6.1", | ||
"eslint": "^7.12.1", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-import-resolver-typescript": "^2.3.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-simple-import-sort": "^5.0.2", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"jest": "^24.9.0", | ||
"rollup": "^2.3.3", | ||
"rollup-plugin-babel": "^4.3.3", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"eslint-plugin-standard": "^4.0.2", | ||
"jest": "^26.6.3", | ||
"license-checker": "^25.0.1", | ||
"rollup": "^2.33.1", | ||
"rollup-plugin-eslint": "^7.0.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"ts-jest": "^24.1.0", | ||
"typedoc": "^0.17.4", | ||
"typescript": "^3.8.3" | ||
"ts-jest": "^26.4.3", | ||
"typedoc": "^0.19.2", | ||
"typescript": "^4.0.5" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -446,2 +446,3 @@ # wegood | ||
| transform | Custom Date object transformer function. Optional. | ```(value) => new Date(value)``` | | ||
| todayDate | Today's date to be used for relative date boundaries. Optional, if not provided the start of the day in current environment timezone will be used | ```new Date('2020-10-10T00:00:00+03:00)``` | | ||
@@ -510,2 +511,5 @@ > [Code documentation](https://briza-insurance.github.io/wegood/modules/_rule_date_.html). | ||
// Passing today Date | ||
date('error message', 1, '60d', undefined, new Date('2020-11-11T00:00:00+03:00')) | ||
``` | ||
@@ -608,13 +612,5 @@ | ||
## Contributing | ||
When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change. | ||
See [contributing.md](https://github.com/briza-insurance/wegood/blob/master/contributing.md). | ||
### Pull Request Process | ||
1. Fork it | ||
2. Create your feature branch (git checkout -b ft/new-feature-name) | ||
3. Commit your changes (git commit -am 'Add some feature') | ||
4. Push to the branch (git push origin ft/new-feature-name) | ||
5. Create new Pull Request | ||
> Please make an issue first if the change is likely to increase. | ||
## License | ||
wegood is released under the MIT license. See [LICENSE.txt](https://github.com/briza-insurance/wegood/blob/master/LICENSE.txt). | ||
wegood is released under the MIT license. See [license.txt](https://github.com/briza-insurance/wegood/blob/master/license.txt). |
@@ -0,0 +0,0 @@ /** |
/** | ||
* Is Null or Undefined predicate. | ||
* @param value Rested value. | ||
* @return | ||
*/ | ||
export declare function isNullOrUndefined(value: any): boolean; | ||
export declare function isNullOrUndefined(value: unknown): value is null | undefined; | ||
/** | ||
* Is number predicate. | ||
* @param value Tested value. | ||
* @return | ||
*/ | ||
export declare function isNumber(value: any): value is number; | ||
export declare function isNumber(value: unknown): value is number; | ||
/** | ||
* Is string type predicate. | ||
* @param value | ||
* @return | ||
*/ | ||
export declare function isString(value: any): value is string; | ||
export declare function isString(value: unknown): value is string; | ||
/** | ||
* Is boolean type predicate. | ||
* @param value | ||
* @return | ||
*/ | ||
export declare function isBoolean(value: any): value is boolean; | ||
export declare function isBoolean(value: unknown): value is boolean; | ||
/** | ||
@@ -30,14 +26,12 @@ * Is function type predicate. | ||
*/ | ||
export declare function isFunction(value: any): boolean; | ||
export declare function isFunction(value: unknown): value is Function; | ||
/** | ||
* Is date type predicate. | ||
* @param value | ||
* @return | ||
*/ | ||
export declare function isDate(value: any): value is Date; | ||
export declare function isDate(value: unknown): value is Date; | ||
/** | ||
* Is Object | ||
* @param value tested value | ||
* @return result of the test | ||
*/ | ||
export declare function isObject(value: any): boolean; | ||
export declare function isObject(value: unknown): value is Record<string, unknown>; |
import { ValidationRule } from './rule'; | ||
import date from './rule/date'; | ||
import year from './rule/year'; | ||
import equal from './rule/equal'; | ||
@@ -9,4 +8,5 @@ import exclude from './rule/exclude'; | ||
import pattern from './rule/pattern'; | ||
import present from './rule/present'; | ||
import range from './rule/range'; | ||
import present from './rule/present'; | ||
import year from './rule/year'; | ||
/** | ||
@@ -51,3 +51,3 @@ * Built-in validation rules | ||
*/ | ||
validate(value: any, firstErrorOnly?: boolean): ValidationResult; | ||
validate(value: unknown, firstErrorOnly?: boolean): ValidationResult; | ||
/** | ||
@@ -58,3 +58,3 @@ * Validity predicate against the value. | ||
*/ | ||
valid(value: any): boolean; | ||
valid(value: unknown): boolean; | ||
/** | ||
@@ -68,4 +68,4 @@ * Get all validation errors, if any. | ||
*/ | ||
errors(value: any, firstErrorOnly?: boolean): string[]; | ||
errors(value: unknown, firstErrorOnly?: boolean): string[]; | ||
} | ||
export default Validator; |
@@ -24,6 +24,9 @@ import { ValidationRule } from '.'; | ||
* convert the testing value into date object. | ||
* @param todayDate Optional Today's date to be used as reference | ||
* validating ranges. If not provided, the start of the day in | ||
* the current runtime timezone will be used. | ||
* @return validation function, fn(value) => true|string, | ||
* returns true when valid, error message otherwise. | ||
*/ | ||
declare function date(errorMsg: string, start: Date | string | number | null | undefined, end: Date | string | number | null | undefined, transform?: (value: string) => Date): ValidationRule; | ||
declare function date(errorMsg: string, start: Date | string | number | null | undefined, end: Date | string | number | null | undefined, transform?: (value: string) => Date, todayDate?: Date): ValidationRule; | ||
export default date; |
@@ -0,0 +0,0 @@ import { ValidationRule } from '.'; |
@@ -0,0 +0,0 @@ import { ValidationRule } from '.'; |
@@ -0,0 +0,0 @@ import { ValidationRule } from '.'; |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ import { ValidationRule } from '.'; |
@@ -0,0 +0,0 @@ import { ValidationRule } from '.'; |
@@ -0,0 +0,0 @@ import { ValidationRule } from '.'; |
@@ -0,0 +0,0 @@ import { ValidationRule } from '.'; |
@@ -0,0 +0,0 @@ import { ValidationRule } from '.'; |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
76603
1777
25
613
1