From f91dbb659a23f7827e2082fa2078e3f487c41064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Adem=20I=C5=9EIKLI?= Date: Sun, 24 Dec 2023 21:50:37 +0100 Subject: [PATCH 1/5] Fixed --- index.ts | 16 +++++++--------- src/Constants.ts | 2 +- src/rules/index.ts | 2 +- src/validator.ts | 5 ----- tests/Locale.test.ts | 4 +--- tests/index.test.ts | 4 +--- tests/rules/accepted.test.ts | 2 +- tests/rules/after.test.ts | 2 +- tests/rules/afterOrEqual.test.ts | 2 +- tests/rules/alpha.test.ts | 2 +- tests/rules/alphaDash.test.ts | 2 +- tests/rules/alphaNum.test.ts | 2 +- tests/rules/array.test.ts | 2 +- tests/rules/before.test.ts | 2 +- tests/rules/beforeOrEqual.test.ts | 2 +- tests/rules/between.test.ts | 2 +- tests/rules/boolean.test.ts | 2 +- tests/rules/confirmed.test.ts | 2 +- tests/rules/date.test.ts | 2 +- tests/rules/digits.test.ts | 2 +- tests/rules/digitsBetween.test.ts | 2 +- tests/rules/email.test.ts | 2 +- tests/rules/hex.test.ts | 2 +- tests/rules/in.test.ts | 2 +- tests/rules/integer.test.ts | 2 +- tests/rules/max.test.ts | 2 +- tests/rules/min.test.ts | 2 +- tests/rules/notIn.test.ts | 2 +- tests/rules/numeric.test.ts | 2 +- tests/rules/required.test.ts | 2 +- tests/rules/size.test.ts | 2 +- tests/rules/string.test.ts | 2 +- tests/rules/url.test.ts | 2 +- 33 files changed, 38 insertions(+), 49 deletions(-) delete mode 100644 src/validator.ts diff --git a/index.ts b/index.ts index 39fc23c..1dc6251 100644 --- a/index.ts +++ b/index.ts @@ -1,9 +1,7 @@ -import validator from "./src/validator"; - -export const getOptions = validator.getOptions; - -export const setLocales = validator.setLocales; - -export const setOptions = validator.setOptions; - -export const validate = validator.validate; +export * from "./src/Options"; +export * from "./src/helpers/validate"; +export * from "./src/Locale"; +export * from "./src/rules"; +export * from "./src/Constants"; +export * from "./src/Interface"; +export * from "./src/Types"; diff --git a/src/Constants.ts b/src/Constants.ts index 3ed342a..fea861f 100644 --- a/src/Constants.ts +++ b/src/Constants.ts @@ -1,5 +1,5 @@ import { RuleType, RuleFunction } from "./Types"; -import rules from "./rules"; +import * as rules from "./rules"; import { IOptions } from "./Interface"; export const RULE_FUNCTION_MAPS: Record = { diff --git a/src/rules/index.ts b/src/rules/index.ts index ab07803..fb8f40d 100644 --- a/src/rules/index.ts +++ b/src/rules/index.ts @@ -26,7 +26,7 @@ import isSize from "./isSize"; import isString from "./isString"; import isUrl from "./isUrl"; -export default { +export { isAccepted, isAfter, isAfterOrEqual, diff --git a/src/validator.ts b/src/validator.ts deleted file mode 100644 index 0502b94..0000000 --- a/src/validator.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { getOptions, setOptions } from "./Options"; -import { validate } from "./helpers/validate"; -import { setLocales } from "./Locale"; - -export default { getOptions, setOptions, validate, setLocales }; diff --git a/tests/Locale.test.ts b/tests/Locale.test.ts index dda6181..77067fe 100644 --- a/tests/Locale.test.ts +++ b/tests/Locale.test.ts @@ -1,7 +1,5 @@ import { describe, test, expect } from "vitest"; -import { LanguageType } from "../src/Types"; -import { setLocales } from "../src/Locale"; -import { ILocale } from "../src/Interface"; +import { LanguageType, setLocales, ILocale } from "../index"; const LANGUAGES: Record = { ar: "ar", diff --git a/tests/index.test.ts b/tests/index.test.ts index 3afd6ac..1ac491c 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,9 +1,7 @@ import { beforeAll, describe, expect, test } from "vitest"; -import { setOptions, validate } from "../index"; -import { setLocales } from "../src/Locale"; +import { setOptions, validate, setLocales, ILocale } from "../index"; import en from "../src/i18n/en.json"; import tr from "../src/i18n/tr.json"; -import { ILocale } from "../src/Interface"; describe("validate() function ", () => { beforeAll(async () => { diff --git a/tests/rules/accepted.test.ts b/tests/rules/accepted.test.ts index fe910dd..ae2e080 100644 --- a/tests/rules/accepted.test.ts +++ b/tests/rules/accepted.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isAccepted from "../../src/rules/isAccepted"; +import { isAccepted } from "../../index"; describe("isAccepted() ", () => { it("should return true for valid values", () => { diff --git a/tests/rules/after.test.ts b/tests/rules/after.test.ts index 68c0b3a..372630b 100644 --- a/tests/rules/after.test.ts +++ b/tests/rules/after.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isAfter from "../../src/rules/isAfter"; +import { isAfter } from "../../index"; describe("isAfter() ", () => { it("should return true if value is after the date", () => { diff --git a/tests/rules/afterOrEqual.test.ts b/tests/rules/afterOrEqual.test.ts index 8848c03..c4add6a 100644 --- a/tests/rules/afterOrEqual.test.ts +++ b/tests/rules/afterOrEqual.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isAfterOrEqual from "../../src/rules/isAfterOrEqual"; +import { isAfterOrEqual } from "../../index"; describe("isAfterOrEqual() ", () => { it("returns true if the value is after or equal to the date (string input)", () => { diff --git a/tests/rules/alpha.test.ts b/tests/rules/alpha.test.ts index 765fbcd..9bef9a1 100644 --- a/tests/rules/alpha.test.ts +++ b/tests/rules/alpha.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isAlpha from "../../src/rules/isAlpha"; +import { isAlpha } from "../../index"; describe("isAlpha() ", () => { it("returns true for valid alphabetic strings", () => { diff --git a/tests/rules/alphaDash.test.ts b/tests/rules/alphaDash.test.ts index f24288b..a90489a 100644 --- a/tests/rules/alphaDash.test.ts +++ b/tests/rules/alphaDash.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isAlphaDash from "../../src/rules/isAlphaDash"; +import { isAlphaDash } from "../../index"; describe("isAlphaDash() ", () => { it("should return true for valid inputs", () => { diff --git a/tests/rules/alphaNum.test.ts b/tests/rules/alphaNum.test.ts index f804d2d..c43cf99 100644 --- a/tests/rules/alphaNum.test.ts +++ b/tests/rules/alphaNum.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isAlphaNum from "../../src/rules/isAlphaNum"; +import { isAlphaNum } from "../../index"; describe("isAlphaNum() ", () => { it("returns true for valid alpha-numeric strings", () => { diff --git a/tests/rules/array.test.ts b/tests/rules/array.test.ts index 90f2cc3..379dae6 100644 --- a/tests/rules/array.test.ts +++ b/tests/rules/array.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isArray from "../../src/rules/isArray"; +import { isArray } from "../../index"; describe("isArray() ", () => { it("should return true for an empty array", () => { diff --git a/tests/rules/before.test.ts b/tests/rules/before.test.ts index 19336aa..2a8163c 100644 --- a/tests/rules/before.test.ts +++ b/tests/rules/before.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isBefore from "../../src/rules/isBefore"; +import { isBefore } from "../../index"; describe("isBefore() ", () => { it("should return true for null or undefined values", () => { diff --git a/tests/rules/beforeOrEqual.test.ts b/tests/rules/beforeOrEqual.test.ts index f3aa767..37e52dd 100644 --- a/tests/rules/beforeOrEqual.test.ts +++ b/tests/rules/beforeOrEqual.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isBeforeOrEqual from "../../src/rules/isBeforeOrEqual"; +import { isBeforeOrEqual } from "../../index"; describe("isBeforeOrEqual() ", () => { it("should return true for null or undefined values", () => { diff --git a/tests/rules/between.test.ts b/tests/rules/between.test.ts index 2f1ea7b..e63e085 100644 --- a/tests/rules/between.test.ts +++ b/tests/rules/between.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "vitest"; -import isBetween from "../../src/rules/isBetween"; +import { isBetween } from "../../index"; describe("isBetween() ", () => { test("string with valid length", () => { diff --git a/tests/rules/boolean.test.ts b/tests/rules/boolean.test.ts index 22ba742..7af1cee 100644 --- a/tests/rules/boolean.test.ts +++ b/tests/rules/boolean.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isBoolean from "../../src/rules/isBoolean"; +import { isBoolean } from "../../index"; describe("isBoolean() ", () => { it("should return true for valid boolean values", () => { diff --git a/tests/rules/confirmed.test.ts b/tests/rules/confirmed.test.ts index c7108c3..dac8a77 100644 --- a/tests/rules/confirmed.test.ts +++ b/tests/rules/confirmed.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isConfirmed from "../../src/rules/isConfirmed"; +import { isConfirmed } from "../../index"; const toContext = (value: any) => { return { diff --git a/tests/rules/date.test.ts b/tests/rules/date.test.ts index 2aa4009..b59cd56 100644 --- a/tests/rules/date.test.ts +++ b/tests/rules/date.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isDate from "../../src/rules/isDate"; +import { isDate } from "../../index"; describe("isDate() ", () => { it("should return true for valid date strings", () => { diff --git a/tests/rules/digits.test.ts b/tests/rules/digits.test.ts index 8925750..e2b75b9 100644 --- a/tests/rules/digits.test.ts +++ b/tests/rules/digits.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isDigits from "../../src/rules/isDigits"; +import { isDigits } from "../../index"; describe("isDigits() ", () => { it("returns true for null or undefined values", () => { diff --git a/tests/rules/digitsBetween.test.ts b/tests/rules/digitsBetween.test.ts index 895a3a5..6df85f7 100644 --- a/tests/rules/digitsBetween.test.ts +++ b/tests/rules/digitsBetween.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "vitest"; -import isDigitsBetween from "../../src/rules/isDigitsBetween"; +import { isDigitsBetween } from "../../index"; describe("digitsBetween() ", () => { test("Valid numeric value within range", () => { diff --git a/tests/rules/email.test.ts b/tests/rules/email.test.ts index dc3a916..5708433 100644 --- a/tests/rules/email.test.ts +++ b/tests/rules/email.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isEmail from "../../src/rules/isEmail"; +import { isEmail } from "../../index"; describe("isEmail() ", () => { it("should return true for valid email addresses", () => { diff --git a/tests/rules/hex.test.ts b/tests/rules/hex.test.ts index 52a9f6e..59414a3 100644 --- a/tests/rules/hex.test.ts +++ b/tests/rules/hex.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isHex from "../../src/rules/isHex"; +import { isHex } from "../../index"; describe("isHex() ", () => { it("should return true for valid hexadecimal strings", () => { diff --git a/tests/rules/in.test.ts b/tests/rules/in.test.ts index 79f3795..43c8039 100644 --- a/tests/rules/in.test.ts +++ b/tests/rules/in.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isIn from "../../src/rules/isIn"; +import { isIn } from "../../index"; describe("isIn() ", () => { it("should return true for null or undefined values", () => { diff --git a/tests/rules/integer.test.ts b/tests/rules/integer.test.ts index c100f68..8314f2b 100644 --- a/tests/rules/integer.test.ts +++ b/tests/rules/integer.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isInteger from "../../src/rules/isInteger"; +import { isInteger } from "../../index"; describe("isInteger() ", () => { it("should return true for null", () => { diff --git a/tests/rules/max.test.ts b/tests/rules/max.test.ts index 7b8854a..479bb13 100644 --- a/tests/rules/max.test.ts +++ b/tests/rules/max.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "vitest"; -import isMax from "../../src/rules/isMax"; +import { isMax } from "../../index"; describe("isMax() ", () => { test("returns true when value is less than or equal to size", () => { diff --git a/tests/rules/min.test.ts b/tests/rules/min.test.ts index 68a4158..1f47072 100644 --- a/tests/rules/min.test.ts +++ b/tests/rules/min.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "vitest"; -import isMin from "../../src/rules/isMin"; +import { isMin } from "../../index"; describe("isMin() ", () => { test("returns false when value is less than or equal to size", () => { diff --git a/tests/rules/notIn.test.ts b/tests/rules/notIn.test.ts index df7a844..bbad0b8 100644 --- a/tests/rules/notIn.test.ts +++ b/tests/rules/notIn.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isNotIn from "../../src/rules/isNotIn"; +import { isNotIn } from "../../index"; describe("isNotIn() ", () => { it("should return true for null or undefined values", () => { diff --git a/tests/rules/numeric.test.ts b/tests/rules/numeric.test.ts index b203a24..a5f9ce4 100644 --- a/tests/rules/numeric.test.ts +++ b/tests/rules/numeric.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "vitest"; -import isNumeric from "../../src/rules/isNumeric"; +import { isNumeric } from "../../index"; describe("isNumeric() ", () => { test("should return true for numeric values", () => { diff --git a/tests/rules/required.test.ts b/tests/rules/required.test.ts index 83c747f..dc62423 100644 --- a/tests/rules/required.test.ts +++ b/tests/rules/required.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isRequired from "../../src/rules/isRequired"; +import { isRequired } from "../../index"; describe("isRequired() ", () => { it("should return true for non-null, non-undefined, and non-empty string values", () => { diff --git a/tests/rules/size.test.ts b/tests/rules/size.test.ts index 0302a21..6eef510 100644 --- a/tests/rules/size.test.ts +++ b/tests/rules/size.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isSize from "../../src/rules/isSize"; +import { isSize } from "../../index"; describe("isSize() ", () => { it("should return true for null or undefined values", () => { diff --git a/tests/rules/string.test.ts b/tests/rules/string.test.ts index 745b744..eb7ce87 100644 --- a/tests/rules/string.test.ts +++ b/tests/rules/string.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isString from "../../src/rules/isString"; +import { isString } from "../../index"; describe("isString() ", () => { it("should return the correct value for the data type", () => { diff --git a/tests/rules/url.test.ts b/tests/rules/url.test.ts index f8f06ae..6e6ce52 100644 --- a/tests/rules/url.test.ts +++ b/tests/rules/url.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import isUrl from "../../src/rules/isUrl"; +import { isUrl } from "../../index"; describe("isUrl() ", () => { it("should return true for null and undefined", () => { From 462caf30af022bf992e4fe8894c0c98f856cdae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Adem=20I=C5=9EIKLI?= Date: Sun, 24 Dec 2023 21:57:33 +0100 Subject: [PATCH 2/5] Fixed rollup configuration and tests --- default.ts | 3 +++ rollup.config.mjs | 2 +- tests/consumers/cjs/index.js | 3 ++- tests/consumers/esm/index.js | 3 ++- tests/consumers/esm/package-lock.json | 5 ++--- tests/consumers/ts/index.ts | 3 ++- 6 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 default.ts diff --git a/default.ts b/default.ts new file mode 100644 index 0000000..5740720 --- /dev/null +++ b/default.ts @@ -0,0 +1,3 @@ +import * as all from "./index"; + +export default all; diff --git a/rollup.config.mjs b/rollup.config.mjs index 6a9692f..bb9e204 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -11,7 +11,7 @@ import path from "path"; const outputFileName = "bundle"; const name = "bundle"; const namedInput = "./index.ts"; -const defaultInput = "./src/validator.ts"; +const defaultInput = "./default.ts"; const buildConfig = ({ es5, diff --git a/tests/consumers/cjs/index.js b/tests/consumers/cjs/index.js index e327820..6029fd2 100644 --- a/tests/consumers/cjs/index.js +++ b/tests/consumers/cjs/index.js @@ -1,4 +1,4 @@ -const { validate, setLocales } = require("robust-validator"); +const { validate, setLocales, isEmail } = require("robust-validator"); const en = require("robust-validator/dist/i18n/en.json"); const data = { @@ -22,6 +22,7 @@ const main = async () => { } console.log("CJS module tests are succeed!"); + console.log("isEmail", isEmail); }; main(); diff --git a/tests/consumers/esm/index.js b/tests/consumers/esm/index.js index 11fa3af..7e1c4fe 100644 --- a/tests/consumers/esm/index.js +++ b/tests/consumers/esm/index.js @@ -1,7 +1,7 @@ import pkg from "robust-validator"; import en from "robust-validator/dist/i18n/en.json" assert { type: "json" }; -const { validate, setLocales } = pkg; +const { validate, setLocales, isEmail } = pkg; const data = { email: null, @@ -24,6 +24,7 @@ const main = async () => { } console.log("ESM module tests are succeed!"); + console.log("isEmail", isEmail); }; main(); diff --git a/tests/consumers/esm/package-lock.json b/tests/consumers/esm/package-lock.json index 055d7b2..a6833b5 100644 --- a/tests/consumers/esm/package-lock.json +++ b/tests/consumers/esm/package-lock.json @@ -92,8 +92,8 @@ }, "../../..": { "name": "robust-validator", - "version": "0.1.0", - "license": "ISC", + "version": "0.1.1", + "license": "MIT", "devDependencies": { "@babel/preset-env": "^7.23.6", "@babel/preset-typescript": "^7.23.3", @@ -117,7 +117,6 @@ "rollup": "^4.9.1", "rollup-plugin-analyzer": "^4.0.0", "rollup-plugin-auto-external": "^2.0.0", - "rollup-plugin-bundle-size": "^1.0.3", "rollup-plugin-copy": "^3.5.0", "rollup-plugin-filesize": "^10.0.0", "rollup-plugin-typescript2": "^0.36.0", diff --git a/tests/consumers/ts/index.ts b/tests/consumers/ts/index.ts index c3f816f..10499ab 100644 --- a/tests/consumers/ts/index.ts +++ b/tests/consumers/ts/index.ts @@ -1,4 +1,4 @@ -import { validate, setLocales, setOptions } from "robust-validator"; +import { validate, setLocales, setOptions, isEmail } from "robust-validator"; import en from "robust-validator/dist/i18n/en.json"; const data = { @@ -25,6 +25,7 @@ const main = async () => { } console.log("ESM module tests are succeed!"); + console.log("isEmail", isEmail); }; main(); From 4ef60bd18f3656f2dff70a393fb50d394a25d1e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Adem=20I=C5=9EIKLI?= Date: Sun, 24 Dec 2023 22:23:04 +0100 Subject: [PATCH 3/5] Added converter functions --- index.ts | 1 + src/converters/accepted.ts | 3 + src/converters/after.ts | 3 + src/converters/afterOrEqual.ts | 3 + src/converters/alpha.ts | 3 + src/converters/alphaDash.ts | 3 + src/converters/alphaNum.ts | 3 + src/converters/array.ts | 3 + src/converters/before.ts | 3 + src/converters/beforeOrEqual.ts | 3 + src/converters/between.ts | 3 + src/converters/boolean.ts | 3 + src/converters/confirmed.ts | 3 + src/converters/date.ts | 3 + src/converters/digits.ts | 3 + src/converters/digitsBetween.ts | 3 + src/converters/email.ts | 3 + src/converters/hex.ts | 3 + src/converters/in.ts | 3 + src/converters/index.ts | 57 +++++++++ src/converters/integer.ts | 3 + src/converters/max.ts | 3 + src/converters/min.ts | 3 + src/converters/notIn.ts | 3 + src/converters/numeric.ts | 3 + src/converters/required.ts | 3 + src/converters/size.ts | 3 + src/converters/string.ts | 3 + src/converters/url.ts | 3 + tests/converters/index.test.ts | 209 ++++++++++++++++++++++++++++++++ 30 files changed, 348 insertions(+) create mode 100644 src/converters/accepted.ts create mode 100644 src/converters/after.ts create mode 100644 src/converters/afterOrEqual.ts create mode 100644 src/converters/alpha.ts create mode 100644 src/converters/alphaDash.ts create mode 100644 src/converters/alphaNum.ts create mode 100644 src/converters/array.ts create mode 100644 src/converters/before.ts create mode 100644 src/converters/beforeOrEqual.ts create mode 100644 src/converters/between.ts create mode 100644 src/converters/boolean.ts create mode 100644 src/converters/confirmed.ts create mode 100644 src/converters/date.ts create mode 100644 src/converters/digits.ts create mode 100644 src/converters/digitsBetween.ts create mode 100644 src/converters/email.ts create mode 100644 src/converters/hex.ts create mode 100644 src/converters/in.ts create mode 100644 src/converters/index.ts create mode 100644 src/converters/integer.ts create mode 100644 src/converters/max.ts create mode 100644 src/converters/min.ts create mode 100644 src/converters/notIn.ts create mode 100644 src/converters/numeric.ts create mode 100644 src/converters/required.ts create mode 100644 src/converters/size.ts create mode 100644 src/converters/string.ts create mode 100644 src/converters/url.ts create mode 100644 tests/converters/index.test.ts diff --git a/index.ts b/index.ts index 1dc6251..78d4289 100644 --- a/index.ts +++ b/index.ts @@ -2,6 +2,7 @@ export * from "./src/Options"; export * from "./src/helpers/validate"; export * from "./src/Locale"; export * from "./src/rules"; +export * from "./src/converters"; export * from "./src/Constants"; export * from "./src/Interface"; export * from "./src/Types"; diff --git a/src/converters/accepted.ts b/src/converters/accepted.ts new file mode 100644 index 0000000..5e05b34 --- /dev/null +++ b/src/converters/accepted.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "accepted"; +}; diff --git a/src/converters/after.ts b/src/converters/after.ts new file mode 100644 index 0000000..72539a2 --- /dev/null +++ b/src/converters/after.ts @@ -0,0 +1,3 @@ +export default (date: string): string => { + return `after:${date}`; +}; diff --git a/src/converters/afterOrEqual.ts b/src/converters/afterOrEqual.ts new file mode 100644 index 0000000..3b829ac --- /dev/null +++ b/src/converters/afterOrEqual.ts @@ -0,0 +1,3 @@ +export default (date: string): string => { + return `after_or_equal:${date}`; +}; diff --git a/src/converters/alpha.ts b/src/converters/alpha.ts new file mode 100644 index 0000000..3c50e0b --- /dev/null +++ b/src/converters/alpha.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "alpha"; +}; diff --git a/src/converters/alphaDash.ts b/src/converters/alphaDash.ts new file mode 100644 index 0000000..9e4f9e7 --- /dev/null +++ b/src/converters/alphaDash.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "alpha_dash"; +}; diff --git a/src/converters/alphaNum.ts b/src/converters/alphaNum.ts new file mode 100644 index 0000000..ce5feb6 --- /dev/null +++ b/src/converters/alphaNum.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "alpha_num"; +}; diff --git a/src/converters/array.ts b/src/converters/array.ts new file mode 100644 index 0000000..fc935e1 --- /dev/null +++ b/src/converters/array.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "array"; +}; diff --git a/src/converters/before.ts b/src/converters/before.ts new file mode 100644 index 0000000..44a1ac5 --- /dev/null +++ b/src/converters/before.ts @@ -0,0 +1,3 @@ +export default (date: string): string => { + return `before:${date}`; +}; diff --git a/src/converters/beforeOrEqual.ts b/src/converters/beforeOrEqual.ts new file mode 100644 index 0000000..6c2bb03 --- /dev/null +++ b/src/converters/beforeOrEqual.ts @@ -0,0 +1,3 @@ +export default (date: string): string => { + return `before_or_equal:${date}`; +}; diff --git a/src/converters/between.ts b/src/converters/between.ts new file mode 100644 index 0000000..193bee7 --- /dev/null +++ b/src/converters/between.ts @@ -0,0 +1,3 @@ +export default (min: number, max: number): string => { + return `between:${min},${max}`; +}; diff --git a/src/converters/boolean.ts b/src/converters/boolean.ts new file mode 100644 index 0000000..f365ee3 --- /dev/null +++ b/src/converters/boolean.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "boolean"; +}; diff --git a/src/converters/confirmed.ts b/src/converters/confirmed.ts new file mode 100644 index 0000000..6dabc1e --- /dev/null +++ b/src/converters/confirmed.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "confirmed"; +}; diff --git a/src/converters/date.ts b/src/converters/date.ts new file mode 100644 index 0000000..d1146d6 --- /dev/null +++ b/src/converters/date.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "date"; +}; diff --git a/src/converters/digits.ts b/src/converters/digits.ts new file mode 100644 index 0000000..13bf1a0 --- /dev/null +++ b/src/converters/digits.ts @@ -0,0 +1,3 @@ +export default (length: number): string => { + return `digits:${length}`; +}; diff --git a/src/converters/digitsBetween.ts b/src/converters/digitsBetween.ts new file mode 100644 index 0000000..96c9e80 --- /dev/null +++ b/src/converters/digitsBetween.ts @@ -0,0 +1,3 @@ +export default (min: number, max: number): string => { + return `digits_between:${min},${max}`; +}; diff --git a/src/converters/email.ts b/src/converters/email.ts new file mode 100644 index 0000000..c9f814f --- /dev/null +++ b/src/converters/email.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "email"; +}; diff --git a/src/converters/hex.ts b/src/converters/hex.ts new file mode 100644 index 0000000..e951bdc --- /dev/null +++ b/src/converters/hex.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "hex"; +}; diff --git a/src/converters/in.ts b/src/converters/in.ts new file mode 100644 index 0000000..15967f7 --- /dev/null +++ b/src/converters/in.ts @@ -0,0 +1,3 @@ +export default (items: string[]): string => { + return `in:${items.join(",")}`; +}; diff --git a/src/converters/index.ts b/src/converters/index.ts new file mode 100644 index 0000000..0e7c2e1 --- /dev/null +++ b/src/converters/index.ts @@ -0,0 +1,57 @@ +import accepted from "./accepted"; +import after from "./after"; +import afterOrEqual from "./afterOrEqual"; +import alpha from "./alpha"; +import alphaDash from "./alphaDash"; +import alphaNum from "./alphaNum"; +import array from "./array"; +import before from "./before"; +import beforeOrEqual from "./beforeOrEqual"; +import between from "./between"; +import boolean from "./boolean"; +import confirmed from "./confirmed"; +import date from "./date"; +import digits from "./digits"; +import digitsBetween from "./digitsBetween"; +import email from "./email"; +import hex from "./hex"; +import inConverter from "./in"; +import integer from "./integer"; +import max from "./max"; +import min from "./min"; +import notIn from "./notIn"; +import numeric from "./numeric"; +import required from "./required"; +import size from "./size"; +import string from "./string"; +import url from "./url"; + +export { + accepted, + after, + afterOrEqual, + alpha, + alphaDash, + alphaNum, + array, + before, + beforeOrEqual, + between, + boolean, + confirmed, + date, + digits, + digitsBetween, + email, + hex, + inConverter, + integer, + max, + min, + notIn, + numeric, + required, + size, + string, + url, +}; diff --git a/src/converters/integer.ts b/src/converters/integer.ts new file mode 100644 index 0000000..2ce3a63 --- /dev/null +++ b/src/converters/integer.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "integer"; +}; diff --git a/src/converters/max.ts b/src/converters/max.ts new file mode 100644 index 0000000..5ce0e32 --- /dev/null +++ b/src/converters/max.ts @@ -0,0 +1,3 @@ +export default (value: number): string => { + return `max:${value}`; +}; diff --git a/src/converters/min.ts b/src/converters/min.ts new file mode 100644 index 0000000..1afa266 --- /dev/null +++ b/src/converters/min.ts @@ -0,0 +1,3 @@ +export default (value: number): string => { + return `min:${value}`; +}; diff --git a/src/converters/notIn.ts b/src/converters/notIn.ts new file mode 100644 index 0000000..93caea1 --- /dev/null +++ b/src/converters/notIn.ts @@ -0,0 +1,3 @@ +export default (items: string[]): string => { + return `not_in:${items.join(",")}`; +}; diff --git a/src/converters/numeric.ts b/src/converters/numeric.ts new file mode 100644 index 0000000..803c363 --- /dev/null +++ b/src/converters/numeric.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "numeric"; +}; diff --git a/src/converters/required.ts b/src/converters/required.ts new file mode 100644 index 0000000..ac7ed02 --- /dev/null +++ b/src/converters/required.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "required"; +}; diff --git a/src/converters/size.ts b/src/converters/size.ts new file mode 100644 index 0000000..a084a2f --- /dev/null +++ b/src/converters/size.ts @@ -0,0 +1,3 @@ +export default (value: number): string => { + return `size:${value}`; +}; diff --git a/src/converters/string.ts b/src/converters/string.ts new file mode 100644 index 0000000..a6a3048 --- /dev/null +++ b/src/converters/string.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "string"; +}; diff --git a/src/converters/url.ts b/src/converters/url.ts new file mode 100644 index 0000000..c17ef0f --- /dev/null +++ b/src/converters/url.ts @@ -0,0 +1,3 @@ +export default (): string => { + return "url"; +}; diff --git a/tests/converters/index.test.ts b/tests/converters/index.test.ts new file mode 100644 index 0000000..e956358 --- /dev/null +++ b/tests/converters/index.test.ts @@ -0,0 +1,209 @@ +import { describe, expect, it, afterAll } from "vitest"; +import { + RULE_FUNCTION_MAPS, + accepted, + after, + afterOrEqual, + alpha, + alphaDash, + alphaNum, + array, + before, + beforeOrEqual, + between, + boolean, + confirmed, + date, + digits, + digitsBetween, + email, + hex, + inConverter, + integer, + max, + min, + notIn, + numeric, + required, + size, + string, + url, +} from "../../index"; + +const RESULT_LIST: string[] = []; + +describe("converters functions", () => { + afterAll(() => { + const ruleNames = Object.keys(RULE_FUNCTION_MAPS); + const results = RESULT_LIST.map((item) => { + const [name] = item.split(":"); + return name; + }); + + for (const rule of ruleNames) { + expect(results.includes(rule), rule).toBe(true); + } + }); + + it("accepted() should be able to convert to the correct pattern.", () => { + const result = accepted(); + RESULT_LIST.push(result); + expect(result).toBe("accepted"); + }); + + it("after() should be able to convert to the correct pattern.", () => { + const result = after("2023-10-21"); + RESULT_LIST.push(result); + expect(result).toBe("after:2023-10-21"); + }); + + it("afterOrEqual() should be able to convert to the correct pattern.", () => { + const result = afterOrEqual("2023-10-21"); + RESULT_LIST.push(result); + expect(result).toBe("after_or_equal:2023-10-21"); + }); + + it("alpha() should be able to convert to the correct pattern.", () => { + const result = alpha(); + RESULT_LIST.push(result); + expect(result).toBe("alpha"); + }); + + it("alphaDash() should be able to convert to the correct pattern.", () => { + const result = alphaDash(); + RESULT_LIST.push(result); + expect(result).toBe("alpha_dash"); + }); + + it("alphaNum() should be able to convert to the correct pattern.", () => { + const result = alphaNum(); + RESULT_LIST.push(result); + expect(result).toBe("alpha_num"); + }); + + it("array() should be able to convert to the correct pattern.", () => { + const result = array(); + RESULT_LIST.push(result); + expect(result).toBe("array"); + }); + + it("before() should be able to convert to the correct pattern.", () => { + const result = before("2023-10-21"); + RESULT_LIST.push(result); + expect(result).toBe("before:2023-10-21"); + }); + + it("beforeOrEqual() should be able to convert to the correct pattern.", () => { + const result = beforeOrEqual("2023-10-21"); + RESULT_LIST.push(result); + expect(result).toBe("before_or_equal:2023-10-21"); + }); + + it("between() should be able to convert to the correct pattern.", () => { + const result = between(5, 10); + RESULT_LIST.push(result); + expect(result).toBe("between:5,10"); + }); + + it("boolean() should be able to convert to the correct pattern.", () => { + const result = boolean(); + RESULT_LIST.push(result); + expect(result).toBe("boolean"); + }); + + it("confirmed() should be able to convert to the correct pattern.", () => { + const result = confirmed(); + RESULT_LIST.push(result); + expect(result).toBe("confirmed"); + }); + + it("date() should be able to convert to the correct pattern.", () => { + const result = date(); + RESULT_LIST.push(result); + expect(result).toBe("date"); + }); + + it("digits() should be able to convert to the correct pattern.", () => { + const result = digits(4); + RESULT_LIST.push(result); + expect(result).toBe("digits:4"); + }); + + it("digitsBetween() should be able to convert to the correct pattern.", () => { + const result = digitsBetween(4, 6); + RESULT_LIST.push(result); + expect(result).toBe("digits_between:4,6"); + }); + + it("email() should be able to convert to the correct pattern.", () => { + const result = email(); + RESULT_LIST.push(result); + expect(result).toBe("email"); + }); + + it("hex() should be able to convert to the correct pattern.", () => { + const result = hex(); + RESULT_LIST.push(result); + expect(result).toBe("hex"); + }); + + it("inConverter() should be able to convert to the correct pattern.", () => { + const result = inConverter(["apple", "orange"]); + RESULT_LIST.push(result); + expect(result).toBe("in:apple,orange"); + }); + + it("integer() should be able to convert to the correct pattern.", () => { + const result = integer(); + RESULT_LIST.push(result); + expect(result).toBe("integer"); + }); + + it("max() should be able to convert to the correct pattern.", () => { + const result = max(5); + RESULT_LIST.push(result); + expect(result).toBe("max:5"); + }); + + it("min() should be able to convert to the correct pattern.", () => { + const result = min(5); + RESULT_LIST.push(result); + expect(result).toBe("min:5"); + }); + + it("notIn() should be able to convert to the correct pattern.", () => { + const result = notIn(["apple", "orange"]); + RESULT_LIST.push(result); + expect(result).toBe("not_in:apple,orange"); + }); + + it("numeric() should be able to convert to the correct pattern.", () => { + const result = numeric(); + RESULT_LIST.push(result); + expect(result).toBe("numeric"); + }); + + it("required() should be able to convert to the correct pattern.", () => { + const result = required(); + RESULT_LIST.push(result); + expect(result).toBe("required"); + }); + + it("size() should be able to convert to the correct pattern.", () => { + const result = size(5); + RESULT_LIST.push(result); + expect(result).toBe("size:5"); + }); + + it("string() should be able to convert to the correct pattern.", () => { + const result = string(); + RESULT_LIST.push(result); + expect(result).toBe("string"); + }); + + it("url() should be able to convert to the correct pattern.", () => { + const result = url(); + RESULT_LIST.push(result); + expect(result).toBe("url"); + }); +}); From 4ac4a6ab6ad00afbd209e8a41a48bbe09d176bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Adem=20I=C5=9EIKLI?= Date: Sun, 24 Dec 2023 22:30:57 +0100 Subject: [PATCH 4/5] Added converter function support --- src/Types.ts | 2 ++ src/helpers/validate.ts | 16 ++++++++++++---- tests/index.test.ts | 21 ++++++++++++++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/Types.ts b/src/Types.ts index 605e1f4..544b88d 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -82,3 +82,5 @@ export type LanguageType = | "zh"; export type Translation = Record; + +export type Definition = Record; diff --git a/src/helpers/validate.ts b/src/helpers/validate.ts index e2d28ce..dbca0c1 100644 --- a/src/helpers/validate.ts +++ b/src/helpers/validate.ts @@ -1,13 +1,13 @@ import { IContext, IOptions, IValidationResult } from "../Interface"; import { getMessage } from "../Locale"; -import { ValidationResult } from "../Types"; +import { Definition, ValidationResult } from "../Types"; import { toRuleDefinition } from "../Factory"; import { getValueViaPath } from "./getValueViaPath"; import { getOptions } from "../Options"; export const validate = async ( data: any, - definition: Record, + definition: Definition, options?: Partial ): Promise => { const currentOptions: IOptions = { @@ -31,7 +31,7 @@ export const validate = async ( const getResults = async ( data: any, - definition: Record, + definition: Definition, options: IOptions ) => { let isValid = true; @@ -42,7 +42,15 @@ const getResults = async ( for (const key in definition) { fields[key] = true; // Parsing the rules - const rules = toRuleNameArray(definition[key]).map(toRuleDefinition); + const params = definition[key]; + let ruleGroup: string = ""; + if (Array.isArray(params)) { + ruleGroup = params.join("|"); + } else { + ruleGroup = params; + } + + const rules = toRuleNameArray(ruleGroup).map(toRuleDefinition); // Getting the value by the path const value = getValueViaPath(data, key); diff --git a/tests/index.test.ts b/tests/index.test.ts index 1ac491c..267ad9f 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,5 +1,12 @@ import { beforeAll, describe, expect, test } from "vitest"; -import { setOptions, validate, setLocales, ILocale } from "../index"; +import { + setOptions, + validate, + setLocales, + ILocale, + required, + email, +} from "../index"; import en from "../src/i18n/en.json"; import tr from "../src/i18n/tr.json"; @@ -152,4 +159,16 @@ describe("validate() function ", () => { expect(result.isValid).toBe(false); expect(result.errors.email[0].message).toBe("Alan bir e-posta olmalıdır."); }); + + test("should be able to use the converters", async () => { + const data = { + email: "sample", + }; + const rules = { + email: [required(), email()], + }; + + const result = await validate(data, rules); + expect(result.isValid).toBe(false); + }); }); From 5ae84bee49b1a8792e6d2f8167e6c746c48a78f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Adem=20I=C5=9EIKLI?= Date: Sun, 24 Dec 2023 22:44:39 +0100 Subject: [PATCH 5/5] 0.2.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index ef7aba2..bda6740 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "robust-validator", - "version": "0.1.1", + "version": "0.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "robust-validator", - "version": "0.1.1", + "version": "0.2.0", "license": "MIT", "devDependencies": { "@babel/preset-env": "^7.23.6", diff --git a/package.json b/package.json index 1c207fc..7f5a847 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "robust-validator", - "version": "0.1.1", + "version": "0.2.0", "description": "Rule-based data validation library", "exports": { ".": {