Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed module structure #12

Merged
merged 5 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as all from "./index";

export default all;
17 changes: 8 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
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/converters";
export * from "./src/Constants";
export * from "./src/Interface";
export * from "./src/Types";
4 changes: 2 additions & 2 deletions 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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "robust-validator",
"version": "0.1.1",
"version": "0.2.0",
"description": "Rule-based data validation library",
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.ts
Original file line number Diff line number Diff line change
@@ -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<RuleType, RuleFunction> = {
Expand Down
2 changes: 2 additions & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ export type LanguageType =
| "zh";

export type Translation = Record<RuleType, string>;

export type Definition = Record<string, string | string[]>;
3 changes: 3 additions & 0 deletions src/converters/accepted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "accepted";
};
3 changes: 3 additions & 0 deletions src/converters/after.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (date: string): string => {
return `after:${date}`;
};
3 changes: 3 additions & 0 deletions src/converters/afterOrEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (date: string): string => {
return `after_or_equal:${date}`;
};
3 changes: 3 additions & 0 deletions src/converters/alpha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "alpha";
};
3 changes: 3 additions & 0 deletions src/converters/alphaDash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "alpha_dash";
};
3 changes: 3 additions & 0 deletions src/converters/alphaNum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "alpha_num";
};
3 changes: 3 additions & 0 deletions src/converters/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "array";
};
3 changes: 3 additions & 0 deletions src/converters/before.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (date: string): string => {
return `before:${date}`;
};
3 changes: 3 additions & 0 deletions src/converters/beforeOrEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (date: string): string => {
return `before_or_equal:${date}`;
};
3 changes: 3 additions & 0 deletions src/converters/between.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (min: number, max: number): string => {
return `between:${min},${max}`;
};
3 changes: 3 additions & 0 deletions src/converters/boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "boolean";
};
3 changes: 3 additions & 0 deletions src/converters/confirmed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "confirmed";
};
3 changes: 3 additions & 0 deletions src/converters/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "date";
};
3 changes: 3 additions & 0 deletions src/converters/digits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (length: number): string => {
return `digits:${length}`;
};
3 changes: 3 additions & 0 deletions src/converters/digitsBetween.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (min: number, max: number): string => {
return `digits_between:${min},${max}`;
};
3 changes: 3 additions & 0 deletions src/converters/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "email";
};
3 changes: 3 additions & 0 deletions src/converters/hex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "hex";
};
3 changes: 3 additions & 0 deletions src/converters/in.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (items: string[]): string => {
return `in:${items.join(",")}`;
};
57 changes: 57 additions & 0 deletions src/converters/index.ts
Original file line number Diff line number Diff line change
@@ -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,
};
3 changes: 3 additions & 0 deletions src/converters/integer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "integer";
};
3 changes: 3 additions & 0 deletions src/converters/max.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (value: number): string => {
return `max:${value}`;
};
3 changes: 3 additions & 0 deletions src/converters/min.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (value: number): string => {
return `min:${value}`;
};
3 changes: 3 additions & 0 deletions src/converters/notIn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (items: string[]): string => {
return `not_in:${items.join(",")}`;
};
3 changes: 3 additions & 0 deletions src/converters/numeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "numeric";
};
3 changes: 3 additions & 0 deletions src/converters/required.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "required";
};
3 changes: 3 additions & 0 deletions src/converters/size.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (value: number): string => {
return `size:${value}`;
};
3 changes: 3 additions & 0 deletions src/converters/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "string";
};
3 changes: 3 additions & 0 deletions src/converters/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (): string => {
return "url";
};
16 changes: 12 additions & 4 deletions src/helpers/validate.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>,
definition: Definition,
options?: Partial<IOptions>
): Promise<IValidationResult> => {
const currentOptions: IOptions = {
Expand All @@ -31,7 +31,7 @@ export const validate = async (

const getResults = async (
data: any,
definition: Record<string, string>,
definition: Definition,
options: IOptions
) => {
let isValid = true;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import isSize from "./isSize";
import isString from "./isString";
import isUrl from "./isUrl";

export default {
export {
isAccepted,
isAfter,
isAfterOrEqual,
Expand Down
5 changes: 0 additions & 5 deletions src/validator.ts

This file was deleted.

4 changes: 1 addition & 3 deletions tests/Locale.test.ts
Original file line number Diff line number Diff line change
@@ -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<LanguageType, string> = {
ar: "ar",
Expand Down
3 changes: 2 additions & 1 deletion tests/consumers/cjs/index.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -22,6 +22,7 @@ const main = async () => {
}

console.log("CJS module tests are succeed!");
console.log("isEmail", isEmail);
};

main();
3 changes: 2 additions & 1 deletion tests/consumers/esm/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,6 +24,7 @@ const main = async () => {
}

console.log("ESM module tests are succeed!");
console.log("isEmail", isEmail);
};

main();
5 changes: 2 additions & 3 deletions tests/consumers/esm/package-lock.json

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

3 changes: 2 additions & 1 deletion tests/consumers/ts/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -25,6 +25,7 @@ const main = async () => {
}

console.log("ESM module tests are succeed!");
console.log("isEmail", isEmail);
};

main();
Loading
Loading