glideapps / glideapps/quicktype

Json Schema => TypeScript additionalProperties exist on the object

Open
#1,322 4 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
13.9k
Forks
1.2k
Avg merge
8h 53m
Merged PRs (30d)
369

Description

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));
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.