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

feat: new string validations #26

Merged
merged 3 commits into from Jan 12, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions js/util/validation.js
Expand Up @@ -64,6 +64,26 @@ export const isIn = function(valueC, message = "Value must be one of: %{1}") {
return validation;
};

export const isUpper = function(message = "Value contains lower cased characters") {
const validation = (value, ctx) => {
if (value === null) return true;
if (value === "") return true;
if (value === value.toUpperCase()) return true;
throw new ValidationError(message);
};
return validation;
};

export const isLower = function(message = "Value contains upper cased characters") {
const validation = (value, ctx) => {
if (value === null) return true;
if (value === "") return true;
if (value === value.toLowerCase()) return true;
throw new ValidationError(message);
};
return validation;
};

export const isSimple = function(message = "Value contains invalid characters") {
const validation = (value, ctx) => {
if (value === null) return true;
Expand Down Expand Up @@ -103,3 +123,30 @@ export const isRegex = function(regex, message = "Value has incorrect format") {
};
return validation;
};

export const stringGt = function(valueC, message = "Must be larger than %{1} characters") {
const validation = (value, ctx) => {
if (value === null) return true;
if (value.length > valueC) return true;
throw new ValidationError(message.replace("%{1}", String(valueC)));
};
return validation;
};

export const stringLt = function(valueC, message = "Must be smaller than %{1} characters") {
const validation = (value, ctx) => {
if (value === null) return true;
if (value.length < valueC) return true;
throw new ValidationError(message.replace("%{1}", String(valueC)));
};
return validation;
};

export const stringEq = function(valueC, message = "Must be exactly %{1} characters") {
const validation = (value, ctx) => {
if (value === null) return true;
if (value.length === valueC) return true;
throw new ValidationError(message.replace("%{1}", String(valueC)));
};
return validation;
};
117 changes: 117 additions & 0 deletions test/util/validation.js
Expand Up @@ -124,6 +124,54 @@ describe("#isIn()", function() {
});
});

describe("#isUpper()", function() {
it("should verify basic is simple conditions", () => {
let result;

result = yonius.isUpper()("HELLO");
assert.strictEqual(result, true);

result = yonius.isUpper()("HELLO.WORLD");
assert.strictEqual(result, true);

result = yonius.isUpper()(null);
assert.strictEqual(result, true);

assert.throws(
() => yonius.isUpper()("illegal!"),
err => {
assert.strictEqual(err instanceof yonius.ValidationError, true);
assert.strictEqual(err.message, "Value contains lower cased characters");
return true;
}
);
});
});

describe("#isLower()", function() {
it("should verify basic is simple conditions", () => {
let result;

result = yonius.isLower()("hello");
assert.strictEqual(result, true);

result = yonius.isLower()("hello.hello");
assert.strictEqual(result, true);

result = yonius.isLower()(null);
assert.strictEqual(result, true);

assert.throws(
() => yonius.isLower()("ILLEGAL!"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep using the typical hello and hello world string "HELLO" example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken from

() => yonius.isSimple()("illegal!"),

err => {
assert.strictEqual(err instanceof yonius.ValidationError, true);
assert.strictEqual(err.message, "Value contains upper cased characters");
return true;
}
);
});
});

describe("#isSimple()", function() {
it("should verify basic is simple conditions", () => {
let result;
Expand Down Expand Up @@ -213,3 +261,72 @@ describe("#isRegex()", function() {
);
});
});

describe("#stringGt()", function() {
it("should verify basic greater than operations", () => {
let result;

result = yonius.stringGt(2)("hel");
assert.strictEqual(result, true);

result = yonius.stringGt(4)("hello");
assert.strictEqual(result, true);

result = yonius.stringGt(4)(null);
assert.strictEqual(result, true);

assert.throws(
() => yonius.stringGt(4)("hell"),
err => {
assert.strictEqual(err instanceof yonius.ValidationError, true);
assert.strictEqual(err.message, "Must be larger than 4 characters");
return true;
}
);
});
});

describe("#stringLt()", function() {
it("should verify basic greater than operations", () => {
let result;

result = yonius.stringLt(2)("h");
assert.strictEqual(result, true);

result = yonius.stringLt(4)("hel");
assert.strictEqual(result, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


result = yonius.stringLt(4)(null);
assert.strictEqual(result, true);

assert.throws(
() => yonius.stringLt(4)("hell"),
err => {
assert.strictEqual(err instanceof yonius.ValidationError, true);
assert.strictEqual(err.message, "Must be smaller than 4 characters");
return true;
}
);
});
});

describe("#stringEq()", function() {
it("should verify basic greater than operations", () => {
let result;

result = yonius.stringEq(2)("he");
assert.strictEqual(result, true);

result = yonius.stringEq(4)(null);
assert.strictEqual(result, true);

assert.throws(
() => yonius.stringEq(4)("hel"),
err => {
assert.strictEqual(err instanceof yonius.ValidationError, true);
assert.strictEqual(err.message, "Must be exactly 4 characters");
return true;
}
);
});
});
4 changes: 2 additions & 2 deletions types/base/main.d.ts
@@ -1,3 +1,3 @@
export declare function register(name: string, value: any): void;
export declare function register(name: string, value: unknown): void;
export declare function unregister(name: string): void;
export declare function request(name: string): any;
export declare function request(name: string): unknown;
10 changes: 5 additions & 5 deletions types/data/model.d.ts
@@ -1,16 +1,16 @@
export declare class Model {
static niw<T = Model>(this: { new(): T }): T
static find<T = Model>(this: { new(): T }, params?: Record<string, any>): T[]
static get<T = Model>(this: { new(): T }, params?: Record<string, any>): T
static find<T = Model>(this: { new(): T }, params?: Record<string, unknown>): T[]
static get<T = Model>(this: { new(): T }, params?: Record<string, unknown>): T

constructor(options?: { fill?: boolean })
apply<T = Model>(this: T, model: Record<string, any>): Promise<T>
apply<T = Model>(this: T, model: Record<string, unknown>): Promise<T>
save<T = Model>(this: T): Promise<T>
delete<T = Model>(this: T, options?: {
preDelete?: boolean,
postDelete?: boolean,
beforeCallbacks?: ((self: T, model: Record<string, any>) => void)[],
afterCallbacks?: ((self: T, model: Record<string, any>) => void)[]
beforeCallbacks?: ((self: T, model: Record<string, unknown>) => void)[],
afterCallbacks?: ((self: T, model: Record<string, unknown>) => void)[]
}): Promise<T>

validate(): Promise<void>
Expand Down
2 changes: 1 addition & 1 deletion types/util/assert.d.ts
@@ -1 +1 @@
export declare function verify(condition: boolean, message?: string, code?: number, exception?: any, kwargs?: any): void;
export declare function verify(condition: boolean, message?: string, code?: number, exception?: unknown, kwargs?: unknown): void;
4 changes: 2 additions & 2 deletions types/util/config.d.ts
@@ -1,2 +1,2 @@
export declare function load(names?: Array<string>, path?: string, encoding?: string, force?: boolean, ctx?: any): Promise<void>;
export declare function conf(name: string, fallback?: any, cast?: string, ctx?: any): any;
export declare function load(names?: Array<string>, path?: string, encoding?: string, force?: boolean, ctx?: unknown): Promise<void>;
export declare function conf(name: string, fallback?: unknown, cast?: string, ctx?: unknown): unknown;
4 changes: 2 additions & 2 deletions types/util/data.d.ts
@@ -1,9 +1,9 @@
export declare function getObject(
params: Record<string, any>,
params: Record<string, unknown>,
options: {
alias?: boolean,
page?: boolean,
find?: boolean,
norm?: boolean
}
): Record<string, any>;
): Record<string, unknown>;
41 changes: 32 additions & 9 deletions types/util/validation.d.ts
@@ -1,40 +1,63 @@
export declare function eq<T>(
valueC: T,
message?: string
): (value?: T, ctx?: any) => boolean;
): (value?: T, ctx?: unknown) => boolean;

export declare function gt<T>(
valueC: T,
message?: string
): (value?: T, ctx?: any) => boolean;
): (value?: T, ctx?: unknown) => boolean;

export declare function gte<T>(
valueC: T,
message?: string
): (value?: T, ctx?: any) => boolean;
): (value?: T, ctx?: unknown) => boolean;

export declare function notEmpty(
message?: string
): (value?: string | unknown[], ctx?: any) => boolean;
): (value?: string | unknown[], ctx?: unknown) => boolean;

export declare function isIn<T>(
valueC: T[],
message?: string
): (value?: T, ctx?: any) => boolean;
): (value?: T, ctx?: unknown) => boolean;

export declare function isUpper(
message?: string
): (value?: string, ctx?: unknown) => boolean;

export declare function isLower(
message?: string
): (value?: string, ctx?: unknown) => boolean;

export declare function isSimple(
message?: string
): (value?: string, ctx?: any) => boolean;
): (value?: string, ctx?: unknown) => boolean;

export declare function isEmail(
message?: string
): (value?: string, ctx?: any) => boolean;
): (value?: string, ctx?: unknown) => boolean;

export declare function isUrl(
message?: string
): (value?: string, ctx?: any) => boolean;
): (value?: string, ctx?: unknown) => boolean;

export declare function isRegex(
regex: string,
message?: string
): (value?: string, ctx?: any) => boolean;
): (value?: string, ctx?: unknown) => boolean;

export declare function stringGt(
valueC: number,
message?: string
): (value?: string, ctx?: unknown) => boolean;

export declare function stringLt(
valueC: number,
message?: string
): (value?: string, ctx?: unknown) => boolean;

export declare function stringEq(
valueC: number,
message?: string
): (value?: string, ctx?: unknown) => boolean;