-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
62 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,37 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { number, object, string } from '../../schemas/index.ts'; | ||
import { parse } from '../parse/index.ts'; | ||
import { number, object, optional, string } from '../../schemas/index.ts'; | ||
import { strict } from './strict.ts'; | ||
|
||
describe('strict', () => { | ||
test('should detect unknown keys', () => { | ||
const schema = strict(object({ key1: string(), key2: number() })); | ||
const schema = strict(object({ key1: string(), key2: optional(number()) })); | ||
|
||
const input1 = { key1: 'test', key2: 123 }; | ||
const output1 = parse(schema, input1); | ||
expect(output1).toEqual(input1); | ||
|
||
const keysError = 'Invalid keys'; | ||
const input2 = { key1: 'test', key2: 123, key3: 'unknown' }; | ||
expect(() => parse(schema, input2)).toThrowError(keysError); | ||
const input3 = { key1: 'test', key2: 123, key3: 'unknown', key4: 123 }; | ||
expect(() => parse(schema, input3)).toThrowError(keysError); | ||
const result1 = schema._parse(input1); | ||
expect(result1.output).toEqual(input1); | ||
|
||
const input2 = { key1: 'test', key2: undefined }; | ||
const result2 = schema._parse(input2); | ||
expect(result2.output).toEqual(input2); | ||
|
||
const input3 = { key1: 'test' }; | ||
const result3 = schema._parse(input3); | ||
expect(result3.output).toEqual(input3); | ||
|
||
const input4 = { key1: 'test', key2: 123, key3: '' }; | ||
const result4 = schema._parse(input4); | ||
expect(result4.issues?.length).toBe(1); | ||
|
||
const input5 = { key1: 'test', key2: undefined, key3: '' }; | ||
const result5 = schema._parse(input5); | ||
expect(result5.issues?.length).toBe(1); | ||
|
||
const input6 = { key1: 'test', key3: '' }; | ||
const result6 = schema._parse(input6); | ||
expect(result6.issues?.length).toBe(1); | ||
|
||
const input7 = { key1: 'test', key2: 123, key3: '', key4: '' }; | ||
const result7 = schema._parse(input7); | ||
expect(result7.issues?.length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,39 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { number, objectAsync, string } from '../../schemas/index.ts'; | ||
import { parseAsync } from '../parse/index.ts'; | ||
import { number, objectAsync, optional, string } from '../../schemas/index.ts'; | ||
import { strictAsync } from './strictAsync.ts'; | ||
|
||
describe('strict', () => { | ||
test('should detect unknown keys', async () => { | ||
const schema = strictAsync(objectAsync({ key1: string(), key2: number() })); | ||
const schema = strictAsync( | ||
objectAsync({ key1: string(), key2: optional(number()) }) | ||
); | ||
|
||
const input1 = { key1: 'test', key2: 123 }; | ||
const output1 = await parseAsync(schema, input1); | ||
expect(output1).toEqual(input1); | ||
|
||
const keysError = 'Invalid keys'; | ||
const input2 = { key1: 'test', key2: 123, key3: 'unknown' }; | ||
await expect(parseAsync(schema, input2)).rejects.toThrowError(keysError); | ||
const input3 = { key1: 'test', key2: 123, key3: 'unknown', key4: 123 }; | ||
await expect(parseAsync(schema, input3)).rejects.toThrowError(keysError); | ||
const result1 = await schema._parse(input1); | ||
expect(result1.output).toEqual(input1); | ||
|
||
const input2 = { key1: 'test', key2: undefined }; | ||
const result2 = await schema._parse(input2); | ||
expect(result2.output).toEqual(input2); | ||
|
||
const input3 = { key1: 'test' }; | ||
const result3 = await schema._parse(input3); | ||
expect(result3.output).toEqual(input3); | ||
|
||
const input4 = { key1: 'test', key2: 123, key3: '' }; | ||
const result4 = await schema._parse(input4); | ||
expect(result4.issues?.length).toBe(1); | ||
|
||
const input5 = { key1: 'test', key2: undefined, key3: '' }; | ||
const result5 = await schema._parse(input5); | ||
expect(result5.issues?.length).toBe(1); | ||
|
||
const input6 = { key1: 'test', key3: '' }; | ||
const result6 = await schema._parse(input6); | ||
expect(result6.issues?.length).toBe(1); | ||
|
||
const input7 = { key1: 'test', key2: 123, key3: '', key4: '' }; | ||
const result7 = await schema._parse(input7); | ||
expect(result7.issues?.length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters