Skip to content

Commit

Permalink
Added date-fns library
Browse files Browse the repository at this point in the history
  • Loading branch information
ozziest committed Sep 27, 2024
1 parent e1405a2 commit 5841bbc
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 115 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
coverage
.DS_Store
.DS_Store
cache
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes

## [2.0.0 (2024-09-27)](https://github.com/axe-api/axe-api/compare/2.0.0...1.1.1)

- Fixed bundling issues
- The [dayjs](https://day.js.org) library with [date-fns](https://date-fns.org). You _MUST_ replace the date formats:
- `YYYY-MM-DD` => `yyyy-MM-dd`

## [1.1.1 (2024-09-19)](https://github.com/axe-api/axe-api/compare/1.1.1...1.1.0)

- Security fix
Expand Down
14 changes: 9 additions & 5 deletions demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@babel/parser": "^7.23.6",
"dayjs": "^1.11.10",
"date-fns": "^4.1.0",
"prettier": "^3.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
10 changes: 5 additions & 5 deletions demo/src/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import dayjs from "dayjs";
import { format } from "date-fns";

export const RULE_PARAMETER_MAP: Record<string, any[]> = {
string: [],
boolean: [],
accepted: [],
after: [dayjs().format("YYYY-MM-DD")],
after_or_equal: [dayjs().format("YYYY-MM-DD")],
after: [format(new Date(), "yyyy-MM-dd")],
after_or_equal: [format(new Date(), "yyyy-MM-dd")],
alpha: [],
alpha_dash: [],
alpha_num: [],
array: [],
before: [dayjs().format("YYYY-MM-DD")],
before_or_equal: [dayjs().format("YYYY-MM-DD")],
before: [format(new Date(), "yyyy-MM-dd")],
before_or_equal: [format(new Date(), "yyyy-MM-dd")],
between: [5, 10],
confirmed: [],
date: [],
Expand Down
6 changes: 3 additions & 3 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ setLocales(en);
setOptions({
stopOnFail: true,
language: "en",
dateFormat: "YYYY-MM-DD",
dateFormat: "yyyy-MM-dd",
});
```

Expand All @@ -30,7 +30,7 @@ setLocales(en);
setOptions({
stopOnFail: true,
language: "en",
dateFormat: "YYYY-MM-DD",
dateFormat: "yyyy-MM-dd",
});

await validate(
Expand All @@ -40,7 +40,7 @@ await validate(
{
stopOnFail: false,
language: "de",
dateFormat: "YYYY-MM-DD",
dateFormat: "yyyy-MM-dd",
},
);
```
70 changes: 26 additions & 44 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,9 @@ isAfter("your-value", "2023-01-01");
:::

:::tip
robust-validator library uses the [dayjs](https://day.js.org/) for the date validations.
robust-validator library uses the [date-fns](https://date-fns.org) for the date validations.

You can check the possible date formats [here](https://day.js.org/docs/en/parse/string-format).
:::

:::warning
You MUST install the [dayjs](https://day.js.org/) is to your project.

`npm install dayjs` or `yarn add dayjs`
You can check the possible date formats [here](https://date-fns.org/v4.1.0/docs/format).
:::

| Rule | Value | `startAt` | Is valid? |
Expand Down Expand Up @@ -128,15 +122,9 @@ isAfterOrEqual("your-value", "2023-01-01");
:::

:::tip
robust-validator library uses the [dayjs](https://day.js.org/) for the date validations.

You can check the possible date formats [here](https://day.js.org/docs/en/parse/string-format).
:::

:::warning
You MUST install the [dayjs](https://day.js.org/) is to your project.
robust-validator library uses the [date-fns](https://date-fns.org) for the date validations.

`npm install dayjs` or `yarn add dayjs`
You can check the possible date formats [here](https://date-fns.org/v4.1.0/docs/format).
:::

| Rule | Value | `startAt` | Is valid? |
Expand Down Expand Up @@ -329,15 +317,9 @@ isBefore("your-value", "2023-01-01");
:::

:::tip
robust-validator library uses the [dayjs](https://day.js.org/) for the date validations.

You can check the possible date formats [here](https://day.js.org/docs/en/parse/string-format).
:::
robust-validator library uses the [date-fns](https://date-fns.org) for the date validations.

:::warning
You MUST install the [dayjs](https://day.js.org/) is to your project.

`npm install dayjs` or `yarn add dayjs`
You can check the possible date formats [here](https://date-fns.org/v4.1.0/docs/format).
:::

| Rule | Value | `finishAt` | Is valid? |
Expand Down Expand Up @@ -378,15 +360,9 @@ isBeforeOrEqual("your-value", "2023-01-01");
The field under validation must be before or equal to the given date.

:::tip
robust-validator library uses the [dayjs](https://day.js.org/) for the date validations.

You can check the possible date formats [here](https://day.js.org/docs/en/parse/string-format).
:::

:::warning
You MUST install the [dayjs](https://day.js.org/) is to your project.
robust-validator library uses the [date-fns](https://date-fns.org) for the date validations.

`npm install dayjs` or `yarn add dayjs`
You can check the possible date formats [here](https://date-fns.org/v4.1.0/docs/format).
:::

| Rule | Value | `finishAt` | Is valid? |
Expand Down Expand Up @@ -516,39 +492,45 @@ isConfirmed("your-data");

The field under validation must be a valid date format which is acceptable by Javascript's Date object.

:::tip
robust-validator library uses the [date-fns](https://date-fns.org) for the date validations.

You can check the possible date formats [here](https://date-fns.org/v4.1.0/docs/format).
:::

::: code-group

```ts [Declarative]
import { validate } from "robust-validator";
// ...
await validate(data, { startAt: "date:YYYY-MM-DD" });
await validate(data, { startAt: "date:yyyy-MM-dd" });
```

```ts [Function-based]
import { validate, date } from "robust-validator";
// ...
await validate(data, { startAt: [date("YYYY-MM-DD")] });
await validate(data, { startAt: [date("yyyy-MM-dd")] });
```

```ts [Direct usage]
import { isDate } from "robust-validator";
// ...
isDate("your-data", "YYYY-MM-DD");
isDate("your-data", "yyyy-MM-dd");
```

:::

| Rule | Value | Is valid? |
| ----------------- | ---------------------------- | --------- |
| `date:YYYY-MM-DD` | `null` | 🔴 |
| `date:YYYY-MM-DD` | `undefined` | 🔴 |
| `date:YYYY-MM-DD` | `2023-12-16` | 🟢 |
| `date:YYYY-MM-DD` | `2023-01-01` | 🟢 |
| `date:YYYY-MM-DD` | `December 16, 2023 12:00:00` | 🔴 |
| `date:YYYY-MM-DD` | `2022-13-01` | 🔴 |
| `date:YYYY-MM-DD` | `2022-12-32` | 🔴 |
| `date:YYYY-MM-DD` | `2022-02-29` | 🔴 |
| `date:YYYY-MM-DD` | `false` | 🔴 |
| `date:yyyy-MM-dd` | `null` | 🔴 |
| `date:yyyy-MM-dd` | `undefined` | 🔴 |
| `date:yyyy-MM-dd` | `2023-12-16` | 🟢 |
| `date:yyyy-MM-dd` | `2023-01-01` | 🟢 |
| `date:yyyy-MM-dd` | `December 16, 2023 12:00:00` | 🔴 |
| `date:yyyy-MM-dd` | `2022-13-01` | 🔴 |
| `date:yyyy-MM-dd` | `2022-12-32` | 🔴 |
| `date:yyyy-MM-dd` | `2022-02-29` | 🔴 |
| `date:yyyy-MM-dd` | `false` | 🔴 |

## `digits:value`

Expand Down
23 changes: 13 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "robust-validator",
"version": "2.0.0-rc-2",
"version": "2.0.0",
"description": "Rule-based data validation library",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"types": "./dist/index.d.ts"
}
Expand Down Expand Up @@ -59,7 +59,6 @@
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"@vitest/coverage-v8": "^1.1.0",
"dayjs": "^1.11.10",
"eslint": "^8.56.0",
"eslint-config-standard-with-typescript": "^43.0.0",
"eslint-plugin-import": "^2.29.1",
Expand All @@ -81,7 +80,7 @@
"typescript": "^5.3.3",
"vitest": "^1.1.0"
},
"peerDependencies": {
"dayjs": "^1.11.10"
"dependencies": {
"date-fns": "^4.1.0"
}
}
2 changes: 1 addition & 1 deletion src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IOptions } from "./Interface";
const DEFAULT_OPTIONS: IOptions = {
stopOnFail: false,
language: "en",
dateFormat: "YYYY-MM-DD",
dateFormat: "yyyy-MM-dd",
};

let OPTIONS: IOptions = {
Expand Down
2 changes: 1 addition & 1 deletion src/converters/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* import { date } from "robust-validator"
*
* const definition = {
* value: [date("YYYY-MM-DD")]
* value: [date("yyyy-MM-dd")]
* };
* @type {string}
* @tutorial https://validator.axe-api.com/rules.html#date-format
Expand Down
7 changes: 3 additions & 4 deletions src/rules/isDate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat.js";
import { parse } from "date-fns";
import { getOptions } from "../Options";
dayjs.extend(customParseFormat);

export default (value: any, dateFormat?: string): boolean => {
const options = getOptions();
const format = dateFormat ?? options.dateFormat;
return dayjs(value, format, true).isValid();
const date: any = parse(value, format, new Date());
return !isNaN(date);
};
Loading

0 comments on commit 5841bbc

Please sign in to comment.