ostrowr / ts-json-validator

Let JSON play nicely with Typescript
MIT License
342 stars 7 forks source link

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

Open antoniusostermann opened 4 years ago

antoniusostermann commented 4 years ago

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

ostrowr commented 4 years ago

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?

nordluf commented 4 years ago

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.

ostrowr commented 4 years ago

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

funkel1989 commented 3 years ago

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