Skip to content

Proposal: Add validations to the data array #12

@glanotte

Description

@glanotte

Reference: https://groups.google.com/forum/?fromgroups=#!topic/collectionjson/Q2dnK82ok9A

Use Case: We would like to add the ability to pass validation information along to our front end clients to compliment our server-side validations. This is to help reduce potentially expensive operations on the server. Our particular scenario is to try to catch invalid file uploads before they are uploaded, but we see opportunities to use this to enrich our UX by having more responsive validations and reduce the risk of a divergence of logic between the two.

The proposed format for the validators is:

{
  "collection": {
    "version": "1.0",
    "href": "http://www.example.com/file_upload",
    "template":{
      "data": [{
        "name": "file",
        "value": null,
        "validations": [{
          "name": "file_size",
          "value": [0, 2097152],
          "prompt": "The maximum size for a file"
        }, {
          "name": "file_type",
          "value": ["png", "bmp", "jpg", "gif"],
          "prompt": "allowed file types"
        }]
      },{
        "name": "label",
        "value": null,
        "validations": [{
          "name": "length",
          "value": [0, 50],
          "prompt": "The allowed length of the label"
        }]
      },{
        "name": "background_color",
        "value": null,
        "validations": [{
          "name": "inclusion",
          "value": ["red", "green", "blue"],
          "prompt": "The list of allowed background colors"
        }]
      },{
        "name": "email_address",
        "value": null,
        "validations": [{
          "name": "format",
          "value": "\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b",
          "prompt": "regex pattern for an email address"
        }]
      }]
    }
  }
}

When proposing on the google group, the suggestion was raised that we would need a common set of validators. I reviewed a few validation libraries and below seemed to be a fairly common set, though the naming will look familiar to many a ruby and rails developer.

  • inclusion - validates value to ensure it is within an Array of allowed values.
    • name: inclusion
    • value: ARRAY
  • exclusion - validates value to ensure it is not within an Array of allowed values.
    • name: exclusion
    • value: ARRAY
  • format - validates String to ensure it matches regex
    • name: format
    • value: Regex as String
  • length - validates a String against and Array containing an upper and lower bound length
    • name: length
    • value: ARRAY - [Lower bound Integer, Upper bound Integer]
  • file_type - validates a File against an Array of allowed file types
    • name: file_type
    • value: ARRAY
  • file_size - validates a File against an Array containing an upper and lower bound file size
    • name: length
    • value: ARRAY - [Lower bound Integer, Upper bound Integer]
  • presence - validates a value to ensure that it present (not null and not empty)
    • name: presence
    • value: null

If there are validators that are missing or the implementation is lacking for some specific needs please let me know. I will be working on a PR to link to this so we can have more of a work in progress.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions