arktypea
arktype8mo ago
Robb

No validation of field until schema is complete ?

Hello,
I am an experienced developer, unfortunately not experienced in TypeScript. I am in the process of learning, so sorry if this is trivial.
I am trying to set up a form for a entity whose schema varies on some type field. I think I have found a way to express this in Arktype, but the validation doesn't seem to work as intended.

Here is a minimal way to reproduce:
import { type } from "arktype"


const BaseSchema = type({
  name: "string.integer",
});

const ASchema = BaseSchema.and({
  type: "'typeA'",
  fieldA: "number",
});
const BSchema = BaseSchema.and({
  type: "'typeB'",
  configuration: "object",
});

export const Thing = ASchema.or(BSchema);

const out = Thing({
    name: "MyName",
})


The errors only show the fact that type is wrong, but won't tell a thing about the name being incorrect.
Is this the wrong way to have discriminated unions ?
Was this page helpful?