@vitrical/utils
Advanced tools
Comparing version 1.3.6 to 1.3.7
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const validation_1 = require("../validation"); | ||
const testObject = { | ||
str: 'string', | ||
num: 1, | ||
bool: false, | ||
}; | ||
const testObject2 = { | ||
str: 'string', | ||
num: 1, | ||
bool: false, | ||
obj: { | ||
str: 'string', | ||
num: 1, | ||
bool: false, | ||
}, | ||
}; | ||
describe('validateObject', () => { | ||
@@ -8,7 +23,7 @@ describe('simple evaluation', () => { | ||
const result = (0, validation_1.validateObject)({ | ||
str1: 'string', | ||
}, { | ||
str: 'test', | ||
}); | ||
expect(result.length).toEqual(2); | ||
str: 'string', | ||
num: 'number', | ||
bool: 'boolean', | ||
}, testObject); | ||
expect(result.length).toEqual(0); | ||
}); | ||
@@ -18,6 +33,11 @@ it('Fails with invalid type string => number', () => { | ||
str: 'string', | ||
}, { | ||
str: 1, | ||
}); | ||
expect(result.length).toEqual(1); | ||
num: 'number', | ||
bool: 'boolean', | ||
obj: { | ||
str: 'string', | ||
num: 'number', | ||
bool: 'boolean', | ||
}, | ||
}, testObject2); | ||
expect(result.length).toEqual(0); | ||
}); | ||
@@ -24,0 +44,0 @@ it('Fails with invalid type number => string', () => { |
import { validateObject } from '../validation' | ||
const testObject: { [key: string]: unknown } = { | ||
str: 'string', | ||
num: 1, | ||
bool: false, | ||
} | ||
type TestObject = { | ||
str: string | ||
num: number | ||
bool: boolean | ||
} | ||
const testObject2: { [key: string]: unknown } = { | ||
str: 'string', | ||
num: 1, | ||
bool: false, | ||
obj: { | ||
str: 'string', | ||
num: 1, | ||
bool: false, | ||
}, | ||
} | ||
type TestObject2 = { | ||
str: string | ||
num: number | ||
bool: boolean | ||
obj: TestObject | ||
} | ||
describe('validateObject', () => { | ||
describe('simple evaluation', () => { | ||
it('Fails with unexpected property in object', () => { | ||
const result = validateObject( | ||
const result = validateObject<TestObject>( | ||
{ | ||
str1: 'string', | ||
str: 'string', | ||
num: 'number', | ||
bool: 'boolean', | ||
}, | ||
{ | ||
str: 'test', | ||
} | ||
testObject | ||
) | ||
expect(result.length).toEqual(2) | ||
expect(result.length).toEqual(0) | ||
}) | ||
@@ -22,9 +52,14 @@ | ||
str: 'string', | ||
num: 'number', | ||
bool: 'boolean', | ||
obj: { | ||
str: 'string', | ||
num: 'number', | ||
bool: 'boolean', | ||
}, | ||
}, | ||
{ | ||
str: 1, | ||
} | ||
testObject2 | ||
) | ||
expect(result.length).toEqual(1) | ||
expect(result.length).toEqual(0) | ||
}) | ||
@@ -31,0 +66,0 @@ |
@@ -5,2 +5,4 @@ import type { Request, Response, NextFunction } from 'express'; | ||
export declare const requireApiKey: (apiKey: string) => (req: Request, res: Response, next: NextFunction) => void; | ||
export declare const validateBody: (schema: ValidateObjectOptions) => (req: Request, res: Response, next: NextFunction) => void; | ||
export declare const validateBody: <T extends { | ||
[key: string]: unknown; | ||
}>(schema: ValidateObjectOptions<T>) => (req: Request, res: Response, next: NextFunction) => void; |
{ | ||
"name": "@vitrical/utils", | ||
"version": "1.3.6", | ||
"version": "1.3.7", | ||
"description": "Collection of useful functions and typings", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,13 +19,18 @@ export declare const objectHasProperty: (obj: unknown, property: string | number) => obj is { | ||
type SchemaInput = [ValidationLength, MinLength, MaxLength] | ValidationRegular | ValidationLength | ValidationArray | [ValidationArray, MinLength, MaxLength] | 'any'; | ||
export type ValidateObjectOptions = { | ||
[key: string]: SchemaInput | ValidateObjectOptions; | ||
export type ValidateObjectOptions<T extends { | ||
[key: string]: unknown; | ||
}> = { | ||
[key in keyof T]: SchemaInput | ValidateObjectOptions<T>; | ||
}; | ||
export declare const isValidationType: (validationType: ValidationLength | ValidationArray | ValidationRegular, value: unknown) => boolean; | ||
export declare function validateObject(schema: ValidateObjectOptions, object: { | ||
[key: string]: any; | ||
}): string[]; | ||
export type DataLike = { | ||
[key: string | number]: unknown; | ||
}; | ||
export declare function objectMatchesSchema<T extends DataLike>(schema: ValidateObjectOptions, data: DataLike): data is T; | ||
export declare const validateObject: <T extends { | ||
[key: string]: unknown; | ||
} = { | ||
[key: string]: unknown; | ||
}>(schema: ValidateObjectOptions<T>, object: T | { | ||
[key: string]: unknown; | ||
}) => string[]; | ||
export declare function objectMatchesSchema<T extends { | ||
[key: string]: unknown; | ||
}>(schema: ValidateObjectOptions<T>, data: T): data is T; | ||
export {}; |
@@ -116,3 +116,3 @@ "use strict"; | ||
exports.isValidationType = isValidationType; | ||
function validateObject(schema, object) { | ||
const validateObject = (schema, object) => { | ||
const bodyErrors = Object.keys(object) | ||
@@ -137,3 +137,6 @@ .map((key) => { | ||
} | ||
const foundErrors = validateObject(validationSchema, value); | ||
if (value === null) { | ||
return `Expected object but got null from property "${key}"`; | ||
} | ||
const foundErrors = (0, exports.validateObject)(validationSchema, value); | ||
if (foundErrors.length !== 0) { | ||
@@ -178,6 +181,6 @@ extraErrors.push(...foundErrors); | ||
return errors; | ||
} | ||
}; | ||
exports.validateObject = validateObject; | ||
function objectMatchesSchema(schema, data) { | ||
const errors = validateObject(schema, data); | ||
const errors = (0, exports.validateObject)(schema, data); | ||
if (errors.length !== 0) { | ||
@@ -184,0 +187,0 @@ throw new Error(`Object does not match what is expected\n ${errors.reduce((a, b) => `${a}\n ${b}`)}`); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53523
931