{"version":3,"file":"tuple.cjs","names":["BaseSchema"],"sources":["../../src/schemas/tuple.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport { BaseSchema } from \"../core/base\";\nimport type { CombinedStandardProps, Schema } from \"../core/types\";\n\nexport class TupleSchema<I, T extends any[]> extends BaseSchema<I, T> {\n  private readonly items: Schema<I, any>[];\n  private readonly rest?: Schema<I, any>;\n\n  constructor(items: Schema<I, any>[], rest?: Schema<I, any>) {\n    super();\n    this.items = items;\n    this.rest = rest;\n    this.jsonSchema = {\n      type: \"array\",\n      prefixItems: items.map((s) => s.jsonSchema),\n      items: rest ? rest.jsonSchema : false,\n      minItems: items.length,\n      maxItems: rest ? undefined : items.length,\n    };\n  }\n\n  /**\n   * Allow additional trailing items of a given schema.\n   * @param {Schema<I, any>} restSchema Schema for trailing items.\n   * @returns {TupleSchema<I, T>} Tuple with rest items allowed.\n   */\n  rest_(restSchema: Schema<I, any>): TupleSchema<I, T> {\n    return new TupleSchema<I, T>(this.items, restSchema);\n  }\n\n  get [\"~standard\"](): CombinedStandardProps<I, T> {\n    return {\n      version: 1,\n      vendor: \"h-schema\",\n      jsonSchema: {\n        input: () => this.jsonSchema,\n        output: () => this.jsonSchema,\n      },\n      validate: (value: unknown) => {\n        if (!Array.isArray(value)) {\n          return { issues: [{ message: `Expected tuple/array, received ${typeof value}` }] };\n        }\n        if (!this.rest && value.length !== this.items.length) {\n          return {\n            issues: [\n              { message: `Expected tuple of length ${this.items.length}, got ${value.length}` },\n            ],\n          };\n        }\n        if (value.length < this.items.length) {\n          return {\n            issues: [{ message: `Tuple too short: expected at least ${this.items.length}` }],\n          };\n        }\n\n        const out: any[] = [];\n        const issues: StandardSchemaV1.Issue[] = [];\n\n        for (let i = 0; i < value.length; i++) {\n          const schema = i < this.items.length ? this.items[i]! : this.rest!;\n          const r = schema[\"~standard\"].validate(value[i]) as StandardSchemaV1.Result<any>;\n          if (\"issues\" in r) {\n            if (r.issues) {\n              issues.push(\n                ...r.issues.map((iss) => ({\n                  ...iss,\n                  path: iss.path ? [i, ...iss.path] : [i],\n                })),\n              );\n            }\n          } else {\n            out.push(r.value);\n          }\n        }\n\n        if (issues.length > 0) {\n          return { issues };\n        }\n        return { value: out as T };\n      },\n      types: {\n        input: {} as I,\n        output: {} as T,\n      },\n    };\n  }\n}\n"],"mappings":";;AAIA,IAAa,cAAb,MAAa,oBAAwCA,aAAAA,WAAiB;CACpE;CACA;CAEA,YAAY,OAAyB,MAAuB;EAC1D,MAAM;EACN,KAAK,QAAQ;EACb,KAAK,OAAO;EACZ,KAAK,aAAa;GAChB,MAAM;GACN,aAAa,MAAM,KAAK,MAAM,EAAE,UAAU;GAC1C,OAAO,OAAO,KAAK,aAAa;GAChC,UAAU,MAAM;GAChB,UAAU,OAAO,KAAA,IAAY,MAAM;EACrC;CACF;;;;;;CAOA,MAAM,YAA+C;EACnD,OAAO,IAAI,YAAkB,KAAK,OAAO,UAAU;CACrD;CAEA,KAAK,eAA4C;EAC/C,OAAO;GACL,SAAS;GACT,QAAQ;GACR,YAAY;IACV,aAAa,KAAK;IAClB,cAAc,KAAK;GACrB;GACA,WAAW,UAAmB;IAC5B,IAAI,CAAC,MAAM,QAAQ,KAAK,GACtB,OAAO,EAAE,QAAQ,CAAC,EAAE,SAAS,kCAAkC,OAAO,QAAQ,CAAC,EAAE;IAEnF,IAAI,CAAC,KAAK,QAAQ,MAAM,WAAW,KAAK,MAAM,QAC5C,OAAO,EACL,QAAQ,CACN,EAAE,SAAS,4BAA4B,KAAK,MAAM,OAAO,QAAQ,MAAM,SAAS,CAClF,EACF;IAEF,IAAI,MAAM,SAAS,KAAK,MAAM,QAC5B,OAAO,EACL,QAAQ,CAAC,EAAE,SAAS,sCAAsC,KAAK,MAAM,SAAS,CAAC,EACjF;IAGF,MAAM,MAAa,CAAC;IACpB,MAAM,SAAmC,CAAC;IAE1C,KAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;KAErC,MAAM,K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