Comments (2)
@alexey13 Hi!
Unfortunately, TypeBox doesn't support this type currently as it's tied to the Draft-7 specification for Tuple representation (which is the default for Ajv). However, you can still implement this type manually....
There's a couple of ways you can approach this. Either using Type.Unsafe (using Ajv as your validator), or registering a new type with the TypeRegistry.
Ajv + Unsafe Type
The following implements this type using Unsafe. To use this type, you will need to configure Ajv for draft 2020-12. This is generally the quickest way to get the type working.
Documentation on configuring Ajv for Draft 2012-12 can be found here
import { Type, Static, TSchema } from '@sinclair/typebox'
export type TupleRestStatic<T extends TSchema[], Rest extends TSchema, Acc extends unknown[] = []> =
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? TupleRestStatic<R, Rest, [...Acc, Static<L>]>
: [...Acc, ...Static<Rest>[]]
export const TupleRest = <T extends TSchema[], Rest extends TSchema>(items: [...T], rest: Rest) =>
Type.Unsafe<TupleRestStatic<T, Rest>>({
type: 'array',
prefixItems: items,
unevaluatedItems: rest
})
const T = TupleRest([Type.String(), Type.Number()], Type.Boolean())
type T = Static<typeof T> // [string, number, ...boolean[]]
TypeRegistry
The following implements the same type using the TypeBox TypeRegistry. This approach will require you to manually implement the check logic for the type.
import { Type, Static, TSchema, TypeRegistry, Kind } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'
// --------------------------------------------------------------------
// TupleRest
// --------------------------------------------------------------------
TypeRegistry.Set<TTupleRest>('TupleRest', (schema, value) => {
return (
Array.isArray(value) && value.length >= schema.prefixItems.length &&
schema.prefixItems.every((schema, index) => Value.Check(schema, value[index])) &&
value.every((value, index) => index < schema.prefixItems.length || Value.Check(schema.unevaluatedItems, value))
)
})
export type TupleRestStatic<T extends TSchema[], Rest extends TSchema, Acc extends unknown[] = []> =
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? TupleRestStatic<R, Rest, [...Acc, Static<L>]>
: [...Acc, ...Static<Rest>[]]
export interface TTupleRest<T extends TSchema[] = [], Rest extends TSchema = TSchema> extends TSchema {
[Kind]: 'TupleRest'
static: TupleRestStatic<T, Rest>
type: 'array'
prefixItems: T
unevaluatedItems: Rest
}
export function TupleRest<T extends TSchema[], Rest extends TSchema>(prefixItems: [...T], unevaluatedItems: Rest): TTupleRest<T, Rest> {
return { [Kind]: 'TupleRest', type: 'array', prefixItems, unevaluatedItems } as never
}
// ...
const T = TupleRest([Type.String(), Type.Number()], Type.Boolean())
type T = Static<typeof T> // [string, number, ...boolean[]]
const R = Value.Check(T, ['', 1, true, true, true]) // true
I'll make a note to implement an overload for the Rest parameter on Type.Tuple
once TypeBox transitions over to Draft 2020-12. But for now, the above approach is the current best option for representing this type.
Let me know if this helps
Cheers
S
from typebox.
Thank you very much!
Will use TypeRegistry example, its awesome!
from typebox.
Related Issues (20)
- Regression bug 0.32.19 HOT 2
- TypeGuard.IsEnum HOT 2
- Better pre-validation errors for invalid TypeBox schema HOT 2
- Static resolution is not working as expected when using a generic function to generate a schema HOT 3
- tsconfig "strict": true - ignore modifiers HOT 2
- `Static` types cannot be used to enforce types from a generic parameter HOT 3
- Applying refinements + general advice on my implementation HOT 11
- Unable to create value of custom class type HOT 2
- Migrate to regexp vs string type in pattern HOT 4
- feedback on Refinement API HOT 2
- Type.Date() cause errors with fastify and querybuilder returning type Date HOT 2
- Should coerce type to array HOT 2
- Interconnected interfaces, missing type annotations and maximum length exceeded. HOT 2
- Optimisations to inference in 0.32 seems to cause `ts(2589)` with composite types. HOT 16
- Add trim option to Type.String() HOT 1
- Should add option to remove redundant prop in Convert() HOT 1
- Metadata missing in custom types HOT 2
- Schema of JSON Schema HOT 3
- Utility type inference appears missing or broken HOT 2
- How to express function optional parameter? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typebox.