* .validate(callback)

Purpose

Checks the current keys/values on the record against the validations specified in the attributes object of your model.

Overview

Parameters

Description Accepted Data Types Required ?
1 Callback function Yes

Callback Parameters

Description Possible Data Types
1 Error Error

Example Usage

  1. User.find().exec(
  2. function(err,myRecords){
  3. // Grab a record off the top, change it to the wrong data type, then try to validate
  4. var getOneRecord = myRecords.pop();
  5. getOneRecord.name = ['Marie','Hank'];
  6. getOneRecord.name.validate(
  7. function(err){
  8. if (err)
  9. console.log(JSON.stringify(err));
  10. });
  11. });
  12. // {"ValidationError":{"name":[{"data":["Marie","Hank"],"message":"Validation error: \"Marie,Hank\" is not of type \"string\"","rule":"string"}]}}

For model

  1. module.exports = {
  2. attributes: {
  3. name: 'string'
  4. }
  5. };

Notes

This is shorthand for Model.validate({ attributes }, cb) If you .save() without first validating, Waterline tries to convert. If it can’t, it will throw an error. In this case, it would have converted the array to the string ‘Marie,Hank’

There will be no parameters in the callback unless there is an error. No news is good news.

This is an instance method. Currently, instance methods ARE NOT TRANSACTIONAL. Because of this, it is recommended that you use the equivalent model method instead.