gbv / avram-js

Avram Schema Language implementation in JavaScript
https://gbv.github.io/avram-js/
MIT License
2 stars 0 forks source link

Support validation with external rules #15

Open nichtich opened 10 months ago

nichtich commented 10 months ago

Requires a method to load/supply code via plugins and/or a ruleLoader.

A plugin (aka rule engine) gets the external rule (string or object) and returns a rule checker. A rule checker is a function that gets a record/field/subfield and returns a (possibly empty) array of errors. An error is a JSON object with keys message. Additional keys error (rule id), tag, occurrence, are set automatically by caller.

Rules at upper level Avram elements are initialized first, so in this case:

{
  "fields": {
    "abc": {
      "rules": [ "x-rule" ]
    }
  },
  "rules": [ 
    { "id": "x-rule", .. }
  ]
}

The rule engine knows about { "id": "x-rule", .. } first so it can use "x-rule" as reference to the former rule.

nichtich commented 9 months ago

Extensions are included in 0.6.2 (not live loading yet). Additional method loadRule returning an object with methods checkRecord, checkField, checkSubfield should be implemented.