models
import "github.com/caltechlibrary/models"
models is a Go package. A model is expressed in
YAML. They are used by modelgen
to render HTML web forms or
SQLite3 schema.
The elements attribute holds a list of elements. You can think of these as HTML5 form elements described in YAML. They will also be used to infer SQLite 3 column types.
Each element is made from the following properties.
required: true
and
checked: true
in YAML. NOTE: attributes value’s are
resolved to quoted strings when rendered as HTML.
The models package starts from the premise of supporting a YAML description of a web form that then can be used to render HTML and SQL Schema. It also needs to be able to be a thin layer in a Go API that can validate the elements of a model just like they are validated browser side by the HTML5 input types. The following are all implemented by the models package using a naive validation approach3.
Additional data types4 can be defined by using the
Model.Define
function provided in this package. You need to
provide a name for the new type as well as the func’s name. The
“defined” data types are applied before the default types. This allows
for improvements to the defaults while retaining a fallback. Hopefully
this mechanism can prove useful to expanding the data types supported by
models.
NOTE: As the models package evolves the validation methods provided out of the box will evolve too. Some may even be dropped if they prove problematic5.
This is an example model of a guest book entry used in a Dataset base guest book web application.
id: test_model
attributes:
method: GET
action: ./
x-success: http://localhost:8000/success.html
x-fail: http://localhost:8000/failed.html
elements:
- id: id
type: text
attributes:
name: id
placeholder: Enter a unique string
required: true
is_primary_id: true
- id: name
type: text
attributes:
name: name
placeholdertext: Enter your name
required: true
- id: msg
type: textarea
attributes:
name: msg
placehodertext: Enter a short message or comment
See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input for details.↩︎
See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern for details of how patterns are used in validation.↩︎
“naive” in this case means overly simplistic validation, e.g. min max ranges don’t validate against step attributes.↩︎
The validation function is used server side only because it is written in Go. E.g. by Dataset’s JSON API.↩︎
E.g. “week” input type is not widely used and is poorly supported by browsers in 2024. “image” doesn’t make a whole lot of sense.↩︎