Socket
Socket
Sign inDemoInstall

@briza/wegood

Package Overview
Dependencies
Maintainers
5
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@briza/wegood - npm Package Compare versions

Comparing version 1.0.10 to 1.0.11

24

changelog.md
# wegood changelog
## 1.0.11
- Update dependencies.
## 1.0.9
* Feature: add Today's date argument to date validator.
- Feature: add Today's date argument to date validator.
## 1.0.8
* Update: Include/Exclude rule supports boolean values.
- Update: Include/Exclude rule supports boolean values.
## 1.0.7
* Feature: added new validation rule "year".
- Feature: added new validation rule "year".
## 1.0.6
* Bug fix: re-instate timezone dependency, but local timezone offset instead.
- Bug fix: re-instate timezone dependency, but local timezone offset instead.
## 1.0.5
* Bug fix: remove timezone dependency.
- Bug fix: remove timezone dependency.
## 1.0.4
* Bug fix: future start, past end, expressed as 1 or -1.
- Bug fix: future start, past end, expressed as 1 or -1.
## 1.0.3
* Bug fix: changed date ISO format to YYYY-MM-DDThh:mm:ss+hh:mm, to support Safari.
- Bug fix: changed date ISO format to YYYY-MM-DDThh:mm:ss+hh:mm, to support Safari.
## 1.0.2
* Add support for isolated start and end dates.
- Add support for isolated start and end dates.
## 1.0.1
* Update documentation.
- Update documentation.
## 1.0.0
* Initial release.
- Initial release.
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);
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
}, _typeof(obj);
}

@@ -36,20 +30,8 @@

if (staticProps) _defineProperties(Constructor, staticProps);
Object.defineProperty(Constructor, "prototype", {
writable: false
});
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _unsupportedIterableToArray(o, minLen) {

@@ -772,4 +754,2 @@ if (!o) return;

_defineProperty(this, "_rules", void 0);
if (rules.length === 0) {

@@ -927,3 +907,2 @@ throw new Error('the validator must have validation rules');

export default Validator;
export { Validator, date, equal, exclude, include, length, pattern, present, range, year };
export { Validator, date, Validator as default, equal, exclude, include, length, pattern, present, range, year };

@@ -8,13 +8,7 @@ 'use strict';

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);
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
}, _typeof(obj);
}

@@ -41,20 +35,8 @@

if (staticProps) _defineProperties(Constructor, staticProps);
Object.defineProperty(Constructor, "prototype", {
writable: false
});
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _unsupportedIterableToArray(o, minLen) {

@@ -777,4 +759,2 @@ if (!o) return;

_defineProperty(this, "_rules", void 0);
if (rules.length === 0) {

@@ -934,3 +914,3 @@ throw new Error('the validator must have validation rules');

exports.date = date;
exports.default = Validator;
exports["default"] = Validator;
exports.equal = equal;

@@ -937,0 +917,0 @@ exports.exclude = exclude;

{
"name": "@briza/wegood",
"version": "1.0.10",
"version": "1.0.11",
"description": "Tiny validation library, so wegood with data.",

@@ -28,3 +28,3 @@ "main": "lib/wegood.js",

"prepublishOnly": "npm run test && 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\""
"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;Python-2.0;Unlicense\""
},

@@ -44,28 +44,28 @@ "repository": {

"devDependencies": {
"@babel/core": "^7.14.8",
"@babel/plugin-proposal-class-properties": "^7.14.5",
"@babel/preset-env": "^7.14.8",
"@babel/preset-typescript": "^7.14.5",
"@babel/core": "^7.16.12",
"@babel/plugin-proposal-class-properties": "^7.16.7",
"@babel/preset-env": "^7.16.11",
"@babel/preset-typescript": "^7.16.7",
"@rollup/plugin-babel": "^5.2.3",
"@rollup/plugin-commonjs": "^19.0.1",
"@rollup/plugin-node-resolve": "^13.0.2",
"@types/jest": "^26.0.24",
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.4",
"eslint": "^7.31.0",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.1.3",
"@types/jest": "^27.4.0",
"@typescript-eslint/eslint-plugin": "^5.10.2",
"@typescript-eslint/parser": "^5.10.2",
"eslint": "^8.8.0",
"eslint-config-standard": "^14.1.1",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-import": "^2.23.4",
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"eslint-plugin-standard": "^5.0.0",
"jest": "^27.0.6",
"jest": "^27.4.7",
"license-checker": "^25.0.1",
"rollup": "^2.53.3",
"rollup": "^2.66.1",
"rollup-plugin-eslint": "^7.0.0",
"ts-jest": "^27.0.4",
"typedoc": "^0.21.4",
"typescript": "^4.3.5"
"ts-jest": "^27.1.3",
"typedoc": "^0.22.11",
"typescript": "^4.5.5"
}
}
# wegood
Tiny validation library, so wegood with data.
> Revision: April 13, 2020.
> Revision: February 1, 2022.
## About
This project has been developed to provide a shared validation logic between front-end and back-end code, easily extend-able with custom rules.
* It is unit tested with 100% code coverage.
* Easily localized validation messages.
* Plug & play custom validation rules.
* Not dependent on any library.
* Readable and declarative validation rules.
- It is unit tested with 100% code coverage.
- Easily localized validation messages.
- Plug & play custom validation rules.
- Not dependent on any library.
- Readable and declarative validation rules.

@@ -20,5 +22,7 @@ > The library is being build as CommonJS module and ESM.

## Installation via NPM or Yarn
```sh
npm install -D @briza/wegood
```
```sh

@@ -61,12 +65,9 @@ yarn add @briza/wegood -D

## Basic Usage
```js
// Import the Validator.
import Validator from '@briza/wegood';
import Validator from "@briza/wegood";
// Import some of the predefined validation rules.
import {
equal,
length,
pattern,
} from '@briza/wegood';
import { equal, length, pattern } from "@briza/wegood";

@@ -78,10 +79,13 @@ /**

const ratingValidator = new Validator([
present('The rating is required.'),
equal('The value must be 5.', 5)
present("The rating is required."),
equal("The value must be 5.", 5),
]);
const idValidator = new Validator([
present('The ID is required.'),
length('The value must 9 characters long.', 9, 9),
pattern('The value must be in A000-0000 format, where 0 could be any number.', /^A\d{3}-\d{4}$/)
present("The ID is required."),
length("The value must 9 characters long.", 9, 9),
pattern(
"The value must be in A000-0000 format, where 0 could be any number.",
/^A\d{3}-\d{4}$/
),
]);

@@ -101,3 +105,3 @@

// Errors contains the error message of all not-satisfied rules.
idValidator.validate('a1234-4', false /* firstErrorOnly */);
idValidator.validate("a1234-4", false /* firstErrorOnly */);

@@ -108,19 +112,20 @@ // Return true if valid, false otherwise.

// Return array of error messages of all not-satisfied rules.
idValidator.errors('a1234-4');
idValidator.errors("a1234-4");
// Return array of error messages of all not-satisfied rules.
idValidator.errors('a1234-4');
idValidator.errors("a1234-4");
// Return only the first error message of the not-satisfied rule, still returned as an array.
idValidator.errors('a1234-4', true /* firstErrorOnly */);
idValidator.errors("a1234-4", true /* firstErrorOnly */);
```
### Validator Methods
```js
// Import the Validator.
import Validator from '@briza/wegood';
import { present } from '@briza/wegood';
import Validator from "@briza/wegood";
import { present } from "@briza/wegood";
// Create the validator.
const validator = new Validator([present('this field is required.')]);
const validator = new Validator([present("this field is required.")]);
```

@@ -131,2 +136,3 @@

#### Rules
Get the validator rules.

@@ -137,3 +143,3 @@

```js
validator.rules
validator.rules;
// validation rules (function[])

@@ -143,13 +149,16 @@ ```

#### Validate
Validate against the tested value, it returns the [Validation Result](#validation-result).
##### Validation Result
The Validation Result is a object with these properties:
| Property | Type | Note | Example |
| --- | --- | --- | --- |
| valid | boolean | Validity state. | ```true``` |
| errors | string[] | Collection of captured validation errors. | ```['this field is required']``` |
| Property | Type | Note | Example |
| -------- | -------- | ----------------------------------------- | ---------------------------- |
| valid | boolean | Validity state. | `true` |
| errors | string[] | Collection of captured validation errors. | `['this field is required']` |
**Example**
```js

@@ -176,3 +185,3 @@ // Valid

```js
validator.validate(testedValue)
validator.validate(testedValue);
/**

@@ -183,8 +192,8 @@ * {

* }
*
*
* The errors will contain only first discovered error.
*
*
*/
validator.validate(testedValue, false)
validator.validate(testedValue, false);
/**

@@ -195,5 +204,5 @@ * {

* }
*
*
* The errors will contain all discovered error.
*
*
*/

@@ -203,2 +212,3 @@ ```

#### Valid
Validity predicate against the tested value.

@@ -209,3 +219,3 @@

```js
validator.valid(testedValue)
validator.valid(testedValue);
// true | false

@@ -215,2 +225,3 @@ ```

#### Errors
Get the validation errors. if any.

@@ -223,7 +234,7 @@

```js
validator.errors(testedValue)
validator.errors(testedValue);
// string[]
// All errors will be captured.
validator.errors(testedValue, true)
validator.errors(testedValue, true);
// string[]

@@ -234,7 +245,14 @@ // Only the first error will be captured.

## Builtin Validation Rules
All builtin validation rules have this construct:
```typescript
function rule(errorMessage: string, agr1: any, ... argN: any): (value: any) => true|string
function rule(
errorMessage: string,
agr1: any,
...argN: any
): (value: any) => true | string;
```
- A function which returns the validation rule function with set error message, plug-able into the Validator, or it could be used individually, e.g. ```rule('error message')(testValue)```.
- A function which returns the validation rule function with set error message, plug-able into the Validator, or it could be used individually, e.g. `rule('error message')(testValue)`.
- The args differs form rule to rule.

@@ -244,14 +262,17 @@ - Please see [Custom Validation Rule](#custom-validation-rule) for more information.

### Present
Verify that the tested value is present, i.e. defined and not empty.
```js
import { present } from '@briza/wegood';
import { present } from "@briza/wegood";
```
**Function Arguments**
```typescript
present(errorMessage)
present(errorMessage);
```
| Argument | Notes | Example |
| --- | --- | --- |
| Argument | Notes | Example |
| ------------ | -------------- | --------------------- |
| errorMessage | Error message. | 'the value must be 5' |

@@ -261,28 +282,32 @@

* Non-empty array tested value is evaluated as valid.
* Empty string tested value is evaluated as invalid.
* Object, Function tested value throws an error.
- Non-empty array tested value is evaluated as valid.
- Empty string tested value is evaluated as invalid.
- Object, Function tested value throws an error.
**Example**
```typescript
// The value must have some value.
present('error message');
present("error message");
```
### Equal
Verify that the tested value is equal to another.
```js
import { equal } from '@briza/wegood';
import { equal } from "@briza/wegood";
```
**Function Arguments**
```typescript
equal(errorMessage, value|customMatcher)
equal(errorMessage, value | customMatcher);
```
| Argument | Notes | Example |
| --- | --- | --- |
| errorMessage | Error message. | 'the value must be 5' |
| value | The equal value. | 5 |
| customMatcher | Custom equality predicate function. | ```(value) => value === 5``` |
| Argument | Notes | Example |
| ------------- | ----------------------------------- | ------------------------ |
| errorMessage | Error message. | 'the value must be 5' |
| value | The equal value. | 5 |
| customMatcher | Custom equality predicate function. | `(value) => value === 5` |

@@ -292,8 +317,9 @@ > [Code documentation](https://briza-insurance.github.io/wegood/modules/_rule_equal_.html).

**Example**
```typescript
// The value must be equal to 5.
equal('error message', 5);
equal("error message", 5);
// Custom matcher.
equal('error message', (value) => {
equal("error message", (value) => {
return value === 5;

@@ -304,16 +330,19 @@ });

### Pattern
Verify that the tested value is matches the pattern.
```js
import { pattern } from '@briza/wegood';
import { pattern } from "@briza/wegood";
```
**Function Arguments**
```typescript
pattern(errorMessage, pattern)
pattern(errorMessage, pattern);
```
| Argument | Notes | Example |
| --- | --- | --- |
| errorMessage | Error message. | 'invalid email format' |
| pattern | Regular expression used to validate the value. | ```/^[^@]+@.*\.[a-z]{2, 5}$/``` |
| Argument | Notes | Example |
| ------------ | ---------------------------------------------- | --------------------------- |
| errorMessage | Error message. | 'invalid email format' |
| pattern | Regular expression used to validate the value. | `/^[^@]+@.*\.[a-z]{2, 5}$/` |

@@ -323,25 +352,30 @@ > [Code documentation](https://briza-insurance.github.io/wegood/modules/_rule_pattern_.html).

**Example**
```typescript
// The value must match the given pattern.
pattern('error message', /^[^@]+@.*\.[a-z]{2, 5}$/);
pattern("error message", /^[^@]+@.*\.[a-z]{2, 5}$/);
```
### Range
Verify that the tested value is in the given range (inclusive).
- Applicable only on the number values, although the rule auto-converts string into number if it is a valid number.
- Could be also used as MAX and MIN.
```js
import { range } from '@briza/wegood';
import { range } from "@briza/wegood";
```
**Function Arguments**
```typescript
range(errorMessage, min, max)
range(errorMessage, min, max);
```
| Argument | Notes | Example |
| --- | --- | --- |
| errorMessage | Error message. | 'the value must be in range 3-10' |
| min | Minimal boundary. If set to ```undefined``` or ```null```, it is being ignored. | 3 |
| max | Maximal boundary. If set to ```undefined``` or ```null```, it is being ignored. | 10 |
| Argument | Notes | Example |
| ------------ | ----------------------------------------------------------------------- | --------------------------------- |
| errorMessage | Error message. | 'the value must be in range 3-10' |
| min | Minimal boundary. If set to `undefined` or `null`, it is being ignored. | 3 |
| max | Maximal boundary. If set to `undefined` or `null`, it is being ignored. | 10 |

@@ -351,15 +385,18 @@ > [Code documentation](https://briza-insurance.github.io/wegood/modules/_rule_range_.html).

**Example**
```typescript
// The value must be between 3 and 10.
range('error message', 3, 10);
range("error message", 3, 10);
// The value must be 3 and above, aka MIN.
range('error message', 3);
range("error message", 3);
// The value must be 10 and bellow, aka MAX.
range('error message', null|undefined, 10);
range("error message", null | undefined, 10);
```
### Length
Verify that the tested value is in the given string length range (inclusive).
- Applicable only on the string values, although the rule auto-converts numbers into strings.

@@ -369,15 +406,16 @@ - Could be also used as MAX and MIN.

```js
import { length } from '@briza/wegood';
import { length } from "@briza/wegood";
```
**Function Arguments**
```typescript
length(errorMessage, min, max)
length(errorMessage, min, max);
```
| Argument | Notes | Example |
| --- | --- | --- |
| errorMessage | Error message. | 'the value must be 3-10 characters long' |
| min | Minimal length. If set to ```undefined``` or ```null```, it is being ignored. | 3 |
| max | Maximal length. If set to ```undefined``` or ```null```, it is being ignored. | 10 |
| Argument | Notes | Example |
| ------------ | --------------------------------------------------------------------- | ---------------------------------------- |
| errorMessage | Error message. | 'the value must be 3-10 characters long' |
| min | Minimal length. If set to `undefined` or `null`, it is being ignored. | 3 |
| max | Maximal length. If set to `undefined` or `null`, it is being ignored. | 10 |

@@ -387,28 +425,32 @@ > [Code documentation](https://briza-insurance.github.io/wegood/modules/_rule_length_.html).

**Example**
```typescript
// The value must be 3 up to 10 characters long.
length('error message', 3, 10);
length("error message", 3, 10);
// The value must be 3 and more characters long, aka MIN.
length('error message', 3);
length("error message", 3);
// The value must be 10 and less characters long, aka MAX.
length('error message', null|undefined, 10);
length("error message", null | undefined, 10);
```
### Exclude
Verify that the tested value is not the exclusion list.
```js
import { exclude } from '@briza/wegood';
import { exclude } from "@briza/wegood";
```
**Function Arguments**
```typescript
exclude(errorMessage, exclusions)
exclude(errorMessage, exclusions);
```
| Argument | Notes | Example |
| --- | --- | --- |
| errorMessage | Error message. | 'invalid value' |
| exclusions | Array of exclusions. | ```[1,2,3]``` |
| Argument | Notes | Example |
| ------------ | -------------------- | --------------- |
| errorMessage | Error message. | 'invalid value' |
| exclusions | Array of exclusions. | `[1,2,3]` |

@@ -418,25 +460,29 @@ > [Code documentation](https://briza-insurance.github.io/wegood/modules/_rule_exclude_.html).

**Example**
```typescript
// The value must not be contained in the list.
exclude('error message', [1, 2, 3]);
exclude("error message", [1, 2, 3]);
// The value must not be contained in the list.
exclude('error message', ['circle', 'square', 'triable']);
exclude("error message", ["circle", "square", "triable"]);
```
### Include
Verify that the tested value is not the exclusion list.
```js
import { include } from '@briza/wegood';
import { include } from "@briza/wegood";
```
**Function Arguments**
```typescript
include(errorMessage, inclusions)
include(errorMessage, inclusions);
```
| Argument | Notes | Example |
| --- | --- | --- |
| errorMessage | Error message. | 'invalid value' |
| inclusions | Array of inclusions. | ```[1,2,3]``` |
| Argument | Notes | Example |
| ------------ | -------------------- | --------------- |
| errorMessage | Error message. | 'invalid value' |
| inclusions | Array of inclusions. | `[1,2,3]` |

@@ -446,28 +492,32 @@ > [Code documentation](https://briza-insurance.github.io/wegood/modules/_rule_include_.html).

**Example**
```typescript
// The value must be contained in the list.
include('error message', [1, 2, 3]);
include("error message", [1, 2, 3]);
// The value must be contained in the list.
include('error message', ['circle', 'square', 'triable']);
include("error message", ["circle", "square", "triable"]);
```
### Date
Verify that the tested value is the date range.
```js
import { date } from '@briza/wegood';
import { date } from "@briza/wegood";
```
**Function Arguments**
```typescript
date(errorMessage, start, end, transform)
date(errorMessage, start, end, transform);
```
| Argument | Notes | Example |
| --- | --- | --- |
| errorMessage | Error message. | 'the date in not in valid range.' |
| start | Start date boundary: ```ISO date string``` (yyyy-mm-dd), ```Date object```, or [Relative Date Offset](#relative-date-offset). If set to ```undefined``` or ```null```, it is being ignored. | 2020-03-16 |
| end | End date boundary: ```ISO date string``` (yyyy-mm-dd), ```Date object```, or [Relative Date Offset](#relative-date-offset). If set to ```undefined``` or ```null```, it is being ignored. | 3y |
| 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)``` |
| Argument | Notes | Example |
| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------- |
| errorMessage | Error message. | 'the date in not in valid range.' |
| start | Start date boundary: `ISO date string` (yyyy-mm-dd), `Date object`, or [Relative Date Offset](#relative-date-offset). If set to `undefined` or `null`, it is being ignored. | 2020-03-16 |
| end | End date boundary: `ISO date string` (yyyy-mm-dd), `Date object`, or [Relative Date Offset](#relative-date-offset). If set to `undefined` or `null`, it is being ignored. | 3y |
| 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)` |

@@ -477,19 +527,21 @@ > [Code documentation](https://briza-insurance.github.io/wegood/modules/_rule_date_.html).

#### Relative Date Offset
Instead of a concrete ISO date string or Date object below annotated shortcodes may be used to set the date boundary relative to TODAY date.
| Annotation | Meaning | Example |
| -- | -- | -- |
| 0 | Today. | 0 |
| -1 | In past. | -1 |
| 1 | In future. | 1 |
| -Nd | N days in past, relative from today. | -10d |
| Nd | N days in past, relative from today. | 10d |
| -Nw | N weeks in past, relative from today. | -2w |
| Nw | N weeks in past, relative from today. | 2w |
| -Nm | N months in past, relative from today. | -6m |
| Nm | N months in past, relative from today. | 6m |
| -Ny | N years in past, relative from today. | -2y |
| Ny | N years in past, relative from today. | 2y |
| Annotation | Meaning | Example |
| ---------- | -------------------------------------- | ------- |
| 0 | Today. | 0 |
| -1 | In past. | -1 |
| 1 | In future. | 1 |
| -Nd | N days in past, relative from today. | -10d |
| Nd | N days in past, relative from today. | 10d |
| -Nw | N weeks in past, relative from today. | -2w |
| Nw | N weeks in past, relative from today. | 2w |
| -Nm | N months in past, relative from today. | -6m |
| Nm | N months in past, relative from today. | 6m |
| -Ny | N years in past, relative from today. | -2y |
| Ny | N years in past, relative from today. | 2y |
**Example**
```typescript

@@ -500,5 +552,9 @@ /**

// ISO strings
date('error message', '2000-12-30', '2020-06-29');
date("error message", "2000-12-30", "2020-06-29");
// Date objects
date('error message', new Date('2000-12-30T00:00:00+00:00'), new Date('2020-06-29T00:00:00+00:00'));
date(
"error message",
new Date("2000-12-30T00:00:00+00:00"),
new Date("2020-06-29T00:00:00+00:00")
);

@@ -509,5 +565,5 @@ /**

// ISO string
date('error message', '2000-12-30');
date("error message", "2000-12-30");
// Date object
date('error message', new Date('2000-12-30T00:00:00+00:00'))
date("error message", new Date("2000-12-30T00:00:00+00:00"));

@@ -518,5 +574,5 @@ /**

// ISO string
date('error message', undefined|null, '2020-06-29');
date("error message", undefined | null, "2020-06-29");
// Date object
date('error message', undefined|null, new Date('2020-06-29T00:00:00+00:00'))
date("error message", undefined | null, new Date("2020-06-29T00:00:00+00:00"));

@@ -528,42 +584,52 @@ /**

// Any date in past, until today.
date('error message', -1, 0);
date("error message", -1, 0);
// Any date in future, starts from today.
date('error message', 0, 1);
date("error message", 0, 1);
// Any date between 2 years ago, up to 3 months from today.
date('error message', '-2y', '3m');
date("error message", "-2y", "3m");
// Combined fixed date with relative date.
// Any date from 2000-12-30 until today.
date('error message', '2000-12-30', 0);
date("error message", "2000-12-30", 0);
// Passing today Date
date('error message', 1, '60d', undefined, new Date('2020-11-11T00:00:00+03:00'))
date(
"error message",
1,
"60d",
undefined,
new Date("2020-11-11T00:00:00+03:00")
);
```
#### Date Tested Value Format
The tested value must be passed to the validation rule in the ```ISO date string``` (yyyy-mm-dd) or as a ```Date object```, otherwise the validation rule throws an error. The **transform** function could be passed to the rule to handle custom conversion from mixed value into the Date object.
The tested value must be passed to the validation rule in the `ISO date string` (yyyy-mm-dd) or as a `Date object`, otherwise the validation rule throws an error. The **transform** function could be passed to the rule to handle custom conversion from mixed value into the Date object.
```typescript
// Custom transform function
date('error message', '2000-12-30', '2020-06-29', (value) => new Date(value));
date("error message", "2000-12-30", "2020-06-29", (value) => new Date(value));
```
### Year
Verify that the tested value is in the year range.
```js
import { year } from '@briza/wegood';
import { year } from "@briza/wegood";
```
**Function Arguments**
```typescript
year(errorMessage, start, end)
year(errorMessage, start, end);
```
| Argument | Notes | Example |
| --- | --- | --- |
| errorMessage | Error message. | 'the year in not in valid range.' |
| start | Start date boundary: ```4-digit year number``` or [Relative Year Offset](#relative-year-offset). If set to ```undefined``` or ```null```, it is being ignored. | 2000 |
| end | End date boundary: ```4-digit year number``` or [Relative Year Offset](#relative-year-offset). If set to ```undefined``` or ```null```, it is being ignored. | 3y |
| Argument | Notes | Example |
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- |
| errorMessage | Error message. | 'the year in not in valid range.' |
| start | Start date boundary: `4-digit year number` or [Relative Year Offset](#relative-year-offset). If set to `undefined` or `null`, it is being ignored. | 2000 |
| end | End date boundary: `4-digit year number` or [Relative Year Offset](#relative-year-offset). If set to `undefined` or `null`, it is being ignored. | 3y |

@@ -573,22 +639,24 @@ > [Code documentation](https://briza-insurance.github.io/wegood/modules/_rule_year_.html).

#### Relative Year Offset
Instead of a concrete year below annotated shortcodes may be used to set the year boundary relative to the CURRENT year.
| Annotation | Meaning | Example |
| -- | -- | -- |
| 0 | Current year. | 0 |
| -1 | In past. | -1 |
| 1 | In future. | 1 |
| -Ny | N years in past, relative from current year. | -2y |
| Ny | N years in past, relative from current year. | 2y |
| Annotation | Meaning | Example |
| ---------- | -------------------------------------------- | ------- |
| 0 | Current year. | 0 |
| -1 | In past. | -1 |
| 1 | In future. | 1 |
| -Ny | N years in past, relative from current year. | -2y |
| Ny | N years in past, relative from current year. | 2y |
**Example**
```typescript
// The value (year) must between the given years range.
year('error message', 2000, 2020);
year("error message", 2000, 2020);
// The value (year) must after the year (inclusive).
year('error message', 2000);
year("error message", 2000);
// The value (year) must before the year (inclusive).
year('error message', undefined|null, 2020);
year("error message", undefined | null, 2020);

@@ -600,22 +668,23 @@ /**

// Any year in past, until current year.
year('error message', -1, 0);
year("error message", -1, 0);
// Any year in future, starts from current year.
year('error message', 0, 1);
year("error message", 0, 1);
// Any year between 2 years ago, up to 2 years from current year.
year('error message', '-2y', '2y');
year("error message", "-2y", "2y");
// Combined fixed year with relative year.
// Any year from 2000 until current year.
year('error message', 2000, 0);
year("error message", 2000, 0);
```
#### Year Tested Value Format
The tested value must be passed to the validation as non zero number (string or number), otherwise the validation will fail implicitly with warning message.
## Custom Validation Rule
## Custom Validation Rule
Validation rule blueprint (typescript):
```typescript

@@ -636,3 +705,3 @@ function email(errorMsg: string): ValidationRule {

- ValidationRule (typescript type), represents: ```(value) => true|string```, where:
- ValidationRule (typescript type), represents: `(value) => true|string`, where:
- true = valid.

@@ -644,5 +713,7 @@ - string = error message.

## Contributing
See [contributing.md](https://github.com/briza-insurance/wegood/blob/master/contributing.md).
## 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 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ import { ValidationRule } from './rule';

@@ -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 @@ /**

@@ -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 '.';

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc