Skip to content

Commit

Permalink
feat: enable type validator to validate complex, user given types
Browse files Browse the repository at this point in the history
  • Loading branch information
IamSebastianDev committed Dec 8, 2022
1 parent 972acf5 commit 56ccb01
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
65 changes: 59 additions & 6 deletions src/lib/Validators/IsType.validator.ts
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.`);
};
};
8 changes: 8 additions & 0 deletions src/types/TypeValidatorInit.d.ts
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;
};
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export type { Primitive } from './Primitive';
export type { ArrayValidatorInit } from './ArrayValidatorInit';
export type { IntegerValidatorInit } from './IntegerValidatorInit';
export type { TextValidatorInit } from './TextValidatorInit';
export type { TypeValidatorInit } from './TypeValidatorInit';

0 comments on commit 56ccb01

Please sign in to comment.