Partial Application of the validator

Is there any way to partially apply the validator for eg:
const test = type({
name: "string",
info: { email: "string.email & string.lower", age: "number" }
});
const test = type({
name: "string",
info: { email: "string.email & string.lower", age: "number" }
});
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
ssalbdivad
ssalbdivad4mo ago
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.
aabad_ankit
aabad_ankitOP4mo ago
Awesome. For my current use case get should be sufficient. Thank you so much for the quick response
ssalbdivad
ssalbdivad4mo ago
No problem! Always better when there's something that exists I can recommend haha
aabad_ankit
aabad_ankitOP4mo ago
Any way to circumvent default values
const test = type({
name: b,
info: { email: "string.email & string.lower", age: "number=10" }
});
console.log(test.get("info")({email: "[email protected]"}));
const test = type({
name: b,
info: { email: "string.email & string.lower", age: "number=10" }
});
console.log(test.get("info")({email: "[email protected]"}));
Gives me { email: '[email protected]', age: 10 } dont want it to add age to it
ssalbdivad
ssalbdivad4mo ago
Funny 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")
aabad_ankit
aabad_ankitOP4mo ago
Just a test example so I do need to validate a complete object without applying default values so .withoutOptionalAndDefaultMeta should solve my problem.
ssalbdivad
ssalbdivad4mo ago
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
ssalbdivad
ssalbdivad4mo ago
GitHub
arktype/ark/type/tests/objects/mapped.test.ts at 89be3c5cc060bb...
TypeScript's 1:1 validator, optimized from editor to runtime - arktypeio/arktype
ssalbdivad
ssalbdivad4mo ago
Or remove their defaults
aabad_ankit
aabad_ankitOP4mo ago
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)
ssalbdivad
ssalbdivad4mo ago
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
aabad_ankit
aabad_ankitOP4mo ago
Great, I will give this a try
ssalbdivad
ssalbdivad4mo ago
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
aabad_ankit
aabad_ankitOP4mo ago
Is map a recent addition?
No description
aabad_ankit
aabad_ankitOP4mo ago
using "arktype": "^2.0.0-rc.5",
ssalbdivad
ssalbdivad4mo ago
Yeah .map is pretty new

Did you find this page helpful?