Skip to content

Provide an ability to add custom validation rules? #129

@am17torres

Description

@am17torres

This library is great! It handles a lot of common validation constraints and coverage the vast majority of use cases. I'd really like to use this library but I have a few more complex business requirements for validations that are not covered natively. Before I put together a PR for this, I wanted to get some initial thoughts and feedback.

One such validation requirement is that the fields in a column need to exist in a remote system. I'm sure there are a number of other use cases other folks might have.

Here's what I am thinking:

In the field.constraints allow the user to pass in custom names and any arguments for that function.

{
  "fields": [
    {
        "name": "col",
        "constraints": {
            "required": true,
            "customFunction1": "scalarArgument", /* int, string, bool, etc. */
            "customFunction2": ["array", "of", 1, "mixed", true, "arguments"],
            "customFunction3": {
                "object": "of",
                "arguments": {
                    "including": "nested"
                }
            }
        }
    }
  ]
}

We can add new validation rules to the table. Perhaps something like table.customValidators() or similar. customValidators accepts a map of synchronous and async functions and should be defined before table.read or table.iter.

table.customValidators({
    customFunction1: async (row, ...args) => {
      // do some async validation
    },
    customFunction2: (row, ...args) => {
      // something sync
    }
    customFunction3:(row, ...args) => {
      // something sync
    }
})

This will allow the tableschema library more flexibility when it comes to validating data.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions