aromanovich / jsl

A Python DSL for describing JSON schemas
http://jsl.readthedocs.org/
Other
218 stars 21 forks source link

Field Names with a hyphen #11

Closed hvishwanath closed 9 years ago

hvishwanath commented 9 years ago

I have a JSON that has keys with a hyphen. For example:

{
  "manifest-version": "1.0",
  "file-type":  "mp3"
}

I am trying to write a jsl to generate a schema.

class Entry(jsl.Document):
    manifest_version = jsl.StringField(required=True)
    file_type = jsl.StringField(enum=["mp3","ogg","wma"])

This produces a schema as below:

{ '$schema': 'http://json-schema.org/draft-04/schema#',
  'additionalProperties': False,
  'properties': { 'file_type': { 'enum': ['mp3', 'ogg', 'wma'],
                                 'type': 'string'},
                  'manifest_version': { 'type': 'string'}},
  'required': ['manifest_version'],
  'type': 'object'}

The keys under properties need to have a hyphen (manifest-version and manifest_version). How can I accomplish this, since a '-' is not allowed in the variable name. I explored the Options and MetaClasses that are collecting the fields, but cannot figure out how to change the field name in the resultant schema.

aromanovich commented 9 years ago

I've just released jsl==0.1.3 which adds a name parameter to all fields. It can be used as follows:

class Entry(jsl.Document):
    manifest_version = jsl.StringField(name='manifest-version', required=True)
    file_type = jsl.StringField(name='file-type', enum=["mp3","ogg","wma"])
hvishwanath commented 9 years ago

Awesome! Thank you!