-
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.
feat: enable type validator to validate complex, user given types
- Loading branch information
1 parent
972acf5
commit 56ccb01
Showing
3 changed files
with
68 additions
and
6 deletions.
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 |
---|---|---|
@@ -1,14 +1,67 @@ | ||
/** @format */ | ||
|
||
import { FlotsamValidationError } from '../../utils'; | ||
import type { Primitive } from '../../types'; | ||
import type { Primitive, TypeValidatorInit, ValidatorFunction } from '../../types'; | ||
|
||
export const IsType = (proposedType: Primitive) => { | ||
const isPrimitive = (value: unknown): value is Primitive => { | ||
return ( | ||
typeof value === 'string' && | ||
['string', 'number', 'object', 'function', 'bigint', 'symbol', 'undefined', 'boolean'].includes(value) | ||
); | ||
}; | ||
|
||
/** | ||
* @description | ||
* Validator to check a given value to be inserted or updated for being an Array. The Validator is being constructed | ||
* by passing an object containing a `type` property. The property can either contain a `string` describing the expected | ||
* type or a function to evaluate the property to be a certain type. | ||
* | ||
* ----- | ||
*@example | ||
* ```ts | ||
* import { Flotsam } from "flotsam/db"; | ||
* import { NotNull, IsType } from "flotsam/validators"; | ||
* | ||
* const authors = ['J.R.R Tolkien', 'Rebecca Gable', 'Douglas Adams'] | ||
* | ||
* const collection = await db.collect<{ title: string, author: string[] }>('collection', { | ||
* validate: { | ||
* title: [NotNull, IsType({ type: "string" })], | ||
* author: [ | ||
* NotNull, | ||
* IsType({ type: (value) => authors.includes(value)}) | ||
* ] | ||
* } | ||
* }); | ||
* | ||
* ``` | ||
* ----- | ||
* | ||
* @param { TypeValidatorInit } validationRules | ||
* @returns { ValidatorFunction } a ValidatorFunction to validate (complex) Types | ||
*/ | ||
|
||
export const IsType = (validationRules: TypeValidatorInit): ValidatorFunction => { | ||
const { type } = validationRules; | ||
return (value: unknown, propertyName: string) => { | ||
if (typeof value === proposedType) return true; | ||
if (typeof type === 'function') { | ||
if (type(value, propertyName)) { | ||
return true; | ||
} | ||
|
||
throw new FlotsamValidationError(`Property '${propertyName}' is not of the expected type.`); | ||
} | ||
|
||
if (isPrimitive(type)) { | ||
if (typeof value === type) { | ||
return true; | ||
} | ||
|
||
throw new FlotsamValidationError( | ||
`Expected property '${propertyName}' to be of type '${type}'. Found property to be of type '${typeof value}' instead.` | ||
); | ||
} | ||
|
||
throw new FlotsamValidationError( | ||
`Expected property '${propertyName}' to be of type '${proposedType}'. Found property to be of type '${typeof value}' instead.` | ||
); | ||
throw new FlotsamValidationError(`Property ${propertyName} could not be evaluated.`); | ||
}; | ||
}; |
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,8 @@ | ||
/** @format */ | ||
|
||
import { Primitive } from './Primitive'; | ||
import { ValidatorFunction } from './ValidatorFunction'; | ||
|
||
export type TypeValidatorInit = { | ||
type: Primitive | ValidatorFunction; | ||
}; |
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