Skip to content

Custom fields in schema (AJVs "custom keywords") #35

@antoniusostermann

Description

@antoniusostermann

First of all: Thanks for this small library, it's exactly what I was looking for.

I'm trying to combine this library with AJV, struggling with the right way to use "custom keywords" (https://github.com/ajv-validator/ajv/blob/master/CUSTOM.md) with ts-json-validator. ts-json-validator does not allow any kind of keywords which aren't part of the official JSON Schema specification, so it throws an error for something like this:

  createSchema({
    type: "object",
    properties: {
      name: createSchema({ type: "string", forbiddenNames: ["Administrator"] }),
    },
  });

("Object literal may only specify known properties, and 'forbiddenNames' does not exist in type (...)")

One possible solution could be to allow additional typings via an optional generic and merge those toegether with the already existing ones, so I'd be able to use createSchema<{ forbiddenNames: string[] }> which would merge the parameter-type with the given one. As a further improvement, createSchema could even "inherit" the given type whenever sub-schemas are defined via properties etc.

What do you think about this?

Edit: Nevermind, had to switch to https://github.com/bcherny/json-schema-to-typescript due to some additional requirements. Keep up the great work! :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions