ts-json-validator icon indicating copy to clipboard operation
ts-json-validator copied to clipboard

Custom fields in schema (AJVs "custom keywords")

Open antoniusostermann opened this issue 4 years ago • 4 comments

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! :-)

antoniusostermann avatar May 26 '20 13:05 antoniusostermann

This definitely seems possible but I have to think about the right design.

Besides the custom keywords issue, what other features were you looking for?

ostrowr avatar May 26 '20 23:05 ostrowr

Is there any chance for this ticket to be finished? Unfortunately, I'm not so familiar with TS and can't help you with PR.

nordluf avatar Jul 09 '20 17:07 nordluf

@nordluf planning on getting to this ASAP but unfortunately probably not this week.

ostrowr avatar Jul 15 '20 02:07 ostrowr

The last update on this was quite a long time ago. Did you ever decide to move forward with this?

funkel1989 avatar Feb 09 '21 14:02 funkel1989