Skip to content

Commit ef3a21d

Browse files
committed
feat: add field option type: "null"
1 parent 6056342 commit ef3a21d

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

src/checks.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export type FieldType =
5555
| 'boolean'
5656
| 'function' // todo remove in v5
5757
| 'integer'
58+
| 'null'
5859
| 'number'
5960
| 'object'
6061
| 'string'
@@ -499,7 +500,7 @@ export function checkRequired (required: boolean, value: any, label: string, pat
499500
*/
500501
export function checkType (
501502
type: FieldType,
502-
value: any[] | boolean | number | object | string | ((...args: any[]) => void),
503+
value: any[] | boolean | null | number | object | string | ((...args: any[]) => void),
503504
label: string,
504505
path: string
505506
): void {
@@ -525,6 +526,11 @@ export function checkType (
525526
throw new FieldTypeError(label, type, path)
526527
}
527528
break
529+
case 'null':
530+
if (value !== null) {
531+
throw new FieldTypeError(label, type, path)
532+
}
533+
break
528534
case 'number':
529535
if (typeof value !== 'number' || Number.isNaN(value)) {
530536
throw new FieldTypeError(label, type, path)

test/SchemaField.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,19 @@ describe('SchemaField', () => {
267267
})
268268

269269
describe('getType()', () => {
270-
describe('with type: String', () => {
271-
it('should return a string', () => {
272-
const field = new SchemaField('field', { type: 'string' })
273-
expect(field.getType()).toBe('string')
270+
describe('with type defined', () => {
271+
it('should return the type', () => {
272+
expect(new SchemaField('field', { type: 'array' }).getType()).toBe('array')
273+
expect(new SchemaField('field', { type: 'boolean' }).getType()).toBe('boolean')
274+
expect(new SchemaField('field', { type: 'integer' }).getType()).toBe('integer')
275+
expect(new SchemaField('field', { type: 'null' }).getType()).toBe('null')
276+
expect(new SchemaField('field', { type: 'number' }).getType()).toBe('number')
277+
expect(new SchemaField('field', { type: 'object' }).getType()).toBe('object')
278+
expect(new SchemaField('field', { type: 'string' }).getType()).toBe('string')
274279
})
275280
})
276281

277-
describe('with type: undefined', () => {
282+
describe('with type undefined', () => {
278283
it('should return undefined', () => {
279284
const field = new SchemaField('field', {})
280285
expect(field.getType()).toBeUndefined()

test/checks.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
22
* The MIT License (MIT)
3-
* Copyright (c) 2023 Karl STEIN
3+
* Copyright (c) 2024 Karl STEIN
44
*/
55

66
import { describe, expect, it } from '@jest/globals'
7-
import { checkAllowed } from '../src/checks'
7+
import { checkAllowed, checkType } from '../src/checks'
88
import FieldAllowedError from '../src/errors/FieldAllowedError'
9+
import FieldTypeError from '../src/errors/FieldTypeError'
910

1011
describe('checkAllowed', () => {
1112
describe('with value allowed', () => {
@@ -23,3 +24,22 @@ describe('checkAllowed', () => {
2324
})
2425
})
2526
})
27+
28+
describe('checkType', () => {
29+
describe('with type = "null"', () => {
30+
describe('with value = null', () => {
31+
it('should not throw an Error', () => {
32+
expect(() => {
33+
checkType('null', null, 'field', 'field')
34+
}).not.toThrow()
35+
})
36+
})
37+
describe('with value !== null', () => {
38+
it('should throw a FieldTypeError', () => {
39+
expect(() => {
40+
checkType('null', 'null', 'field', 'field')
41+
}).toThrow(FieldTypeError)
42+
})
43+
})
44+
})
45+
})

0 commit comments

Comments
 (0)