Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ Validators are just functions. It will be executed for each property, and return
- [Custom function](#custom-function)
- [Date](#date)
- [Model](#model)
- [Email](#email)
- [Regex](#regex)

## Type
If the data has not the specifed type, validation will fail, and that field will not be added to final validated data.

It uses typeof, so arrays will be considered valid objects. Also, there is a type "array", wich ensures data is an array.

```javascript
{
type: String // Check if data has a JS type (uses typeof)
type: String // Check if data has a JS type
}
```

Expand Down Expand Up @@ -118,7 +122,7 @@ Check if data is a JavaScript Date. Just need to set a boolean `date` parameter,

```javascript
{
data: Boolean // Check if data is a JS Date
date: Boolean // Check if data is a JS Date
}
```
> _Remember that JavaScript dates has type `Object`_
Expand Down Expand Up @@ -168,6 +172,48 @@ var batman = geopoint.modelate(myGeopoint);
console.log(batman);
```

## Email
Check if data is an email. Might be just a boolean, or an object to check domains:

```javascript
{
email: Boolean // Check if data is an email
}
```

Also, to check domains:
```javascript
{
email: {
domain: 'gmail.com'
}
}
```

Or, to enable more than one valid domains:
```javascript
{
email: {
domain: ['gmail.com', 'google.com']
}
}
```

## Regex
Test a regular expression over the data.
```javascript
{
regex: Regex // Check if data is regex valid
}
```
An example might be:
```javascript
{
regex: /[a]/
}
```
This will validate `asd` but not `sdf`

# Tests

I'm currently adding tests to this code. I'm using Jasmine, and saving tests in `/spec` folder.
Expand Down Expand Up @@ -195,10 +241,10 @@ Master version might not have the last test updates. Check out the `develop` bra
- [ ] Validators
- [x] Type
- [x] Length
- [ ] Value
- [ ] Custom function
- [ ] Date
- [ ] Model
- [x] Value
- [x] Custom function
- [x] Date
- [ ] Model (Need help with this #15)

# Contribute
You can use this code as you like. If you find a bug, or want to ask for a feature, just open an issue, and we'll do our best. If you can fix it, do a pull request to dev branch, and we promise to review it as fast as possible to merge it.
Expand Down
6 changes: 4 additions & 2 deletions examples/chat.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* Just a first basic test */
var Modelate = require('../index');
// In production, just change the require for:
// var Modelate = require('modelate');

// Models
var userModel = {
name: {
type: 'string',
length: { // For now, length do nothng.
length: {
max: 10,
min: 1,
},
Expand All @@ -15,7 +17,7 @@ var userModel = {
},
surname: {
type: 'string',
length: { // For now, length do nothng.
length: {
max: 3,
min: 1,
},
Expand Down
6 changes: 4 additions & 2 deletions examples/user.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* Just a first basic test */
var Modelate = require('../index');
// In production, just change the require for:
// var Modelate = require('modelate');

var model = {
name: {
type: 'string',
length: { // For now, length do nothng.
length: {
max: 10,
min: 1,
},
Expand All @@ -14,7 +16,7 @@ var model = {
},
surname: {
type: 'string',
length: { // For now, length do nothng.
length: {
max: 3,
min: 1,
},
Expand Down
4 changes: 2 additions & 2 deletions lib/validators/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ function isValid(data, model) {
if (data instanceof Date) {
return true;
}

// Try to parse data to Date;
var date = Date.parse(data);
if (!isNaN()) {
if (!isNaN(date)) {
data = new Date(date);
return true;
}


return false;
}

Expand Down
50 changes: 50 additions & 0 deletions lib/validators/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Email validator
*
* {
* email: {
* domain: String, Array
* }
* }
*/

function isValid(data, model) {
if (!model.email) {
return true;
}

if(typeof model.email === 'object' && typeof model.email.domain !== 'undefined') {
if (isEmail(data)) {
return validateDomain(data, model.email.domain);
} else {
return false;
}
}

return isEmail(data);
}

function isEmail(data) {
// regex from http://emailregex.com/
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return emailRegex.test(data);
}

function validateDomain(data, domain) {
const dataDomain = data.split('@')[1];

if (typeof domain === 'string') {
return dataDomain.toUpperCase() === domain.toUpperCase();
} else if (Array.isArray(domain)) {
for (var i = domain.length - 1; i >= 0; i--) {
if(domain[i].toUpperCase() === dataDomain.toUpperCase()) {
return true;
}
}
return false;
}

return false;
}

module.exports = isValid;
4 changes: 4 additions & 0 deletions lib/validators/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function isValid(data, model) {
return true;
}

if(typeof model.func !== 'function') {
return false;
}

return model.func(data);
}

Expand Down
4 changes: 4 additions & 0 deletions lib/validators/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function isValid(data, model) {
return true;
}

if(!models[model.model]) {
return false;
}

var validate = module.parent.parent.exports;
var valid = validate(data, { model: models[model.model] });

Expand Down
22 changes: 22 additions & 0 deletions lib/validators/regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Regex validator
*
* {
* regex: Regex
* }
*/

function isValid(data, model) {
if (!model.regex) {
return true;
}

if(typeof model.regex === 'object' && typeof model.regex.test === 'function') {
return model.regex.test(data);
}

return false;
}


module.exports = isValid;
4 changes: 3 additions & 1 deletion lib/validators/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ function isValid(data, model) {
return true;
}

if ((typeof (data)).toUpperCase() === model.type.toUpperCase()) {
if ((typeof data).toUpperCase() === model.type.toUpperCase()) {
return true;
} else if (model.type.toUpperCase() === 'ARRAY') {
return Array.isArray(data);
}

return false;
Expand Down
66 changes: 48 additions & 18 deletions lib/validators/value.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var util = require('../util');

/**
* Value validator
*
Expand All @@ -7,6 +9,7 @@
* max: Number, // Maximum allowed value
* min: Number, // Minimum allowed value
* contains: String || Number // The value contains
* list: Array // List of exact allowed values
* }
* }
*/
Expand All @@ -18,28 +21,21 @@ function isValid(data, model) {

// Data is different than equals
if (model.value.eq) {
if (typeof (data) === typeof (model.value.eq) && data !== model.value.eq) {
// Same data type and different values
if(!equals(data, model.value.eq)) {
return false;
}
// Different types: Try to convert one to other.
var parsed = data;
var type = typeof (model.value.eq);
switch (type.toLowerCase()) {
case 'string':
parsed = String(data);
break;
case 'number':
parsed = Number(data);
break;
case 'boolean':
parsed = Boolean(data);
break;
}
}

if (data !== model.value.eq) {
if (model.value.list) {
if(!Array.isArray(model.value.list)) {
return false;
}

for (var i = model.value.list.length - 1; i >= 0; i--) {
if(!equals(data, model.value.list[i])) {
return false;
}
}
}

// Data is higher than max
Expand All @@ -53,7 +49,41 @@ function isValid(data, model) {
}

// Data does not contain something
if (model.value.contains && data.indexOf(model.value.contains) === -1) {
if (model.value.contains) {
var testData = data;
if(typeof data === 'number') {
testData = data.toString();
}

return (testData.indexOf(model.value.contains) !== -1);
}

return true;
}

function equals(data, expected) {
// Same data type and different values
if (typeof (data) === typeof (expected) && data !== expected) {
// If object, return util.compare. Else, return false.
return (typeof data === 'object') ? util.equal(data, expected) : false;
}

// Different types: Try to convert one to other.
var parsed = data;
var type = typeof expected;
switch (type.toLowerCase()) {
case 'string':
parsed = String(data);
break;
case 'number':
parsed = Number(data);
break;
case 'boolean':
parsed = Boolean(data);
break;
}

if (data !== expected) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modelate",
"version": "0.2.2",
"version": "0.2.3",
"description": "A simple data modeling tool for NodeJS",
"main": "index.js",
"scripts": {
Expand Down
31 changes: 31 additions & 0 deletions spec/validators/date.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const valid = require('../../lib/validators/date');

const model = {
date: true
};

describe(' - Date validator', () => {

it('shall be a function', () => {
expect(typeof valid).toEqual('function');
});

it('shall return true with a valid date', () => {
const date = new Date();
const result = valid(date, model);
expect(result).toEqual(true);
});

it('shall return true with a parsable date', () => {
const date = 'Jan 9, 2018';
const result = valid(date, model);
expect(result).toEqual(true);
});

it('shall return false with a non parsable date', () => {
const date = 'NoDate';
const result = valid(date, model);
expect(result).toEqual(false);
});

});
Loading