Categorygithub.com/caltechlibrary/models
modulepackage
0.0.5
Repository: https://github.com/caltechlibrary/models.git
Documentation: pkg.go.dev

# README

License Latest release

models

This is a Go package used to describe data models aligned with the HTML5 data types. The model can be expressed in YAML or JSON. The YAML (or JSON) data structure is patterned after the HTML5 form elements. A single model can be used to generate HTML web forms or used to validate a map that confirms to the model. In princple generators can be written to express the model in other forms, e.g. SQL.

It is important to note that is not an Object Relational Mapper (ORM). The purpose of the model package is to facilitate describing simple data models using YAML then beable to reuse the models in other Go based projects (e.g. dataset, Newt).

This Go package assumes Go version 1.23.1 or better.

Oberservation: Web forms describe a simple data structure

The models package grew out of an observation that if you can define the elements of an HTML5 web form you can also describe a simple data model or schema. The problem is HTML5 is combersum to type, read and work with. On the other hand it lends itself to expression in simpler representations.

YAML can be used to represent a web form in a clear and concise way. From that description you can extrapulate HTML and SQL Schema. You can also use it as a guide to data validation for web form submissions.

Our common use cases.

  1. Web form as YAML can be used to generate HTML web forms
  2. Web form elements can be used to inferring the SQL column type
  3. Web form as YAML is a guide to validating web form submissions

A simple example

A "guest book" model.

id: guest_book_entry
attributes:
  action: ./signbook.html
  method: POST
  x-success: ./thankyou.html
  x-failure: ./oops.html
elements:
  - id: record_id
    type: text
    pattern: [a-z0-9]+\.[a-z0-9]+
    attributes:
      name: record_id
      placeholder: A unique record id
      required: true
  - id: name
    type: text
    attributes:
      name: name
      placeholder: E.g. Doe, Jane
      required: true
  - id: message
    type: text
    attributes:
      placeholder: Please leave a message
      name: message
  - id: signed
    type: date
    attributes:
      name: signed
      required: true

This "model" describes JSON data that might look like the following.

{ 
    "record_id": "jane.doe",
    "Doe, Jane",
    "signed": "2024-09-10"
}

The model could be used to generate the web form and validate the data. It implies an SQL Scheme. The model package provides the means of working with a model and to validate the model's content. By normalizing your data elements to throse supported by HTML5 you also can easily generate the code you need (e.g. HTML form or SQL scheme).

The package doesn't provide the extrapolated forms but does provide the functions and method to make it easy to build them.

# Packages

No description provided by the author

# Functions

ElementToHTML renders an individual element as HTML, includes label as well as input element.
FmtHelp lets you process a text block with simple curly brace markup.
GenerateButton sets up for an HTML input type "button".
GenerateCheckbox sets up for an HTML input type "checkbox".
GenerateColor sets up for HTML input type "color".
GenerateDate setups up for HTML date input element.
GenerateDateTimeLocal sets up for HTML input type "datetime-local".
GenerateEmail sets up for HTML input type "email".
GenerateImage sets up for an HTML input type "image".
GenerateISNI sets up for an HTML input type "text" with a pattern for INSI input.
GenerateMonth sets up for HTML input type "month".
GenerateNumber sets up for an HTML input type "number".
GenerateORCID sets up for an HTML input type text using a pattern for ORCID.
GeneratePassword sets up for an HTML input type "password".
GenerateRadio sets up for an HTML input type "radio".
GenerateRange sets up for an HTML input "range" (defauting is min 0 to max 100, step 1).
GenerateReset sets up for an HTML input type "reset".
GenerateROR setups up for an HTML ROR type input element.
GenerateSearch sets up HTML input type "search".
GenerateSubmit sets up for an HTML input type "submit".
GenerateTel sets up for an HTML input type "tel" (i.e.
GenerateText generates an Element setup to hold an HTML text input elements.
GenerateTextarea sets up for HTML textarea input.
GenerateTime sets up for an HTML input type "time".
GenerateURL sets up for an HTML input type "url".
GenerateUUID setups up for an HTML uuid type input element.
GenerateWeek generates an Element setup to hold an HTML week input.
IsValidVarname tests a sting confirms it conforms to Model's naming rule.
No description provided by the author
ModelToHTML takes a model and renders an input form.
No description provided by the author
No description provided by the author
No description provided by the author
ModelToYAML renders a Model struct as YAML.
NewElement, makes sure element id is valid, populates an element as a basic input type.
NewModel, makes sure model id is valid, populates a Model with the identifier element providing returns a *Model and error value.
NewPrompt creates a new prompt struct for use with the prompt methods.
SetDebug sets the model's Debug flag to debug.
No description provided by the author
No description provided by the author
ValidateCheckbox checks is the form value was provided, returns false if empty string recieved for value.
ValidateColor checks to see if the value is expressed using Hexidecimal notation.
ValidateDate makes sure the date string conforms to YYYY-MM-DD.
ValidateDateTimeLocal makes sure the datetime string conforms to Spec: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-local-date-and-time-string.
ValidateEmail parses email address to confirm it is valid.
ValidateImage, if value is empty string this returns true.
No description provided by the author
ValidateMonth parses the string for a year and month value, i.e.
ValidateNumber implements a number validation using the json package.
No description provided by the author
ValidatePassword makes sure an password input element holds a single string.
No description provided by the author
ValidateRange retrieves the form's value as a float64 then checks if it is in range.
No description provided by the author
Validate ROR form element.
No description provided by the author
No description provided by the author
ValidateTel validates formValue conforms to a phone number.
ValidateText will check to see if pattern is set, if so it will evaluate the formValue against the RegExp given in Pattern.
No description provided by the author
ValidateTime validates the formValue is a time format.
ValidateURL validates a formValue is a URL.
No description provided by the author
ValidateWeek attempts to validate a week number with year, string is WW-YYYY formatted NOTE: this is a crude validation since some years have 52 weeks other 53 depending on how the days of the week line up against the year.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
ReleaseDate, the date version.go was generated.
ReleaseHash, the Git hash when version.go was generated.
No description provided by the author
Version number of release.

# Variables

Debug turns on verbose log output for model debugging.
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Element implementes the GitHub YAML issue template syntax for an input element.
Model implements a data structure description inspired by GitHub YAML issue template syntax.
Prompt holds the elements need to present questions and menus to build simple conversational console interfaces.

# Type aliases

GenElementFunc is a function which will generate an Element configured represent a model's supported "types".
RenderFunc is a function thation takes an io.Writer and Model then renders the model into the io.Writer.
ValidateFunc is a function that validates form assocaited with the Element and the string value received in the web form (value before converting to Go type).