Skip to content

Support set Schema output/input types explicitly #48

Description

@moshest

In cases we already have the schema types defined, it would be a nice to have the Schema receive the output type and optionally the input type explicitly.

Motivation: Validate that the validator actually implement the given types exactly.

For example:

type Person {
  name: string;
  age: number;
}

type PersonInput {
  name: unknown;
  age: unknown;
}

const PersonSchema = Schema<Person, PersonInput>({
  name: unknown.string(),
  age: unknown.number(),
);

// typeof PersonSchema == (input: PersonInput) => Person;

I tried to implement it from the beginning, but I couldn't find a way to make it work nicely without breaking some functionality. Maybe it's time to give it another try.

Considerations:

  • Support Schema without any type arguments
  • Support giving only output type
  • Support giving both output and input types
  • Support async validators
  • Bonus: Support IDE when Ctrl+Click on scheme field (ie. clicking on schema age: will jump to Person['age'])

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions