Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made change to support the meta._req object for precise Locale per request #57

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
sails-hook-validation
=====================

[![Build Status](https://travis-ci.org/lykmapipo/sails-hook-validation.svg?branch=master)](https://travis-ci.org/lykmapipo/sails-hook-validation)
[![Build Status](https://travis-ci.org/fpm-git/sails-hook-validation.svg?branch=master)](https://travis-ci.org/fpm-git/sails-hook-validation)

Custom validation error messages for sails model with i18n support. Its works with `callback`, `deferred` and `promise` style `model API` provided with sails.

*Note:*
- *This requires Sails v0.11.0+. If v0.11.0+ isn't published to NPM yet, you'll need to install it via Github.*
- *`sails-hook-validation` work by patch model static `validate()`, `create()`, `createEach()`, `findOrCreate()`, `findOrCreateEach()` and `update()`.*
- *To have custom error messages at model instance level consider using [sails-model-new](https://github.com/lykmapipo/sails-model-new).*
- *`sails-hook-validation` opt to use `error.Errors` and not to re-create or remove any properties of error object so as to remain with sails legacy options*

## Installation
```sh
$ npm install --save sails-hook-validation
$ npm install --save '@floatplane/sails-hook-validation'
```

## Usage
Expand Down
14 changes: 7 additions & 7 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ module.exports = function(model, validateCustom) {
//prepare new create method
//which wrap sailsCreate
//with custom error message checking
function create(values, callback) {

function create(values, callback, meta) {
if (!meta) { meta = {}; }

// handle Deferred where
// it passes criteria first
// see https://github.com/balderdashy/waterline/blob/master/lib/waterline/query/dql/create.js#L26
if (arguments.length === 3) {
var args = Array.prototype.slice.call(arguments);
callback = args.pop();
values = args.pop();
if((_ || sails.util._).isPlainObject(arguments[0]) && ((_ || sails.util._).isPlainObject(arguments[1]) || (_ || sails.util._).isArray(arguments[1]))) {
values = arguments[1];
callback = arguments[2];
}

// return Deferred
Expand All @@ -54,7 +54,7 @@ module.exports = function(model, validateCustom) {
//custom errors messages
if (error.invalidAttributes) {
var customError =
validateCustom(model, error.invalidAttributes);
validateCustom(model, error.invalidAttributes, meta._req);

// will return and override with empty object
// when using associations
Expand Down
13 changes: 7 additions & 6 deletions lib/createEach.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ module.exports = function(model, validateCustom) {
var sailsCreate = model.createEach;

//prepare new createEach method
function createEach(values, callback) {
function createEach(values, callback, meta) {
if (!meta) { meta = {}; }

// handle Deferred where
// it passes criteria first
// See https://github.com/balderdashy/waterline/blob/master/lib/waterline/query/aggregate.js#L27
if (arguments.length === 3) {
var args = Array.prototype.slice.call(arguments);
callback = args.pop();
values = args.pop();
if((_ || sails.util._).isPlainObject(arguments[0]) && ((_ || sails.util._).isPlainObject(arguments[1]) || (_ || sails.util._).isArray(arguments[1]))) {
values = arguments[1];
callback = arguments[2];
}

// return Deferred
Expand All @@ -51,7 +52,7 @@ module.exports = function(model, validateCustom) {
//custom errors messages
if (error.invalidAttributes) {
var customError =
validateCustom(model, error.invalidAttributes);
validateCustom(model, error.invalidAttributes, meta._req);

// will return and override with empty object
// when using associations
Expand Down
6 changes: 4 additions & 2 deletions lib/findOrCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ module.exports = function(model, validateCustom) {
var sailsFindOrCreate = model.findOrCreate;

//prepare new findOrCreate method
function findOrCreate(criteria, values, callback) {
function findOrCreate(criteria, values, callback, meta) {
if (!meta) { meta = {}; }

// return Deferred
// if no callback passed
// See https://github.com/balderdashy/waterline/blob/master/lib/waterline/query/composite.js#L43
Expand All @@ -42,7 +44,7 @@ module.exports = function(model, validateCustom) {
//custom errors messages
if (error.invalidAttributes) {
var customError =
validateCustom(model, error.invalidAttributes);
validateCustom(model, error.invalidAttributes, meta._req);

// will return and override with empty object when using associations
if (Object.keys(customError).length !== 0) {
Expand Down
6 changes: 4 additions & 2 deletions lib/findOrCreateEach.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ module.exports = function(model, validateCustom) {
var sailsFindOrCreateEach = model.findOrCreateEach;

//prepare new findOrCreateEach method
function findOrCreateEach(criterias, values, callback) {
function findOrCreateEach(criterias, values, callback, meta) {
if (!meta) { meta = {}; }

// return Deferred
// if no callback passed
// See https://github.com/balderdashy/waterline/blob/master/lib/waterline/query/aggregate.js#L96
Expand All @@ -43,7 +45,7 @@ module.exports = function(model, validateCustom) {
//custom errors messages
if (error.invalidAttributes) {
var customError =
validateCustom(model, error.invalidAttributes);
validateCustom(model, error.invalidAttributes, meta._req);

// will return and override with empty object
// when using associations
Expand Down
5 changes: 3 additions & 2 deletions lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module.exports = function(model, validateCustom) {
//prepare new update method
//which wrap sailsUpdate
//with custom error message checking
function update(criterias, values, callback) {
function update(criterias, values, callback, meta) {
if (!meta) { meta = {}; }

// return Deferred
// if no callback passed
Expand All @@ -45,7 +46,7 @@ module.exports = function(model, validateCustom) {
//custom errors messages
if (error.invalidAttributes) {
var customError =
validateCustom(model, error.invalidAttributes);
validateCustom(model, error.invalidAttributes, meta._req);

// will return and override with empty object
// when using associations
Expand Down
6 changes: 4 additions & 2 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ module.exports = function(model, validateCustom) {
var sailsValidate = model.validate;

//prepare new validation method
function validate(values, presentOnly, callback) {
function validate(values, presentOnly, callback, meta) {
if (!meta) { meta = {}; }

if(typeof presentOnly === 'function'){
callback = presentOnly;
presentOnly = null;
Expand All @@ -33,7 +35,7 @@ module.exports = function(model, validateCustom) {
//custom errors messages
if (error.invalidAttributes) {
var customError =
validateCustom(model, error.invalidAttributes);
validateCustom(model, error.invalidAttributes, meta._req);

// will return and override with empty object
// when using associations
Expand Down
8 changes: 4 additions & 4 deletions lib/validateCustom.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
*
* @param {Object} model valid sails model definition
* @param {Object} invalidAttributes a valid sails validation error object.
* @param {Object} [req] a request object - It's necessary to get the exact locale
*
* @returns {Object} an object with friendly validation error conversions.
*/
module.exports = function(model, invalidAttributes) {

module.exports = function(model, invalidAttributes, req) {
//grab model validations definitions
var validations = model._validator.validations || {};

Expand Down Expand Up @@ -70,8 +70,8 @@ module.exports = function(model, invalidAttributes) {

if(sails.config.i18n){
//deduce locale from request else
//use default locale
locale =
//use default locale from config
locale = (typeof req !== 'undefined' && req.getLocale !== 'undefined') ? req.getLocale() :
sails.config.i18n.requestLocale ||
sails.config.i18n.defaultLocale;

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-hook-validation",
"version": "0.4.7",
"name": "@floatplane/sails-hook-validation",
"version": "0.5.0",
"description": "Custom validation error messages for sails model with i18n support",
"main": "index.js",
"sails": {
Expand All @@ -12,7 +12,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/lykmapipo/sails-hook-validation.git"
"url": "https://github.com/fpm-git/sails-hook-validation.git"
},
"keywords": [
"sails",
Expand Down Expand Up @@ -41,9 +41,9 @@
},
"license": "MIT",
"bugs": {
"url": "https://github.com/lykmapipo/sails-hook-validation/issues"
"url": "https://github.com/fpm-git/sails-hook-validation/issues"
},
"homepage": "https://github.com/lykmapipo/sails-hook-validation",
"homepage": "https://github.com/fpm-git/sails-hook-validation",
"contributors": [{
"name": "lykmapipo",
"github": "https://github.com/lykmapipo"
Expand Down