1.6.0 • Published 4 months ago

@tepez/joi-misc-extensions v1.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

joi-misc-extensions

Various hapijs/joi extensions

npm version Build Status

Install

npm install --save @tepez/joi-misc-extensions
VersionJoi
0.4.x14 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');
1.6.0

4 months ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

4 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.0

5 years ago