0.1.1 • Published 5 years ago

mharj-filter-schema-util v0.1.1

Weekly downloads
3
License
LGPL-3.0-or-later
Repository
github
Last release
5 years ago

filter-schema-util

Schema type filtering utility

Basic example

interface IUser {
	_id?: string;
	email: string;
	passwordHash: string;
}

const userFilter: IFilterSchema<IUser> = {
	_id: {type: 'string', required: false},
	email: {type: 'string', required: true, forceCase: 'lower'},
	passwordHash: {type: 'string', required: true, hidden: true},
};

const output = filterSchema<IUser>(
	{
		_id: '3b5e3abc-9218-413f-8ef0-644656d1680f',
		email: 'Some.nasty@BUG.com',
		passwordHash: 'xxyyzzzzzwwwrrrr',
	},
	userFilter,
);

sub schema example

interface IUser {
	_id?: string;
	email: string;
	roles: IRole[];
}

interface IRole {
	_id?: string;
	name: string;
}

const roleFilter: IFilterSchema<IRole> = {
	_id: {type: 'string'},
	name: {type: 'string', required: true, forceCase: 'upper'},
};

const userFilter: IFilterSchema<IUser> = {
	_id: {type: 'string', required: false},
	email: {type: 'string', required: true, forceCase: 'lower'},
	roles: [{type: 'schema', required: true, filter: roleFilter}],
};

const output = filterSchema<IUser>(
	{
		_id: '3b5e3abc-9218-413f-8ef0-644656d1680f',
		email: 'Some.nasty@BUG.com',
		roles: [{name: 'sOmE_rOLe'}],
	},
	userFilter,
);

example to Wire conversion, (converts Date => msec);

interface IUser {
	_id?: string;
	email: string;
	lastLogin?: Date;
}

const userFilter: IFilterSchema<IUser> = {
	_id: {type: 'string', required: false},
	email: {type: 'string', required: true, forceCase: 'lower'},
	lastLogin: {type: 'date'},
};

const output = filterSchema<IUser>(
	{
		_id: '3b5e3abc-9218-413f-8ef0-644656d1680f',
		email: 'Some.nasty@BUG.com',
		lastLogin: new Date(),
	},
	userFilter,
	{toWire: true},
);

More examples from unit tests

./test/testFilters.ts

0.1.1

5 years ago

0.1.0

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago