Skip to content

Commit

Permalink
DUMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark committed Feb 26, 2017
1 parent 889bd3b commit 9b5fa1b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 34 deletions.
94 changes: 61 additions & 33 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,41 +112,18 @@ The field under validation must be numeric and must have an exact length of valu
----
The field under validation must have a length between the given min and max.


~~dimensions~~
----
The file under validation must be an image meeting the dimension constraints as specified by the rule's parameters:

~~distinct~~
----
When working with arrays, the field under validation must not have any duplicate values.


email
----
The field under validation must be formatted as an e-mail address.


~~exists:table,column~~
----
The field under validation must exist on a given database table.


~~file~~
----
The field under validation must be a successfully uploaded file.


~~filled~~
----
The field under validation must not be empty when it is present.


~~image~~
----
The file under validation must be an image (jpeg, png, bmp, gif, or svg)


in:foo,bar,... (in_array)
----
The field under validation must be included in the given list of values. Since this rule often requires you to implode an array.
Expand Down Expand Up @@ -204,15 +181,6 @@ The field under validation must be less than or equal to a maximum value.

Warning: Not support File type.

~~mimetypes:text/plain,...~~
----
The file under validation must match one of the given MIME types:

~~mimes:foo,bar,...~~
----
The file under validation must have a MIME type corresponding to one of the listed extensions.


~~nullable~~
----
The field under validation may be null. This is particularly useful when validating primitive such as strings and integers that can contain null values.
Expand Down Expand Up @@ -293,6 +261,11 @@ The field under validation must be a string. If you would like to allow the fiel
The field under validation must be a valid timezone identifier according to the `momentjs` timezone function.



~~exists:table,column~~
----
The field under validation must exist on a given database table.

~~unique:table,column,except,idColumn~~
----
The field under validation must be unique in a given database table. If the column option is not specified, the field name will be used.
Expand Down Expand Up @@ -332,9 +305,64 @@ ends_with:foo
The field under validation must be end within value.


### File & Image

## Custom messages
#### handle multipart bodies

you may be interested in the following modules:
* [formidable](https://www.npmjs.org/package/formidable#readme)
* [multer](https://www.npmjs.org/package/multer#readme)
When you using above modules, you could get the file or image information.
Then, you must to transfer information below before use file validation:
```
const file = {
mimetype: 'image/png', // for mime type or other valiations
path: '/var/tmp/xxxx.png', // for dimensions
}
```

#### Image
Before using validation, you need to download and install [GraphicsMagick](http://www.graphicsmagick.org/) or [ImageMagick](http://www.imagemagick.org/). In Mac OS X, you can simply use [Homebrew](http://mxcl.github.io/homebrew/) and do:
```
brew install imagemagick
brew install graphicsmagick
```
More details: https://github.com/aheckmann/gm#getting-started


~~file~~
----
The field under validation must be a successfully uploaded file.

~~image~~
----
The file under validation must be an image (jpeg, png, bmp, gif, or svg)
```
const rules = {
'file' : 'image',
}
```

mimetypes:text/plain,...
----
The file under validation must match one of the given MIME types:

~~mimes:foo,bar,...~~
----
The file under validation must have a MIME type corresponding to one of the listed extensions.

dimensions
----
The file under validation must be an image meeting the dimension constraints as specified by the rule's parameters:
```
const rules = {
'image' : 'dimensions:min_width=100,min_height=200,ratio=3/2',
}
```

Available constraints are: min_width, max_width, min_height, max_height, width, height, ratio.

## Custom messages
```
const messages = {
required: 'This field is required to complete the registration process.'
Expand Down
28 changes: 27 additions & 1 deletion src/Validations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const numericRules = ['numeric', 'integer'];
* @return {Array}
* @private
*/
const imageMimeTypes = ['image/jpeg', 'image/png', 'image/bmp', 'image/gif'];
const imageMimeTypes = ['image/jpeg', 'image/png', 'image/bmp', 'image/gif', 'image/svg+xml'];

/**
* @description enforces a field to be confirmed by another.
Expand Down Expand Up @@ -1236,6 +1236,32 @@ Validations.string = function (data, field, message, args) {
});
};

/**
* @description Validate the mime type of an file matches the given values
* @method regex
* @param {Object} data
* @param {String} field
* @param {String} message
* @param {Array} args
* @return {Object}
* @public
*/
Validations.mimetypes = function (data, field, message, args) {
return new Promise(function (resolve, reject) {
const fieldValue = _.get(data, field);
if (skippable(fieldValue)) {
resolve('validation skipped');
return;
}

if (fieldValue.mimetype === args[0]) {
resolve('validation passed');
return;
}
reject(message);
});
};

Validations.image = function (data, field, message, args) {
return new Promise(function (resolve, reject) {
const fieldValue = _.get(data, field);
Expand Down

0 comments on commit 9b5fa1b

Please sign in to comment.