diff --git a/js/util/validation.js b/js/util/validation.js index 9f8f7a9..e2e07bc 100644 --- a/js/util/validation.js +++ b/js/util/validation.js @@ -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; @@ -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; +}; diff --git a/test/util/validation.js b/test/util/validation.js index 5169cfa..9a40498 100644 --- a/test/util/validation.js +++ b/test/util/validation.js @@ -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!"), + 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; @@ -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); + + 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; + } + ); + }); +}); diff --git a/types/base/main.d.ts b/types/base/main.d.ts index 5f1d95f..fb628e5 100644 --- a/types/base/main.d.ts +++ b/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; diff --git a/types/data/model.d.ts b/types/data/model.d.ts index 631d3b8..2ff0ebb 100644 --- a/types/data/model.d.ts +++ b/types/data/model.d.ts @@ -1,16 +1,16 @@ export declare class Model { static niw(this: { new(): T }): T - static find(this: { new(): T }, params?: Record): T[] - static get(this: { new(): T }, params?: Record): T + static find(this: { new(): T }, params?: Record): T[] + static get(this: { new(): T }, params?: Record): T constructor(options?: { fill?: boolean }) - apply(this: T, model: Record): Promise + apply(this: T, model: Record): Promise save(this: T): Promise delete(this: T, options?: { preDelete?: boolean, postDelete?: boolean, - beforeCallbacks?: ((self: T, model: Record) => void)[], - afterCallbacks?: ((self: T, model: Record) => void)[] + beforeCallbacks?: ((self: T, model: Record) => void)[], + afterCallbacks?: ((self: T, model: Record) => void)[] }): Promise validate(): Promise diff --git a/types/util/assert.d.ts b/types/util/assert.d.ts index f6075fc..986be76 100644 --- a/types/util/assert.d.ts +++ b/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; diff --git a/types/util/config.d.ts b/types/util/config.d.ts index a42c91f..53a1d2d 100644 --- a/types/util/config.d.ts +++ b/types/util/config.d.ts @@ -1,2 +1,2 @@ -export declare function load(names?: Array, path?: string, encoding?: string, force?: boolean, ctx?: any): Promise; -export declare function conf(name: string, fallback?: any, cast?: string, ctx?: any): any; +export declare function load(names?: Array, path?: string, encoding?: string, force?: boolean, ctx?: unknown): Promise; +export declare function conf(name: string, fallback?: unknown, cast?: string, ctx?: unknown): unknown; diff --git a/types/util/data.d.ts b/types/util/data.d.ts index 6c27ec4..4bb7d0f 100644 --- a/types/util/data.d.ts +++ b/types/util/data.d.ts @@ -1,9 +1,9 @@ export declare function getObject( - params: Record, + params: Record, options: { alias?: boolean, page?: boolean, find?: boolean, norm?: boolean } -): Record; +): Record; diff --git a/types/util/validation.d.ts b/types/util/validation.d.ts index 193d442..4e4bde3 100644 --- a/types/util/validation.d.ts +++ b/types/util/validation.d.ts @@ -1,40 +1,63 @@ export declare function eq( valueC: T, message?: string -): (value?: T, ctx?: any) => boolean; +): (value?: T, ctx?: unknown) => boolean; export declare function gt( valueC: T, message?: string -): (value?: T, ctx?: any) => boolean; +): (value?: T, ctx?: unknown) => boolean; export declare function gte( 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( 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;