Partial Application of the validator
Is there any way to partially apply the validator
for eg:
Now I want to partially use
test.info.email
or apply the validator based on the partial object I pass.
Usecase: I will use an arktype to represent the schema in my db. Now I want to allow to partially update the db. but still apply the transformation and validation specified in the schema.16 Replies
You can use
.get
to extract a path from a Type
But I guess you mean you want to pass it in the original structure like { info: {email: "blah"} }
?
There's not a builtin solution for that yet, but the natural one would be a deepPartial/deepRequired
generic + chainable alongside the existing partial
/required
Feel free to create an issue with the use case if you'd like, would definitely be doable and fit into the API.Awesome. For my current use case
get
should be sufficient.
Thank you so much for the quick responseNo problem! Always better when there's something that exists I can recommend haha
Any way to circumvent default values
Gives me
{ email: '[email protected]', age: 10 }
dont want it to add age to itFunny I am about to publish a
.withoutOptionalAndDefaultMeta
for an internal fix haha
If you don't need morphs on your type you can do .allows
instead of the root type invocation to just check if it's a valid input
Not sure if it is useful in this case but you couljd also check email directly with test.get("info", "email")
Just a test example so I do need to validate a complete object without applying default values so
.withoutOptionalAndDefaultMeta
should solve my problem.Hmm I wasn't planning on exposing that
It wouldn't exactly be what you're looking for anyways because that just removes it from an individual value
Maybe you could just use
.map
to strip off the values you don't care about?
https://github.com/arktypeio/arktype/blob/89be3c5cc060bb14e5bf69e677b0cfcff2a1da1f/ark/type/__tests__/objects/mapped.test.tsGitHub
arktype/ark/type/tests/objects/mapped.test.ts at 89be3c5cc060bb...
TypeScript's 1:1 validator, optimized from editor to runtime - arktypeio/arktype
Or remove their defaults
Are you saying that using
map
it is possible to remove all the default from all the properties(nested or otherwise) (I dont' know the properties name which can have default)Yeah you can do it at runtime, but obviously TS wont' be able to statically infer it if you don't know the names ahead of time
You might have to do some casting in the implementation since TS hates dealing with generic params in a scenario like that but yeah should work
Great, I will give this a try
There are nuances I'm looking at now with how/when default/optionality metadata is preserved on values (as opposed to associated with a prop) so if it's not behaving as expected let me know.
If you do something parallel to what's in those test cases at least it should be fine
Is map a recent addition?
using
"arktype": "^2.0.0-rc.5",
Yeah
.map
is pretty new