quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

Json Schema => TypeScript additionalProperties exist on the object

Open CarolineM opened this issue 6 years ago • 4 comments

I am converting Json Schema to Typescript.

I want parsing not to fail if unknown properties are encountered, so I am using the additionalProperties key in my schema.

This works fine, but the next step I have is to serialize the object. The unknown properties exist on my quicktype parsing output. So, if I want to remove them, it seems that I need to recurse through the object and only pick the ones defined in my TypeScript interface.

Is there any way around this? I want to remove the additionalProperties from the outputted object.

Schema

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/MyTest",
    "definitions": {
      "MyTest": {
        "type": "object",
        "additionalProperties": true,
        "properties": {
          "Allowed": {
            "type": "string"
          }
      }
   }
}

Sample Json

{
  "Allowed": "test",
  "AnotherThing": "want to remove" 
}

This should fail but it passes

  const test = (name) => {
    const fileStr = readFile(name);
    const outfile = Convert.toMyTest(fileStr);
    const convertedStr = Convert.myTestToJson(outfile);
    expect(JSON.parse(fileStr)).toStrictEqual(JSON.parse(convertedStr));
  }

CarolineM avatar Oct 10 '19 01:10 CarolineM

@CarolineM did you figured out how it works? I'm stucking on the same thing ...

benjamin-carl avatar Nov 03 '19 21:11 benjamin-carl

@CarolineM did you figured out how it works? I'm stucking on the same thing ...

No, I gave up and did it the hard way.

CarolineM avatar Nov 04 '19 00:11 CarolineM

Commenting in the vain hope that this issue receives attention. Optionally stripping additional properties in the validation is the standard in validation libraries nowadays [1]

[1] https://github.com/gcanti/io-ts/blob/master/index.md#exact-types

albertodiazdorado avatar Apr 20 '22 06:04 albertodiazdorado