admin管理员组文章数量:1316393
I am working on an io-ts validation where I would like to validate the list length (it has to be between min and max). I am wondering if there's a way to achieve this behavior since it could e quite handy at runtime for API endpoint validation.
What I have so far is
interface IMinMaxArray {
readonly minMaxArray: unique symbol // use `unique symbol` here to ensure uniqueness across modules / packages
}
const minMaxArray = (min: number, max: number) => t.brand(
t.array,
(n: Array): n is t.Branded<Array, IMinMaxArray> => min < n.length && n.length < max,
'minMaxArray'
);
The code above does not work, it requires an argument for the Array
-s and t.array
is also not accepted. How could I make this work in a generic way?
I am working on an io-ts validation where I would like to validate the list length (it has to be between min and max). I am wondering if there's a way to achieve this behavior since it could e quite handy at runtime for API endpoint validation.
What I have so far is
interface IMinMaxArray {
readonly minMaxArray: unique symbol // use `unique symbol` here to ensure uniqueness across modules / packages
}
const minMaxArray = (min: number, max: number) => t.brand(
t.array,
(n: Array): n is t.Branded<Array, IMinMaxArray> => min < n.length && n.length < max,
'minMaxArray'
);
The code above does not work, it requires an argument for the Array
-s and t.array
is also not accepted. How could I make this work in a generic way?
1 Answer
Reset to default 10Your definition is missing the typing and the codec for the array. You could make this work with a few modifications on the interface definition and extending the branded type with a codec:
interface IMinMaxArray<T> extends Array<T> {
readonly minMaxArray: unique symbol
}
const minMaxArray = <C extends t.Mixed>(min: number, max: number, a: C) => t.brand(
t.array(a),
(n: Array<C>): n is t.Branded<Array<C>, IMinMaxArray<C>> => min < n.length && n.length < max,
'minMaxArray'
);
Now you can create a definition like
minMaxArray(3,5, t.number)
If you want the definition to be more generic and posable, you could write a generic branded type that accepts a predicate:
interface RestrictedArray<T> extends Array<T> {
readonly restrictedArray: unique symbol
}
const restrictedArray = <C>(predicate: Refinement<C[], ArrayOfLength<C>>) => <C extends t.Mixed>(a: C) => t.brand(
t.array(a), // a codec representing the type to be refined
(n): n is t.Branded<C[], RestrictedArray<C>> => predicate(n), // a custom type guard using the build-in helper `Branded`
'restrictedArray' // the name must match the readonly field in the brand
)
interface IRestrictedArrayPredicate<C extends t.Mixed> {
(array: C[]): array is ArrayOfLength<C>
}
Now you can define your restrictions. It's probably a good idea to define min and max separately, since they can be useful on their own too:
const minArray = <C extends t.Mixed>(min: number)
=> restrictedArray(<IRestrictedArrayPredicate<C>>((array) => array.length >= min));
const maxArray = <C extends t.Mixed>(max: number)
=> restrictedArray(<IRestrictedArrayPredicate<C>>((array) => array.length <= max));
And bining these two you can define minMaxArray:
export const minMaxArray = <C extends t.Mixed>(min: number, max: number, a: C) => t.intersection([minArray(min)(a), maxArray(max)(a)])
Hope this helps.
本文标签: javascriptHow to validate array length with iotsStack Overflow
版权声明:本文标题:javascript - How to validate array length with io-ts? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742003166a2411425.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论