Go to file
Daniil Fajnberg 6b241feda5 form field `clean` now force-stringifies value 2022-04-26 21:55:56 +02:00
requirements added functions to check interfaces against their HTML counterparts; optional dependency on bs4 2022-01-01 13:28:36 +01:00
src/yamlhttpforms form field `clean` now force-stringifies value 2022-04-26 21:55:56 +02:00
tests initial 2021-12-18 21:09:40 +01:00
.coveragerc initial 2021-12-18 21:09:40 +01:00
.gitignore initial 2021-12-18 21:09:40 +01:00
README.md updated readme 2022-01-05 15:07:25 +01:00
coverage.sh initial 2021-12-18 21:09:40 +01:00
pyproject.toml initial 2021-12-18 21:09:40 +01:00
setup.cfg correct dependency 2022-02-02 13:37:22 +01:00

README.md

HTTP forms defined in YAML

This module allows creating simple interfaces to forms/payloads for use in HTTP POST requests by defining them in highly readable and easily maintainable YAML files.

Form definition

A form is defined by its fields.

A field is defined by its name, which is the parameter name in the payload sent during a POST request, and which typically corresponds to the name attribute of a <select> or <input> HTML tag.

Optionally, a field can have an alias (for internal use), a default value, value options (as <select> tags do), and may be declared required.

A form definition in YAML format will consist of the field names as top-level keys, and either nothing/null or the corresponding fields' definitions as key-value-pairs below them.

Example

Definition

# definition.yaml

way_too_long_field_name:
  alias: short_name
foo: 
choice_field:
  options:
    value1: text for option 1
    value2: text for option 2
  default: value1
mandatory_field:
  alias: special
  required: true

Usage

>>> from yamlhttpforms import load_form
>>> form_interface = load_form('definition.yaml')
>>> form_interface.get_payload(short_name='abc', foo='bar', special='420')
{'way_too_long_field_name': 'abc', 'foo': 'bar', 'choice_field': 'value1', 'mandatory_field': '420'}

>>> form_interface.get_payload(short_name='abc', choice_field='baz', special='420')
Traceback (most recent call last):
  ...
ValueError: "baz" is not a valid option for <SelectField: name="choice_field", default="value1", options={'value1': 'text for option 1', 'value2': 'text for option 2'}>