From 5841bbc4843beb57ac008e35171d01f9f324376c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Adem=20I=C5=9EIKLI?= Date: Fri, 27 Sep 2024 23:12:39 +0200 Subject: [PATCH] Added date-fns library --- .gitignore | 3 +- CHANGELOG.md | 6 ++ demo/package-lock.json | 14 +++-- demo/package.json | 2 +- demo/src/Constants.ts | 10 ++-- docs/options.md | 6 +- docs/rules.md | 70 ++++++++-------------- package-lock.json | 23 +++---- package.json | 15 +++-- src/Options.ts | 2 +- src/converters/date.ts | 2 +- src/rules/isDate.ts | 7 +-- tests/consumers/cjs/package-lock.json | 11 ++-- tests/consumers/esm/package-lock.json | 17 ++---- tests/consumers/esm/package.json | 1 - tests/consumers/ts-local/package-lock.json | 11 ++-- tests/rules/date.test.ts | 14 ++--- 17 files changed, 99 insertions(+), 115 deletions(-) diff --git a/.gitignore b/.gitignore index 8a771b4..bf34382 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules dist coverage -.DS_Store \ No newline at end of file +.DS_Store +cache \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dcf128..d847fc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/demo/package-lock.json b/demo/package-lock.json index e3608ed..430c52c 100644 --- a/demo/package-lock.json +++ b/demo/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.0", "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", @@ -1806,10 +1806,14 @@ "integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==", "dev": true }, - "node_modules/dayjs": { - "version": "1.11.10", - "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.10.tgz", - "integrity": "sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ==" + "node_modules/date-fns": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-4.1.0.tgz", + "integrity": "sha512-Ukq0owbQXxa/U3EGtsdVBkR1w7KOQ5gIBqdH2hkvknzZPYvBxb/aa6E8L7tmjFtkwZBu3UXBbjIgPo/Ez4xaNg==", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/kossnocorp" + } }, "node_modules/debug": { "version": "4.3.4", diff --git a/demo/package.json b/demo/package.json index 6b6c89f..4aa311e 100644 --- a/demo/package.json +++ b/demo/package.json @@ -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", diff --git a/demo/src/Constants.ts b/demo/src/Constants.ts index 5576cc1..9d76a51 100644 --- a/demo/src/Constants.ts +++ b/demo/src/Constants.ts @@ -1,17 +1,17 @@ -import dayjs from "dayjs"; +import { format } from "date-fns"; export const RULE_PARAMETER_MAP: Record = { 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: [], diff --git a/docs/options.md b/docs/options.md index a5d2c40..f875c8d 100644 --- a/docs/options.md +++ b/docs/options.md @@ -13,7 +13,7 @@ setLocales(en); setOptions({ stopOnFail: true, language: "en", - dateFormat: "YYYY-MM-DD", + dateFormat: "yyyy-MM-dd", }); ``` @@ -30,7 +30,7 @@ setLocales(en); setOptions({ stopOnFail: true, language: "en", - dateFormat: "YYYY-MM-DD", + dateFormat: "yyyy-MM-dd", }); await validate( @@ -40,7 +40,7 @@ await validate( { stopOnFail: false, language: "de", - dateFormat: "YYYY-MM-DD", + dateFormat: "yyyy-MM-dd", }, ); ``` diff --git a/docs/rules.md b/docs/rules.md index 6fecf05..abf3265 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -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? | @@ -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? | @@ -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? | @@ -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? | @@ -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` diff --git a/package-lock.json b/package-lock.json index 191f7e7..4e9eb00 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,16 @@ { "name": "robust-validator", - "version": "2.0.0-rc-2", + "version": "2.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "robust-validator", - "version": "2.0.0-rc-2", + "version": "2.0.0", "license": "MIT", + "dependencies": { + "date-fns": "^4.1.0" + }, "devDependencies": { "@babel/preset-env": "^7.23.6", "@babel/preset-typescript": "^7.23.3", @@ -17,7 +20,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", @@ -41,9 +43,6 @@ }, "engines": { "node": ">=18.0.0" - }, - "peerDependencies": { - "dayjs": "^1.11.10" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -5577,10 +5576,14 @@ "node": ">= 8" } }, - "node_modules/dayjs": { - "version": "1.11.10", - "dev": true, - "license": "MIT" + "node_modules/date-fns": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-4.1.0.tgz", + "integrity": "sha512-Ukq0owbQXxa/U3EGtsdVBkR1w7KOQ5gIBqdH2hkvknzZPYvBxb/aa6E8L7tmjFtkwZBu3UXBbjIgPo/Ez4xaNg==", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/kossnocorp" + } }, "node_modules/debug": { "version": "4.3.4", diff --git a/package.json b/package.json index df94dfb..c0a2ec5 100644 --- a/package.json +++ b/package.json @@ -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" } @@ -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", @@ -81,7 +80,7 @@ "typescript": "^5.3.3", "vitest": "^1.1.0" }, - "peerDependencies": { - "dayjs": "^1.11.10" + "dependencies": { + "date-fns": "^4.1.0" } } diff --git a/src/Options.ts b/src/Options.ts index a2cf43e..d3597dd 100644 --- a/src/Options.ts +++ b/src/Options.ts @@ -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 = { diff --git a/src/converters/date.ts b/src/converters/date.ts index d634704..dd0c52f 100644 --- a/src/converters/date.ts +++ b/src/converters/date.ts @@ -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 diff --git a/src/rules/isDate.ts b/src/rules/isDate.ts index 9048dea..07f1cfd 100644 --- a/src/rules/isDate.ts +++ b/src/rules/isDate.ts @@ -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); }; diff --git a/tests/consumers/cjs/package-lock.json b/tests/consumers/cjs/package-lock.json index aa1d07d..8d49c5d 100644 --- a/tests/consumers/cjs/package-lock.json +++ b/tests/consumers/cjs/package-lock.json @@ -12,8 +12,12 @@ } }, "../../..": { - "version": "1.1.1", + "name": "robust-validator", + "version": "2.0.0-rc-2", "license": "MIT", + "dependencies": { + "date-fns": "^4.1.0" + }, "devDependencies": { "@babel/preset-env": "^7.23.6", "@babel/preset-typescript": "^7.23.3", @@ -23,7 +27,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", @@ -31,7 +34,6 @@ "eslint-plugin-promise": "^6.1.1", "husky": "^9.0.10", "jest": "^29.7.0", - "microbundle": "^0.15.1", "rollup": "^4.9.1", "rollup-plugin-analyzer": "^4.0.0", "rollup-plugin-auto-external": "^2.0.0", @@ -48,9 +50,6 @@ }, "engines": { "node": ">=18.0.0" - }, - "peerDependencies": { - "dayjs": "^1.11.10" } }, "node_modules/robust-validator": { diff --git a/tests/consumers/esm/package-lock.json b/tests/consumers/esm/package-lock.json index f41b833..8d49c5d 100644 --- a/tests/consumers/esm/package-lock.json +++ b/tests/consumers/esm/package-lock.json @@ -8,13 +8,16 @@ "name": "tester", "version": "1.0.0", "dependencies": { - "dayjs": "^1.11.10", "robust-validator": "file:../../../" } }, "../../..": { - "version": "1.1.1", + "name": "robust-validator", + "version": "2.0.0-rc-2", "license": "MIT", + "dependencies": { + "date-fns": "^4.1.0" + }, "devDependencies": { "@babel/preset-env": "^7.23.6", "@babel/preset-typescript": "^7.23.3", @@ -24,7 +27,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", @@ -32,7 +34,6 @@ "eslint-plugin-promise": "^6.1.1", "husky": "^9.0.10", "jest": "^29.7.0", - "microbundle": "^0.15.1", "rollup": "^4.9.1", "rollup-plugin-analyzer": "^4.0.0", "rollup-plugin-auto-external": "^2.0.0", @@ -49,16 +50,8 @@ }, "engines": { "node": ">=18.0.0" - }, - "peerDependencies": { - "dayjs": "^1.11.10" } }, - "node_modules/dayjs": { - "version": "1.11.13", - "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.13.tgz", - "integrity": "sha512-oaMBel6gjolK862uaPQOVTA7q3TZhuSvuMQAAglQDOWYO9A91IrAOUJEyKVlqJlHE0vq5p5UXxzdPfMH/x6xNg==" - }, "node_modules/robust-validator": { "resolved": "../../..", "link": true diff --git a/tests/consumers/esm/package.json b/tests/consumers/esm/package.json index 64c81da..2b03a3f 100644 --- a/tests/consumers/esm/package.json +++ b/tests/consumers/esm/package.json @@ -7,7 +7,6 @@ "test": "rm -rf node_modules && rm -rf package-lock.json && npm install && node index.js" }, "dependencies": { - "dayjs": "^1.11.10", "robust-validator": "file:../../../" } } diff --git a/tests/consumers/ts-local/package-lock.json b/tests/consumers/ts-local/package-lock.json index c3b81fc..f5787c6 100644 --- a/tests/consumers/ts-local/package-lock.json +++ b/tests/consumers/ts-local/package-lock.json @@ -16,8 +16,12 @@ } }, "../../..": { - "version": "1.1.1", + "name": "robust-validator", + "version": "2.0.0-rc-2", "license": "MIT", + "dependencies": { + "date-fns": "^4.1.0" + }, "devDependencies": { "@babel/preset-env": "^7.23.6", "@babel/preset-typescript": "^7.23.3", @@ -27,7 +31,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", @@ -35,7 +38,6 @@ "eslint-plugin-promise": "^6.1.1", "husky": "^9.0.10", "jest": "^29.7.0", - "microbundle": "^0.15.1", "rollup": "^4.9.1", "rollup-plugin-analyzer": "^4.0.0", "rollup-plugin-auto-external": "^2.0.0", @@ -52,9 +54,6 @@ }, "engines": { "node": ">=18.0.0" - }, - "peerDependencies": { - "dayjs": "^1.11.10" } }, "node_modules/@cspotcode/source-map-support": { diff --git a/tests/rules/date.test.ts b/tests/rules/date.test.ts index 2a8e5b1..f6c7839 100644 --- a/tests/rules/date.test.ts +++ b/tests/rules/date.test.ts @@ -6,18 +6,18 @@ describe("isDate() ", () => { expect(isDate("2023-01-01")).toBe(true); expect(isDate("2023-01-15")).toBe(true); expect(isDate("2023-01-31")).toBe(true); - expect(isDate("2023-31-01", "YYYY-DD-MM")).toBe(true); - expect(isDate("2023.31.01", "YYYY.DD.MM")).toBe(true); - expect(isDate("2012-05-28 10:21:15", "YYYY-MM-DD HH:mm:ss")).toBe(true); - expect(isDate("2023 January 15", "YYYY MMMM DD")).toBe(true); - expect(isDate("2023 Jan 15", "YYYY MMM DD")).toBe(true); + expect(isDate("2023-31-01", "yyyy-dd-MM")).toBe(true); + expect(isDate("2023.31.01", "yyyy.dd.MM")).toBe(true); + expect(isDate("2012-05-28 10:21:15", "yyyy-MM-dd HH:mm:ss")).toBe(true); + expect(isDate("2023 January 15", "yyyy MMMM dd")).toBe(true); + expect(isDate("2023 Jan 15", "yyyy MMM dd")).toBe(true); }); test("should return FALSE for invalid dates", () => { expect(isDate("2023-01-34")).toBe(false); expect(isDate("2023-02-31")).toBe(false); expect(isDate("02-31-2023")).toBe(false); - expect(isDate("2023-31-01", "YYYY-MM-DD")).toBe(false); - expect(isDate("2023-31-01", "YYYY.DD.MM")).toBe(false); + expect(isDate("2023-31-01", "yyyy-MM-dd")).toBe(false); + expect(isDate("2023-31-01", "yyyy.dd.MM")).toBe(false); }); });