Skip to content

Commit

Permalink
fix: minor type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Oct 27, 2023
1 parent 43956c3 commit 593df23
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Validator<T extends object> {
];

while (stack.length !== 0) {
const { parentKey, item, config } = stack.shift();
const { parentKey, item, config } = stack.shift() as Validator.StackItem;

for (const key in config) {
const configItem = config[key];
Expand Down Expand Up @@ -82,7 +82,7 @@ export class Validator<T extends object> {
}

export namespace Validator {
export type Fn<T> = (value: T) => string;
export type Fn<T> = (value: T) => string | void;

export type Config<T extends object> = {
[key in keyof T]?: T[key] extends Array<object>
Expand Down
20 changes: 10 additions & 10 deletions src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Validator } from '..';
* @param length the length to compare to
* @returns a validator function matching the criteria
*/
export function isLength(length): Validator.Fn<any[] | string> {
return (value) => {
export function isLength(length: number): Validator.Fn<any[] | string> {
return (value): string | void => {
if (value.length !== length) return `has length not equal to "${length}"`;
};
}
Expand All @@ -16,8 +16,8 @@ export function isLength(length): Validator.Fn<any[] | string> {
* @param length the length to compare to
* @returns a validator function matching the criteria
*/
export function isNotLength(length): Validator.Fn<any[] | string> {
return (value) => {
export function isNotLength(length: number): Validator.Fn<any[] | string> {
return (value): string | void => {
if (value.length === length) return `has length equal to "${length}"`;
};
}
Expand All @@ -27,8 +27,8 @@ export function isNotLength(length): Validator.Fn<any[] | string> {
* @param length the maximum length (inclusive)
* @returns a validator function matching the criteria
*/
export function isWithinLength(length): Validator.Fn<any[] | string> {
return (value) => {
export function isWithinLength(length: number): Validator.Fn<any[] | string> {
return (value): string | void => {
if (value.length > length) return `exceeds max length of "${length}"`;
};
}
Expand All @@ -38,7 +38,7 @@ export function isWithinLength(length): Validator.Fn<any[] | string> {
* @param thing the thing to validate
* @returns why the validator failed
*/
export function isDefined(thing: any): string {
export function isDefined(thing: any): string | void {
if ([undefined, null].includes(thing)) return 'is not defined';
}

Expand All @@ -48,7 +48,7 @@ export function isDefined(thing: any): string {
* @returns a validator function matching the criteria
*/
export function isEqualTo<T>(expectedValue: T): Validator.Fn<T> {
return (value: T): string => {
return (value: T): string | void => {
if (value !== expectedValue) return `is not equal to "${expectedValue}"`;
};
}
Expand All @@ -59,7 +59,7 @@ export function isEqualTo<T>(expectedValue: T): Validator.Fn<T> {
* @returns a validator function matching the criteria
*/
export function isNotEqualTo<T>(expectedValue: T): Validator.Fn<T> {
return (value: T): string => {
return (value: T): string | void => {
if (value === expectedValue) return `is equal to "${expectedValue}"`;
};
}
Expand All @@ -70,7 +70,7 @@ export function isNotEqualTo<T>(expectedValue: T): Validator.Fn<T> {
* @returns a validator function matching the criteria
*/
export function isAny<T>(...expectedValues: T[]): Validator.Fn<T> {
return (value: T): string => {
return (value: T): string | void => {
if (!expectedValues.includes(value)) return `is not equal to "${expectedValues.join(', ')}"`;
};
}
Expand Down
14 changes: 7 additions & 7 deletions src/validators/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isNotEqualTo } from './index';
* @returns the reason for the number validator failing
*/
export function coerceNumber(validator: Validator.Fn<number>): Validator.Fn<string | number> {
return (value: string | number) => {
return (value: string | number): string | void => {
return validator(typeof value === 'string' ? Number(value) : value);
};
}
Expand All @@ -19,7 +19,7 @@ export function coerceNumber(validator: Validator.Fn<number>): Validator.Fn<stri
* @returns a validator function matching the criteria
*/
export function isBetweenInclusive(min: number, max: number): Validator.Fn<number> {
return (value: number): string => {
return (value: number): string | void => {
return isGreaterThanOrEqualTo(min)(value) ?? isLessThanOrEqualTo(max)(value);
};
}
Expand All @@ -31,7 +31,7 @@ export function isBetweenInclusive(min: number, max: number): Validator.Fn<numbe
* @returns a validator function matching the criteria
*/
export function isBetween(min: number, max: number): Validator.Fn<number> {
return (value: number): string => {
return (value: number): string | void => {
return isGreaterThan(min)(value) ?? isLessThan(max)(value);
};
}
Expand All @@ -42,7 +42,7 @@ export function isBetween(min: number, max: number): Validator.Fn<number> {
* @returns a validator function matching the criteria
*/
export function isGreaterThanOrEqualTo(min: number): Validator.Fn<number> {
return (value: number): string => {
return (value: number): string | void => {
if (value < min) return `is less than "${min}"`;
};
}
Expand All @@ -53,7 +53,7 @@ export function isGreaterThanOrEqualTo(min: number): Validator.Fn<number> {
* @returns a validator function matching the criteria
*/
export function isGreaterThan(min: number): Validator.Fn<number> {
return (value: number): string => {
return (value: number): string | void => {
return isNotEqualTo(min)(value) ?? isGreaterThanOrEqualTo(min)(value);
};
}
Expand All @@ -64,7 +64,7 @@ export function isGreaterThan(min: number): Validator.Fn<number> {
* @returns a validator function matching the criteria
*/
export function isLessThanOrEqualTo(max: number): Validator.Fn<number> {
return (value: number): string => {
return (value: number): string | void => {
if (value > max) return `is greater than "${max}"`;
};
}
Expand All @@ -75,7 +75,7 @@ export function isLessThanOrEqualTo(max: number): Validator.Fn<number> {
* @returns a validator function matching the criteria
*/
export function isLessThan(max: number): Validator.Fn<number> {
return (value: number): string => {
return (value: number): string | void => {
return isNotEqualTo(max)(value) ?? isLessThanOrEqualTo(max)(value);
};
}
6 changes: 3 additions & 3 deletions src/validators/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param thing the string to validate
* @returns why the validator failed
*/
export function isAlpha(thing: string): string {
export function isAlpha(thing: string): string | void {
if (!/[A-z ]+/.test(thing)) return 'includes non alpha characters';
}

Expand All @@ -12,7 +12,7 @@ export function isAlpha(thing: string): string {
* @param thing the string to validate
* @returns why the validator failed
*/
export function isAlphaNumeric(thing: string): string {
export function isAlphaNumeric(thing: string): string | void {
if (!/[A-z \d]+/.test(thing)) return 'includes non alpha-numeric characters';
}

Expand All @@ -21,6 +21,6 @@ export function isAlphaNumeric(thing: string): string {
* @param thing the string to validate
* @returns why the validator failed
*/
export function isNumeric(thing: string): string {
export function isNumeric(thing: string): string | void {
if (!/\d+/.test(thing)) return 'includes non numeric characters';
}

0 comments on commit 593df23

Please sign in to comment.