Individually validating key/value pairs in a type #830
-
I'm looking for a way to validate single key/value pairs in a class Example<T extends Scope> {
constructor(private schema: T) {}
// This is essentially how I want to validate the data, key by key
private validate<K extends keyof T['infer']>(key: K, value: unknown) {
return this.schema.compile()[key](value);
}
}
// Inferred as "{ foo: string; 'bar?': string }"
const scopeExample= scope({
foo: 'string',
'bar?': 'string'
});
// Inferred as "{ foo: string; bar?: string | undefined }"
const typeExample = type({
foo: 'string',
'bar?': 'string'
});
const types = scopeExample.compile();
types.foo(data);
types.bar(data); // types['bar?'](data) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
At some point, we'll add the ability to index in to types like I've created an issue to track that here: For now, I think the easiest thing to do would just be to have a list of "optional" aliases passed to Let me know if that feels like an acceptable intermediate solution! |
Beta Was this translation helpful? Give feedback.
At some point, we'll add the ability to index in to types like
"myType['foo']"
(like TS) and probably additionally using the JS dot notation i.e.myType.foo
.I've created an issue to track that here:
#831
For now, I think the easiest thing to do would just be to have a list of "optional" aliases passed to
Example
. When validating an alias from that list, you could check if the input wasundefined
before passing it to the scope validator and allow it.Let me know if that feels like an acceptable intermediate solution!