ts-shallow-object-schema
v0.1.3
Published
Shallow type checks an object based on a schema and returns if the type is compatible with the schema
Downloads
48
Readme
TS Object Schema
Quick shallow object schema for strict TS types with 0 dependencies.
Usage
import * as assert from 'assert';
import validateObject from 'ts-shallow-object-schema';
const testData: unknown = {
testString: 'hello',
testInt: 5,
testObj: {
a: 'a',
b: 1,
},
testArr: [1, 2, 3],
};
interface ITest {
testString: string;
testInt: number;
testObj: object;
testArr: number[];
testOptional?: number;
}
const lengthValidator: (data: ITest) => boolean = (data) => data.testArr.length === 3;
const testSchema: Schema<ITest> = [{
key: 'testString',
type: 'string',
}, {
key: 'testInt',
type: 'number',
}, {
key: 'testObj',
type: 'object',
}, {
key: 'testArr',
type: 'array',
customValidator: lengthValidator,
}, {
key: 'testOptional',
type: 'number',
optional: true,
}];
assert.strictEquals(validateObject(testSchema, testData), true); // true
const otherSchema: Schema<ITest> = [{
key: 'missingKey',
type: 'string',
}, ...testSchema];
assert.strictEquals(validateObject(otherSchema, testData), false); // true