Skip to content

Commit

Permalink
feature(#285): Add Type.BOOLEAN
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWendelborn committed Sep 21, 2020
1 parent 208569b commit 5da96f6
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
70 changes: 70 additions & 0 deletions packages/vue-props-validators/source/modules/boolean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable no-magic-numbers */
import { vuePropsValidators } from '..'
import { REQUIRED } from '../constants'

import { TypeBoolean } from './boolean'

import { Type } from '.'

const BASE_BOOLEAN: TypeBoolean = {
default: () => null,
nullable: true,
type: Type.BOOLEAN,
}

test('boolean has correct type', () =>
expect(vuePropsValidators.create({ example: BASE_BOOLEAN })).toMatchObject({
example: { type: Boolean },
}))

test('boolean validator works', () => {
const { example } = vuePropsValidators.create({
example: BASE_BOOLEAN,
})

expect(example.validator('test')).toBeFalsy()
expect(example.validator(false)).toBeTruthy()
expect(example.validator(true)).toBeTruthy()
expect(example.validator(undefined)).toBeFalsy()
expect(example.validator(NaN)).toBeFalsy()
expect(example.validator(42)).toBeFalsy()
expect(example.validator(2.3)).toBeFalsy()
})

test('boolean (nullable: false)', () =>
expect(
vuePropsValidators
.create({
example: { ...BASE_BOOLEAN, nullable: false },
})
.example.validator(null),
).toBeFalsy())

test('boolean (nullable: true)', () =>
expect(
vuePropsValidators
.create({
example: { ...BASE_BOOLEAN, nullable: true },
})
.example.validator(null),
).toBeTruthy())

test('boolean (default)', () =>
expect(
vuePropsValidators
.create({
example: { ...BASE_BOOLEAN, default: () => null },
})
.example.default(),
).toBe(null))

test('boolean (required)', () =>
expect(
vuePropsValidators.create({
example: { ...BASE_BOOLEAN, default: REQUIRED },
}),
).toMatchObject({
example: {
required: true,
},
}))
20 changes: 20 additions & 0 deletions packages/vue-props-validators/source/modules/boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { baseValidator } from '../common/base-validator'
import { resolveDefault } from '../common/resolve-default'
import { Result } from '../types'

import { TypeBase, Type } from '.'

export type TypeBoolean = TypeBase & {
type: Type.BOOLEAN
}

export const createBoolean = <OPTION extends TypeBoolean>(
option: OPTION,
): Result<OPTION, BooleanConstructor> => ({
...resolveDefault(option),
type: Boolean,
validator: baseValidator(
option,
(value: unknown) => typeof value === 'boolean',
),
})
9 changes: 8 additions & 1 deletion packages/vue-props-validators/source/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { REQUIRED } from '../constants'

import { TypeBoolean } from './boolean'
import { TypeEnum } from './enum'
import { TypeFloat } from './float'
import { TypeInteger } from './integer'
Expand All @@ -11,10 +12,16 @@ export type TypeBase = {
}

export enum Type {
BOOLEAN = 'boolean',
ENUM = 'enum',
FLOAT = 'float',
INTEGER = 'integer',
STRING = 'string',
}

export type Option = TypeEnum | TypeFloat | TypeInteger | TypeString
export type Option =
| TypeBoolean
| TypeEnum
| TypeFloat
| TypeInteger
| TypeString
5 changes: 5 additions & 0 deletions packages/vue-props-validators/source/namespace.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Type, Option } from './modules'
import { createBoolean } from './modules/boolean'
import { createEnum } from './modules/enum'
import { createFloat } from './modules/float'
import { createInteger } from './modules/integer'
import { createString } from './modules/string'
import { Options, Result } from './types'

type ResultMap<OPTION extends Option> = {
[Type.BOOLEAN]: Result<OPTION, BooleanConstructor>
[Type.ENUM]: Result<OPTION, StringConstructor>
[Type.FLOAT]: Result<OPTION, NumberConstructor>
[Type.INTEGER]: Result<OPTION, NumberConstructor>
Expand All @@ -20,6 +22,9 @@ export const create = <PROPS extends Options>(
Object.fromEntries(
Object.entries(props).map(([prop, option]) => {
switch (option.type) {
case Type.BOOLEAN:
return [prop, createBoolean(option)]

case Type.ENUM:
return [prop, createEnum(option)]

Expand Down

0 comments on commit 5da96f6

Please sign in to comment.