Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller authored Sep 8, 2023
2 parents 25a46b5 + 66145c6 commit 9e7833a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 29 deletions.
1 change: 1 addition & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to the library will be documented in this file.
## vX.X.X (Month DD, YYYY)

- Add `cuid2` validation (pull request #130)
- Fix bug in `strict` and `strictAsync` method for optional keys (issue #131)

## v0.13.1 (August 23, 2023)

Expand Down
39 changes: 28 additions & 11 deletions library/src/methods/strict/strict.test.ts
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);
});
});
4 changes: 1 addition & 3 deletions library/src/methods/strict/strict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export function strict<TSchema extends ObjectSchema<any>>(
_parse(input, info) {
const result = schema._parse(input, info);
return !result.issues &&
// Check length of input and output keys
Object.keys(input as object).length !==
Object.keys(result.output).length
Object.keys(input as object).some((key) => !(key in result.output))
? getIssues(info, 'object', 'strict', error || 'Invalid keys', input)
: result;
},
Expand Down
41 changes: 30 additions & 11 deletions library/src/methods/strict/strictAsync.test.ts
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);
});
});
4 changes: 1 addition & 3 deletions library/src/methods/strict/strictAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export function strictAsync<TSchema extends ObjectSchemaAsync<any>>(
async _parse(input, info) {
const result = await schema._parse(input, info);
return !result.issues &&
// Check length of input and output keys
Object.keys(input as object).length !==
Object.keys(result.output).length
Object.keys(input as object).some((key) => !(key in result.output))
? getIssues(info, 'object', 'strict', error || 'Invalid keys', input)
: result;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const LoginSchema = object({
]),
password: string('Your password must be a string.', [
minLength(1, 'Please enter your password.'),
minLength(8, 'You password must have 8 characters or more.'),
minLength(8, 'Your password must have 8 characters or more.'),
]),
});
```
Expand Down

0 comments on commit 9e7833a

Please sign in to comment.