Skip to content

Commit

Permalink
fix: support types for arrays within objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvera committed Aug 31, 2024
1 parent c014fbe commit cfe931d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/type-aliases/Schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

## Defined in

[src/types.ts:61](https://github.com/ericvera/betterbe/blob/main/src/types.ts#L61)
[src/types.ts:63](https://github.com/ericvera/betterbe/blob/main/src/types.ts#L63)
2 changes: 1 addition & 1 deletion docs/type-aliases/Value.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Type Alias: Value\<TData\>

> **Value**\<`TData`\>: `TData` _extends_ `object` ? [`ObjectValidator`](../interfaces/ObjectValidator.md)\<`TData`\> : `TData` _extends_ `string` ? [`StringValidator`](../interfaces/StringValidator.md) : `TData` _extends_ `number` ? [`NumberValidator`](../interfaces/NumberValidator.md) : `TData` _extends_ `boolean` ? [`BooleanValidator`](../interfaces/BooleanValidator.md) : [`ValidatorBase`](../interfaces/ValidatorBase.md)
> **Value**\<`TData`\>: `TData` _extends_ `unknown`[] ? [`ArrayValidator`](../interfaces/ArrayValidator.md) : `TData` _extends_ `object` ? [`ObjectValidator`](../interfaces/ObjectValidator.md)\<`TData`\> : `TData` _extends_ `string` ? [`StringValidator`](../interfaces/StringValidator.md) : `TData` _extends_ `number` ? [`NumberValidator`](../interfaces/NumberValidator.md) : `TData` _extends_ `boolean` ? [`BooleanValidator`](../interfaces/BooleanValidator.md) : [`ValidatorBase`](../interfaces/ValidatorBase.md)
## Type Parameters

Expand Down
18 changes: 17 additions & 1 deletion src/object.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, it, vi } from 'vitest'
import { ValidationError } from './ValidationError.js'
import { Schema, boolean, number, object, string } from './index.js'
import { Schema, array, boolean, number, object, string } from './index.js'

interface User {
name: string
Expand Down Expand Up @@ -242,3 +242,19 @@ it('should throw an error if an inner test function throws', () => {
validator.validate({ name: 'John Doe', age: 42 })
}).toThrowErrorMatchingInlineSnapshot(`[Error: 'name' cannot be John Doe]`)
})

it('should work for object containing an array', () => {
const schema = {
uids: array(string({ minLength: 5, maxLength: 5 })),
}

interface UserList {
uids: string[]
}

const validator = object<UserList>(schema)

expect(() => {
validator.validate({ uids: ['asdij', '01234', '1414k'] })
}).not.throw()
})
20 changes: 11 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ export interface ArrayValidator extends ValidatorBase {
type: ValidatorType.ARRAY
}

export type Value<TData> = TData extends object
? ObjectValidator<TData>
: TData extends string
? StringValidator
: TData extends number
? NumberValidator
: TData extends boolean
? BooleanValidator
: ValidatorBase
export type Value<TData> = TData extends unknown[]
? ArrayValidator
: TData extends object
? ObjectValidator<TData>
: TData extends string
? StringValidator
: TData extends number
? NumberValidator
: TData extends boolean
? BooleanValidator
: ValidatorBase

export type Schema<TData> = {
[P in keyof TData]-?: Value<TData[P]>
Expand Down

0 comments on commit cfe931d

Please sign in to comment.