quicktype
quicktype copied to clipboard
Json Schema => TypeScript additionalProperties exist on the object
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 did you figured out how it works? I'm stucking on the same thing ...
@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.
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