Skip to content

Commit

Permalink
Retain struct's type in partial and pick helpers (#1149)
Browse files Browse the repository at this point in the history
* Retain struct's type in partial and pick helpers

* Refactor for clarity

Refactors partial helper to more directly reflect its internal logic
including schema shorthand usage.
  • Loading branch information
arturmuller authored Dec 16, 2023
1 parent a4c2ebd commit 4511928
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
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,
}

0 comments on commit 4511928

Please sign in to comment.