ciam-commons
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -6,2 +6,5 @@ export declare namespace Check { | ||
const strictFlagRegex: RegExp; | ||
class CheckError extends Error { | ||
constructor(message: string); | ||
} | ||
function objectId(id: string, message?: string): void; | ||
@@ -8,0 +11,0 @@ function discordId(id: string, message?: string): void; |
@@ -7,5 +7,12 @@ export var Check; | ||
Check.strictFlagRegex = /^[a-z0-9]+(\.[a-z0-9]+)*$/; | ||
class CheckError extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = 'CheckError'; | ||
} | ||
} | ||
Check.CheckError = CheckError; | ||
function objectId(id, message = 'Invalid objectId') { | ||
if (!id.match(Check.objectIdRegex)) | ||
throw new Error(`${message} "${id}"`); | ||
throw new CheckError(`${message} "${id}"`); | ||
} | ||
@@ -15,3 +22,3 @@ Check.objectId = objectId; | ||
if (!id.match(Check.discordIdRegex)) | ||
throw new Error(`${message} "${id}"`); | ||
throw new CheckError(`${message} "${id}"`); | ||
} | ||
@@ -21,3 +28,3 @@ Check.discordId = discordId; | ||
if (!flag.match(Check.flagRegex)) | ||
throw new Error(`${message} "${flag}"`); | ||
throw new CheckError(`${message} "${flag}"`); | ||
} | ||
@@ -27,3 +34,3 @@ Check.flag = flag; | ||
if (!flag.match(Check.strictFlagRegex)) | ||
throw new Error(`${message} "${flag}"`); | ||
throw new CheckError(`${message} "${flag}"`); | ||
} | ||
@@ -33,3 +40,3 @@ Check.strictFlag = strictFlag; | ||
if (obj.length == 0) | ||
throw new Error(`${name} cannot be empty`); | ||
throw new CheckError(`${name} cannot be empty`); | ||
} | ||
@@ -39,3 +46,3 @@ Check.notEmpty = notEmpty; | ||
if (n < min) | ||
throw new Error(`${name} cannot be less than ${min}`); | ||
throw new CheckError(`${name} cannot be less than ${min}`); | ||
} | ||
@@ -45,3 +52,3 @@ Check.min = min; | ||
if (n > max) | ||
throw new Error(`${name} cannot be greater than ${max}`); | ||
throw new CheckError(`${name} cannot be greater than ${max}`); | ||
} | ||
@@ -56,3 +63,3 @@ Check.max = max; | ||
if (!options.includes(obj)) | ||
throw new Error(`${name} must be one of ${options.toString()}`); | ||
throw new CheckError(`${name} must be one of ${options.toString()}`); | ||
} | ||
@@ -59,0 +66,0 @@ Check.oneOf = oneOf; |
{ | ||
"name": "ciam-commons", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "Common types and functions for CIAM", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -8,28 +8,35 @@ export namespace Check { | ||
export class CheckError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = 'CheckError'; | ||
} | ||
} | ||
export function objectId(id: string, message: string = 'Invalid objectId') { | ||
if (!id.match(objectIdRegex)) throw new Error(`${message} "${id}"`); | ||
if (!id.match(objectIdRegex)) throw new CheckError(`${message} "${id}"`); | ||
} | ||
export function discordId(id: string, message: string = 'Invalid discordId') { | ||
if (!id.match(discordIdRegex)) throw new Error(`${message} "${id}"`); | ||
if (!id.match(discordIdRegex)) throw new CheckError(`${message} "${id}"`); | ||
} | ||
export function flag(flag: string, message: string = 'Invalid permission flag') { | ||
if (!flag.match(flagRegex)) throw new Error(`${message} "${flag}"`); | ||
if (!flag.match(flagRegex)) throw new CheckError(`${message} "${flag}"`); | ||
} | ||
export function strictFlag(flag: string, message: string = 'Invalid strict permission flag') { | ||
if (!flag.match(strictFlagRegex)) throw new Error(`${message} "${flag}"`); | ||
if (!flag.match(strictFlagRegex)) throw new CheckError(`${message} "${flag}"`); | ||
} | ||
export function notEmpty(obj: string | Array<any>, name: string) { | ||
if (obj.length == 0) throw new Error(`${name} cannot be empty`); | ||
if (obj.length == 0) throw new CheckError(`${name} cannot be empty`); | ||
} | ||
export function min(n: number, min: number, name: string) { | ||
if (n < min) throw new Error(`${name} cannot be less than ${min}`); | ||
if (n < min) throw new CheckError(`${name} cannot be less than ${min}`); | ||
} | ||
export function max(n: number, max: number, name: string) { | ||
if (n > max) throw new Error(`${name} cannot be greater than ${max}`); | ||
if (n > max) throw new CheckError(`${name} cannot be greater than ${max}`); | ||
} | ||
@@ -43,3 +50,3 @@ | ||
export function oneOf<T>(obj: T, options: Array<T>, name: string) { | ||
if (!options.includes(obj)) throw new Error(`${name} must be one of ${options.toString()}`); | ||
if (!options.includes(obj)) throw new CheckError(`${name} must be one of ${options.toString()}`); | ||
} | ||
@@ -46,0 +53,0 @@ |
11596
280