|
1 | 1 | # modelate |
2 | | -A data modeling tool for NodeJS |
| 2 | +A data modeling tool for NodeJS. It's 100% database agnostic, and 100% customizable. |
| 3 | + |
| 4 | +# How does it work? |
| 5 | +Create a model, and turn your objects into that model. |
| 6 | + |
| 7 | +```javascript |
| 8 | +var Modelate = require('./index'); |
| 9 | + |
| 10 | +var model = { |
| 11 | + name: { |
| 12 | + type: 'string', |
| 13 | + length: { |
| 14 | + max: 10, |
| 15 | + min: 1 |
| 16 | + }, |
| 17 | + value: { |
| 18 | + eq: 'Paco' |
| 19 | + } |
| 20 | + }, |
| 21 | + age: { |
| 22 | + type: 'number', |
| 23 | + value: { |
| 24 | + max: 95, |
| 25 | + min: 18 |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +var user = Modelate('User').model(model); |
| 31 | +var data = { |
| 32 | + name: 'Paco', |
| 33 | + age: 17 // Age does not match with min value |
| 34 | +}; |
| 35 | + |
| 36 | +var result = user.modelate(data); // => { name: 'Paco } |
| 37 | +``` |
| 38 | +# Validators |
| 39 | +Validators are just functions. It will be executed for each property, and return a boolean value indicating if the content match the model requirements or not. |
| 40 | + |
| 41 | + - [Type](#type) |
| 42 | + - [Length](#length) |
| 43 | + - [Value](#value) |
| 44 | + - [Custom function](#custom-function) |
| 45 | + |
| 46 | + ## Type |
| 47 | + If the data has not the specifed type, validation will fail, and that field will not be added to final validated data. |
| 48 | + |
| 49 | + ```javascript |
| 50 | + { |
| 51 | + type: String // Check if data has a JS type (uses typeof) |
| 52 | + } |
| 53 | + ``` |
| 54 | + |
| 55 | + ## Length |
| 56 | + Check data length. It uses default .length param, so it's valid for so much types. |
| 57 | + |
| 58 | + ```javascript |
| 59 | +{ |
| 60 | + length: { |
| 61 | + eq: Number, // Exact allowed value |
| 62 | + max: Number, // Maximum allowed value |
| 63 | + min: Number // Minimum allowed value |
| 64 | + } |
| 65 | +} |
| 66 | + ``` |
| 67 | + |
| 68 | + ## Value |
| 69 | + Check data length. It uses default .length param, so it's valid for so much types. |
| 70 | + |
| 71 | + ```javascript |
| 72 | +{ |
| 73 | + value: { |
| 74 | + eq: String || Number, // Exact allowed value (equals) |
| 75 | + max: Number, // Maximum allowed value |
| 76 | + min: Number, // Minimum allowed value |
| 77 | + contains: String || Number // The value contains |
| 78 | + } |
| 79 | +} |
| 80 | + ``` |
| 81 | + |
| 82 | +## Custom function |
| 83 | +Use a custom function to check at your own criteria. The only necessary thing is the function to return true or false- |
| 84 | + |
| 85 | +```javascript |
| 86 | +{ |
| 87 | + func: function A function to validate. Must return true or false. |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +The function might look like this: |
| 92 | +``` |
| 93 | +function is42(value) { |
| 94 | + var allowed = [42, '42', 'cuarenta y dos', 'fourty two', 'the answer to the life the universe and everything']; |
| 95 | + if(allowedValues.indexOf(value) === -1) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + return true; |
| 99 | +} |
| 100 | +``` |
0 commit comments