Skip to content

Commit

Permalink
Merge pull request #5
Browse files Browse the repository at this point in the history
Merge to main
  • Loading branch information
trapcodeio authored Sep 7, 2024
2 parents ce85a86 + 15a4ace commit c4699db
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 173 deletions.
2 changes: 1 addition & 1 deletion bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ files.push(getGzippedSize(fileEsm));
// copy index.d.ts to index.esm.d.ts

const folder = __dirname + `/validators`;
let validatorFolders = ["array", "date", "object", "string", "utils"];
let validatorFolders = ["array", "date", "object", "string", "utils", "number"];

for (const f of validatorFolders) {
const from = folder + `/${f}/index.js`;
Expand Down
2 changes: 2 additions & 0 deletions index.esm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ import Abolish from "./src/Abolish";
import { Schema, Rule } from "./src/functions";
import type { AV } from "./src/validator";

// Export
export * from "./src/shortcuts";
export { Abolish, Rule, Schema, AV };
6 changes: 1 addition & 5 deletions index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@
* Esm Custom File.
* This is not an autogenerated file.
*/
import AbolishEsm from "./index.esm-bundled.js";

const { Abolish, Schema, Rule } = AbolishEsm;

export { Abolish, Schema, Rule };
export * from "./index.esm-bundled.js";
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import Abolish from "./src/Abolish";
import { Rule, Schema } from "./src/functions";
import type { AV } from "./src/validator";

// Export Abolish and Rule generator
// Export
export { Abolish, Rule, Schema, AV };
export * from "./src/shortcuts";
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "abolish",
"version": "6.7.2",
"version": "6.7.3",
"description": "A javascript object validator.",
"main": "index.js",
"module": "index.esm.js",
Expand Down Expand Up @@ -34,8 +34,8 @@
"devDependencies": {
"@trapcode/benchmark": "^0.1.3",
"@types/lodash": "^4.17.5",
"@types/node": "^20.14.2",
"esbuild": "^0.21.5",
"@types/node": "^22.5.4",
"esbuild": "^0.23.1",
"japa": "^4.0.0",
"joi": "^17.13.1",
"lodash-es": "^4.17.21",
Expand All @@ -52,4 +52,4 @@
"object-validator",
"async-object-validator"
]
}
}
9 changes: 1 addition & 8 deletions src/Abolish.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// noinspection DuplicatedCode

import type {
$errorRule,
$errorsRule,
Expand Down Expand Up @@ -31,7 +29,6 @@ import {
CompiledValidator
} from "./Compiler";
import { assertType } from "./types-checker";
import { string } from "joi";

type Job = {
$name: string | false;
Expand Down Expand Up @@ -851,11 +848,7 @@ class Abolish {
* @param rules
* @param abolish
*/
static attempt<V = any>(
variable: V,
rules: AbolishRule | AbolishCompiled,
abolish?: typeof Abolish
): V {
static attempt<V = any>(variable: V, rules: AbolishRule | AbolishCompiled): V {
return new this().attempt(variable, rules);
}

Expand Down
37 changes: 36 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { $skipRule, AbolishInlineValidator } from "./types";
import type { $skipRule, AbolishInlineValidator, AbolishRule } from "./types";
import type { AbolishRuleTyped } from "./functions";

/**
* $inLine object generator
Expand Down Expand Up @@ -46,3 +47,37 @@ export function skipIfNotDefined(rule: string | Record<string, any> | any[]) {
* Optional - alias for skipIfNotDefined
*/
export const optional = skipIfNotDefined;

/**
* Required helper function.
* @example
* required("string")
* // is same as
* ["required", "string"]
*/
export function required(rule: AbolishRule): AbolishRuleTyped {
if (typeof rule === "string") {
// add `required` to rule
return "required|" + rule;
} else if (Array.isArray(rule)) {
// add `required` to rule
return ["required", ...rule];
} else if (typeof rule === "object") {
// add `required` to rule
return { required: true, ...rule };
} else {
throw new Error("Required: Invalid Rule");
}
}

/**
* Required helper function for typed rules.
* @param rule
* @example
* requiredT("string")
* // is same as
* ["required", "string"]
*/
export function requiredT(rule: AbolishRuleTyped): AbolishRuleTyped {
return required(rule);
}
45 changes: 45 additions & 0 deletions src/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Note:
* Only functions related to Abolish Should be added here.
*/
import type { AbolishSchema } from "./types";
import Abolish from "./Abolish";
import type { AbolishSchemaTyped } from "./functions";

/**
* Compile a schema object.
* @param schema
* @param abolish
*/
export function compileSchema(schema: AbolishSchema, abolish?: typeof Abolish) {
if (!abolish) abolish = Abolish;
return abolish.compileObject(schema);
}

/**
* Compile a rule object.
* @param rule
* @param abolish
*/
export function compileRule(rule: any, abolish?: typeof Abolish) {
if (!abolish) abolish = Abolish;
return abolish.compile(rule);
}

/**
* Compile a schema object typed.
* @param schema
* @param abolish
*/
export function compileSchemaT(schema: AbolishSchemaTyped, abolish?: typeof Abolish) {
return compileSchema(schema, abolish);
}

/**
* Compile a rule object typed.
* @param rule
* @param abolish
*/
export function compileRuleT(rule: any, abolish?: typeof Abolish) {
return compileRule(rule, abolish);
}
4 changes: 0 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ export interface AbolishAsyncValidator extends Omit<AbolishValidator, "isAsync">
}

export type AbolishRule = string | string[] | Record<string, any> | AbolishRule[];
// export type AbolishSchema<Keys extends string | number | symbol = string> = Record<
// Keys,
// AbolishRule
// >;
export type AbolishSchema<Keys extends Record<string, any> = Record<string, any>> = Record<
keyof Keys | string,
AbolishRule | boolean
Expand Down
1 change: 1 addition & 0 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export declare module AvailableValidators {
default: any;
required: boolean;
typeof: string | string[] | false;
type: string | string[] | false;
exact: string | boolean | number;
min: number;
max: number;
Expand Down
Loading

0 comments on commit c4699db

Please sign in to comment.