S
S
st-liberum2016-08-30 17:58:25
Node.js
st-liberum, 2016-08-30 17:58:25

How to write a standalone validate module based on validate.js?

Good afternoon.
I'm trying to write a separate module for validation:

const validate = require("validate.js");
const constraints = {
  Name: {
    presence: true,
    format: {
      pattern: "^[^0-9][^@#]+$",
      flags: "i",
      message: "Pattern error"
    },
    length: {
      minimum: 3,
      maximum: 20,
      message: "Length error"
    }
  },
  email: {
    presence: true,
    email: true
  }
};

function success(attributes) {
  LOG("Success!", attributes);
}

function error(errors) {
  if (errors instanceof Error) {
    // This means an exception was thrown from a validator
    LOG("An error ocurred", errors);
  } else {
    LOG("Validation errors", errors);
  }
}

module.exports.validData = (newData) => {
  validate.async(newData, constraints).then(success, error);
};

in another file I can refer, but how can I get the result in another file?
Call example:
validData(newData, function(err){
      if(!err) return  next(err);
      console.log("validate successful");
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question