Skip to content

[POSSIBLE BREAKING CHANGES] Create DSL for satpam rule #54

@sendyhalim

Description

@sendyhalim

I think it's better if we have our own DSL for rule e.g.

const satpam = require('satpam');

// With string (current implementation)
const rules = {
  phoneNumber: ['required', 'string'];
  title: ['required', {
    name: 'memberOf'
    params: [['mr', 'mrs']]
  }]
};

// Next implementation could be like this
const rules = {
  phoneNumber: [
    satpam.rules.required(), 
    satpam.rules.string()
  ],
  title: [
    satpam.rules.required(),
    satpam.rules.memberOf({
      acceptedValues: ['mr', 'mrs']
    })
  ]
};

Reasoning behind this is it's easier to read and it's easier to avoid rule typo, because sometimes we do something like this

const rules = {
  phoneNumber: ['require']
};

module.exports = (req, res) => {
  // Uh oh rule typo >.<, it won't happen if we have our own DSL, 
  // would throw error when starting the server
  const  validationResult = satpam.validate(rules, req.body);
  ....
};

Bonus point
we can make it backward compatible, we just need to check old rule format and if it matches then parse the rule using the old implementation

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