Giter VIP home page Giter VIP logo

Comments (2)

sinclairzx81 avatar sinclairzx81 commented on June 4, 2024 1

@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

TypeScript Link 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.

alexey13 avatar alexey13 commented on June 4, 2024

Thank you very much!
Will use TypeRegistry example, its awesome!

from typebox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.