Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain struct's type in partial and pick helpers #1149

Merged
merged 2 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/reference/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ partial(
{ name: 'Jane' }
```

`partial` allows you to create a new struct based on an existing object struct, but with all of its properties being optional.
`partial` allows you to create a new struct based on an existing `object` or `type` struct, but with all of its properties being optional.

### `pick`

Expand All @@ -106,4 +106,4 @@ pick(
)
```

`pick` allows you to create a new struct based on an existing object struct, but only including specific properties.
`pick` allows you to create a new struct based on an existing `object` or `type` struct, but only including specific properties.
20 changes: 15 additions & 5 deletions src/structs/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Struct, Context, Validator } from '../struct'
import { Context, Struct, Validator } from '../struct'
import { Assign, ObjectSchema, ObjectType, PartialObjectSchema } from '../utils'
import { object, optional, type } from './types'
import { ObjectSchema, Assign, ObjectType, PartialObjectSchema } from '../utils'

/**
* Create a new struct that combines the properties properties from multiple
Expand Down Expand Up @@ -192,13 +192,17 @@ export function omit<S extends ObjectSchema, K extends keyof S>(
export function partial<S extends ObjectSchema>(
struct: Struct<ObjectType<S>, S> | S
): Struct<ObjectType<PartialObjectSchema<S>>, PartialObjectSchema<S>> {
const schema: any =
struct instanceof Struct ? { ...struct.schema } : { ...struct }
const isStruct = struct instanceof Struct
const schema: any = isStruct ? { ...struct.schema } : { ...struct }

for (const key in schema) {
schema[key] = optional(schema[key])
}

if (isStruct && struct.type === 'type') {
return type(schema) as any
}

return object(schema) as any
}

Expand All @@ -220,7 +224,13 @@ export function pick<S extends ObjectSchema, K extends keyof S>(
subschema[key] = schema[key]
}

return object(subschema as Pick<S, K>)
switch (struct.type) {
case 'type':
return type(subschema) as any

default:
return object(subschema) as any
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions test/validation/partial/valid-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { number, partial, string, type } from '../../../src'

export const Struct = partial(
type({
name: string(),
age: number(),
})
)

export const data = {
name: 'john',
unknownProperty: true,
}

export const output = {
name: 'john',
unknownProperty: true,
}
19 changes: 19 additions & 0 deletions test/validation/pick/valid-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { number, pick, string, type } from '../../../src'

export const Struct = pick(
type({
name: string(),
age: number(),
}),
['name']
)

export const data = {
name: 'john',
unknownProperty: true,
}

export const output = {
name: 'john',
unknownProperty: true,
}