xddq / schema2typebox

Creating TypeBox code from JSON schemas
MIT License
66 stars 15 forks source link

throws on properties containing a `-` #45

Closed smeijer closed 6 months ago

smeijer commented 6 months ago

Library version: 1.7.2

JSON schema version: draft-04, draft-06, draft-07, 2019-09, 2020-12

The current behavior

Throws when a property contains a hyphen:

import { schema2typebox } from "schema2typebox";

const schema = JSON.stringify({
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "car-brand": { // < troubling
      "type": "string"
    }
  }
});

await schema2typebox({ input: schema });

The expected behavior

It's prettier that throws when formatting. This lib should just quote the property name, so it's:

Type.Object({
  'car-brand': Type.String(),
})

Why does it happen? What/How to fix the issue?

This lib generates invalid code by naming writing something like { car-brand: Type.String() }, which prettier rightfully throws upon.

Content of minimal json schema file which causes the problem

Click to expand/collapse ```json { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "car-brand": { "type": "string" } } } ```