@tepez/joi-misc-extensions
v1.6.0
Published
Various hapijs/joi extensions
Downloads
220
Readme
joi-misc-extensions
Various hapijs/joi extensions
Install
npm install --save @tepez/joi-misc-extensions
| Version | Joi |
| ------- | ------------------------------------- |
| 0.4.x
| 14 joi
|
Usage
Extensions
import * as Joi from 'joi'
import {
numberLuhnExtension,
numberRegexExtension,
stringConvertible,
jsonObjectExtension,
jsonArrayExtension,
StringConvertibleExtended
} from '@tepez/joi-misc-extensions'
export const ExtendedJoi: typeof Joi & StringConvertibleExtended = Joi.extend(
numberLuhnExtension,
numberRegexExtension,
stringConvertible,
jsonObjectExtension,
jsonArrayExtension,
);
const luhnSchema = ExtendedJoi.number().luhn();
const regexSchema = ExtendedJoi.number().regex(/^\d{3}$/);
const stringConvertible = ExtendedJoi.stringConvertible();
ExtendedJoi.object().decodeJson().validate(`{ "foo": "bar" }`);
ExtendedJoi.array().decodeJson().validate(`[1,2,3]`);
Schemas
Validation of Typescript enums
- enumSchema
- enumValueOrAutoLabelSchema
- enumOrEmptySchema
- arrayEnumSchema
Utility functions for object().fork()
- makeRequired
- makeOptional
- makeForbidden
import * as Joi from 'joi'
import { makeForbidden } from '@tepez/joi-misc-extensions';
const obj = Joi.object().keys({
a: Joi.string(),
b: Joi.string(),
});
obj.fork([
a,
b,
], makeRequired)
obj.fork([
a,
b,
], makeOptional)
obj.fork([
a,
b,
], makeForbidden)
Misc
- typedValid
import * as Joi from 'joi'
import { typedValid } from '@tepez/joi-misc-extensions';
type Type =
| 'aaa'
| 'bbb'
| 'ccc';
typedValid<Type>('aaa', 'bbb', 'ccc');