Skip to content

Commit

Permalink
feat: add field option type: "null"
Browse files Browse the repository at this point in the history
  • Loading branch information
jalik committed Nov 19, 2024
1 parent 6056342 commit ef3a21d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type FieldType =
| 'boolean'
| 'function' // todo remove in v5
| 'integer'
| 'null'
| 'number'
| 'object'
| 'string'
Expand Down Expand Up @@ -499,7 +500,7 @@ export function checkRequired (required: boolean, value: any, label: string, pat
*/
export function checkType (
type: FieldType,
value: any[] | boolean | number | object | string | ((...args: any[]) => void),
value: any[] | boolean | null | number | object | string | ((...args: any[]) => void),
label: string,
path: string
): void {
Expand All @@ -525,6 +526,11 @@ export function checkType (
throw new FieldTypeError(label, type, path)
}
break
case 'null':
if (value !== null) {
throw new FieldTypeError(label, type, path)
}
break
case 'number':
if (typeof value !== 'number' || Number.isNaN(value)) {
throw new FieldTypeError(label, type, path)
Expand Down
15 changes: 10 additions & 5 deletions test/SchemaField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,19 @@ describe('SchemaField', () => {
})

describe('getType()', () => {
describe('with type: String', () => {
it('should return a string', () => {
const field = new SchemaField('field', { type: 'string' })
expect(field.getType()).toBe('string')
describe('with type defined', () => {
it('should return the type', () => {
expect(new SchemaField('field', { type: 'array' }).getType()).toBe('array')
expect(new SchemaField('field', { type: 'boolean' }).getType()).toBe('boolean')
expect(new SchemaField('field', { type: 'integer' }).getType()).toBe('integer')
expect(new SchemaField('field', { type: 'null' }).getType()).toBe('null')
expect(new SchemaField('field', { type: 'number' }).getType()).toBe('number')
expect(new SchemaField('field', { type: 'object' }).getType()).toBe('object')
expect(new SchemaField('field', { type: 'string' }).getType()).toBe('string')
})
})

describe('with type: undefined', () => {
describe('with type undefined', () => {
it('should return undefined', () => {
const field = new SchemaField('field', {})
expect(field.getType()).toBeUndefined()
Expand Down
24 changes: 22 additions & 2 deletions test/checks.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
* The MIT License (MIT)
* Copyright (c) 2023 Karl STEIN
* Copyright (c) 2024 Karl STEIN
*/

import { describe, expect, it } from '@jest/globals'
import { checkAllowed } from '../src/checks'
import { checkAllowed, checkType } from '../src/checks'
import FieldAllowedError from '../src/errors/FieldAllowedError'
import FieldTypeError from '../src/errors/FieldTypeError'

describe('checkAllowed', () => {
describe('with value allowed', () => {
Expand All @@ -23,3 +24,22 @@ describe('checkAllowed', () => {
})
})
})

describe('checkType', () => {
describe('with type = "null"', () => {
describe('with value = null', () => {
it('should not throw an Error', () => {
expect(() => {
checkType('null', null, 'field', 'field')
}).not.toThrow()
})
})
describe('with value !== null', () => {
it('should throw a FieldTypeError', () => {
expect(() => {
checkType('null', 'null', 'field', 'field')
}).toThrow(FieldTypeError)
})
})
})
})

0 comments on commit ef3a21d

Please sign in to comment.