Methods

django-schemulator provides the following methods:

wtfield_to_schema(field)

This method takes a WTForms field and returns a JSON schema (a dictionary).

 

field_to_schema(field)

This method takes a Django Forms field and returns a JSON schema (a dictionary).

 

form_to_schema(form)

This method takes a Django Form or a WTForms and returns a JSON schema (a dictionary). Internally, it analyzes the fields that the form contains and uses one of the previous methods to provide the JSON schema representation.

Example

For both forms, the corresponding JSON Shema is the following. Note: Some irrelevant entries have been removed for clarity.

{ '$schema': 'http://json-schema.org/draft-04/schema#', 
  'description': 'This is a JSON Schema describing a form',
  'title': 'JSON Schema'
  'properties': {
       'boolean_field': {
           'description': 'This is a boolean field', 
           'null': False, 
           'optional': True,
           'title': 'Boolean Field',
           'default': True,
           'type': 'boolean'}, 
       'email_field': {
           'description': 'This is an email field',
           'format': 'email',
           'maxLength': 100,
           'null': False, 
           'optional': True, 
           'title': 'Email Field',
           'default': 'email@example.com',
           'type': 'string'}, 
       'text_field': {
           'description': 'This is a text field',
           'minLength': 20,
           'maxLength': 100,
           'null': False,
           'optional': True,
           'title': 'Text Field',
           'default': None,
           'type': 'string'}},
}

 

schema_to_form(schema, form_type=None)

This method takes a dictionary with a valid JSON Schema syntax and returns a form. By default, the form returned is a Django Form, but by setting theform_type argument to wtforms the output is a WTForm.

 

schema_to_wtfield(schema)

This method is used by theschema_to_form() method to dynamically generate the fields within the returned form. It takes a dictionary with a valid JSON Schema syntax describing a field and returns an WTForms UnboundField instance.

If you where to use it independently, you may bind the field to a form by setting it as an attribute of a WTForms form before instatination, via setattr.

 

schema_to_field(schema)

This method is used by theschema_to_form() method to dynamically generate the fields within the returned form. It takes a dictionary with a valid JSON Schema syntax describing a field and returns an Django Forms field instance.

 

Special JSON Schema Keywords

django-schemulator recognizes three special keywords within a JSON Schema representation of a form. These can be provided to explicitly indicate which field classes and widgets should be used within the forms. These should be the name of the form or widget class.

Example:

The following JSON Schema describes a Django forms CharField with a Textarea widget instead of the default TextInput widget.

{
'__django_form_field_cls': 'CharField', 
'__widget': 'Textarea',
'type': 'string',
'title': 'Text Area Field',
'description': 'This is a text area field',
'minLength': 0, 
'maxLength': 200,
'optional': False,
}