-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b464576
commit c4b909f
Showing
4 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** @format */ | ||
|
||
import { ArrayValidatorInit, ValidatorFunction } from '../../types'; | ||
import { FlotsamValidationError } from '../../utils'; | ||
|
||
export const IsArray = (validationRules?: ArrayValidatorInit): ValidatorFunction => { | ||
const { min, max, items } = validationRules || {}; | ||
return (value: unknown, propertyName: string) => { | ||
if (!Array.isArray(value)) { | ||
throw new FlotsamValidationError( | ||
`Expected property '${propertyName}' to be of type 'Array'. Found property to be of type '${typeof value}' instead.` | ||
); | ||
} | ||
|
||
const { length } = value; | ||
|
||
if (min && length < min) { | ||
throw new FlotsamValidationError( | ||
`Expected property '${propertyName}' to be at least of length '${min}'. Found length '${length}' instead.` | ||
); | ||
} | ||
|
||
if (max && length > max) { | ||
throw new FlotsamValidationError( | ||
`Expected property '${propertyName}' to be at max of length '${max}'. Found length '${length}' instead.` | ||
); | ||
} | ||
|
||
if (items && !value.every((item) => typeof item === items)) { | ||
throw new FlotsamValidationError( | ||
`Expected property '${propertyName}' to contain only elements of type ${items}` | ||
); | ||
} | ||
|
||
return true; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** @format */ | ||
|
||
import type { Primitive } from './Primitive'; | ||
|
||
export type ArrayValidatorInit = { | ||
min?: number; | ||
max?: number; | ||
items?: Primitive; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
/** @format */ | ||
|
||
type Primitive = 'string' | 'number' | 'object' | 'function' | 'bigint' | 'symbol' | 'undefined' | 'boolean'; | ||
export type Primitive = 'string' | 'number' | 'object' | 'function' | 'bigint' | 'symbol' | 'undefined' | 'boolean'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters