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

A partly partial with io-ts #450

Closed
midzdotdev opened this issue Apr 19, 2020 · 4 comments
Closed

A partly partial with io-ts #450

midzdotdev opened this issue Apr 19, 2020 · 4 comments
Labels
experimental something related to the experimental features

Comments

@midzdotdev
Copy link

midzdotdev commented Apr 19, 2020

I just posted a new question on StackOverflow regarding making a partial from an existing schema.

Any help would be appreciated.


I'm having some issues with using io-ts. I've found that its really lacking documentation, most of the progress I've made is through GitHub issues. And no I don't understand the HKTs so no help there.

Basically I'm creating a type elsewhere, type which has a certain set of keys:

import * as S from 'io-ts/lib/Schema'

const someType = S.make(S => S.type({
  id: S.string,
  foo: S.string,
  bar: S.boolean
}))

type T = S.TypeOf<typeof someType>

And I need to transform this to a partial where some keys are still required. Say just the id key is still required, defining the type in TS would be like:

type PartlyPartial <T> = Partial<T> & { id: string }

Simple!

Now, I want to be able to do this at runtime through io-ts so that I can create a validator function with the Guard module. This is what I've got so far:

import * as G from 'io-ts/lib/Guard'

const propsBase = S.make(S => S.type({
  id: S.string
}))

const partial = S.make(S => S.intersection(
  someType(S) // needs to be partial,
  propsBase(S)
))

// So that this returns `true`
partial(G.guard).is({
  id: 'some id',
  bar: true
})

But the someType(S) defines the type as having all keys required, but none of them should be required.

@gcanti gcanti added the experimental something related to the experimental features label Apr 20, 2020
@gcanti
Copy link
Owner

gcanti commented Apr 20, 2020

making a partial from an existing schema

That's not possible

@gcanti gcanti closed this as completed Apr 20, 2020
@midzdotdev
Copy link
Author

midzdotdev commented Apr 20, 2020

I appreciate you getting back to me.

Is there no workaround that I could create temporarily? All I need to do it validate with these conditions.

Could the properties be attached as a property to the Schema fn produced by make for future manipulation. I understand it seems you've tried to get away from the whole props idea after dealing with io-ts' initial Type but in my case it seems necessary.

@RemiLelaidier
Copy link

@midzdotdev
Copy link
Author

Found that
https://stackoverflow.com/questions/61311182/a-partly-partial-with-io-ts

Yes, that's my post which I linked at the beginning, although I didn't reply saying that I found a workaround.

If anybody has the same issue, then its possible to create a wrapper around make which provides the features I'm talking about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experimental something related to the experimental features
Projects
None yet
Development

No branches or pull requests

3 participants