├── .gitignore ├── LICENSE ├── README.md ├── bower.json ├── dist └── strategy.min.js ├── lib └── strategy.js ├── package.json └── tests └── strategySpec.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | bower_components 3 | .DS_Store 4 | .idea 5 | npm-debug.log -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React validatorjs strategy 2 | 3 | Strategy for using [validatorjs](https://github.com/skaterdav85/validatorjs) with [react-validation-mixin](https://github.com/jurassix/react-validation-mixin). 4 | 5 | The strategy interface for `react-validation-mixin` is defined [here](https://jurassix.gitbooks.io/docs-react-validation-mixin/content/overview/strategies.html) and that is what this library implements as an interface for [validatorjs](https://github.com/skaterdav85/validatorjs). 6 | 7 | ## Installation 8 | 9 | ### Browser 10 | 11 | First follow the instructions to install [validatorjs](https://github.com/skaterdav85/validatorjs) (tested with version 3.9) and then download `dist/strategy.min.js` and include via a script tag: 12 | 13 | ```html 14 | 15 | ``` 16 | 17 | Or you can install with bower: 18 | 19 | ``` 20 | bower install react-validatorjs-strategy 21 | ``` 22 | 23 | ### Node 24 | 25 | Requires at least Node 4.0.0. 26 | 27 | On the command line type: 28 | 29 | ``` 30 | npm install react-validatorjs-strategy 31 | ``` 32 | 33 | Then in your JavaScript file: 34 | 35 | ```javascript 36 | var strategy = require('react-validatorjs-strategy') 37 | ``` 38 | 39 | This also works if you're using `webpack` or `browserify` to compile your React components. 40 | 41 | ## Usage 42 | 43 | A working example containing the below can be found at . 44 | 45 | ### In React components 46 | 47 | `react-validation-mixin` requires a component to be validated to have `this.validatorTypes` defined. To create this, call `strategy.createSchema` either in the constructor if you're using classes, or in a function which is run very early, such as `getInitialState`. This method takes one required parameter and two optional ones: 48 | 49 | ```javascript 50 | this.validatorTypes = strategy.createSchema( 51 | // First parameter is a list of rules for each element name 52 | { 53 | name: 'required', 54 | email: 'required|email', 55 | age: 'min:18' 56 | }, 57 | // Second parameter is optional and is a list of custom error messages for elements 58 | { 59 | "required.email": "Without an :attribute we can't reach you!" 60 | } 61 | // Third parameter is also optional; a callback that takes the validator instance created 62 | // and can be used to call methods on it. This is run at the point of validation. 63 | function (validator) { 64 | validator.lang = 'ru'; 65 | } 66 | ); 67 | ``` 68 | 69 | To call the validation on for example a form submission: 70 | 71 | ```javascript 72 | handleSubmit = function (e) { 73 | e.preventDefault(); 74 | 75 | this.props.validate(function (error) { 76 | if (!error) { 77 | // Submit the data 78 | } 79 | }); 80 | }, 81 | ``` 82 | 83 | The use of this strategy makes no difference to how the validation is handled in the render method, but just for the sake of completeness, triggering the validation on blur and then rendering any validation messages under the element: 84 | 85 | ```html 86 | 92 | 93 | {this.props.getValidationMessages('name')} 94 | ``` 95 | 96 | Then after the component is defined: 97 | 98 | ```javascript 99 | Component = validation(strategy)(Component); 100 | ``` 101 | 102 | #### Validating onChange 103 | 104 | I prefer to validate on the change event of an input to get immediate feedback. However, this has a problem. If for example, you're validating for an email address, as soon as the user enters one character the field will be flagged up as invalid, even though they've not yet had a chance to enter valid data. What ideally should happen is that the field is not validated for the first time until it is blurred out and from then on, any change should be validated immediately. 105 | 106 | To achieve this, there is another way of creating validation schemas; `createInactiveSchema`. This is called in exactly the same way, but all rules are turned off by default until `activateRule` is enabled, which should be called with `onBlur`. 107 | 108 | An example: 109 | 110 | ```javascript 111 | this.validatorTypes = strategy.createInactiveSchema(...); 112 | ``` 113 | 114 | Then the events bound to the element have to be changed slightly: 115 | 116 | ```html 117 | 125 | ``` 126 | 127 | These methods have to be created in the component: 128 | 129 | ```javascript 130 | /** 131 | * Activate the validation rule for the element on blur 132 | * 133 | * @param {Event} e 134 | */ 135 | activateValidation(e) { 136 | strategy.activateRule(this.validatorTypes, e.target.name); 137 | this.props.handleValidation(e.target.name)(e); 138 | }, 139 | /** 140 | * Set the state of the changed variable and then when set, call validator 141 | * 142 | * @param {Event} e 143 | */ 144 | handleChange(e) { 145 | var state = {}; 146 | state[e.target.name] = e.target.value; 147 | 148 | this.setState(state, () => { 149 | this.props.handleValidation(e.target.name)(e); 150 | }); 151 | }, 152 | ``` 153 | 154 | Submitting the whole form (when `this.props.validate` is called) works the same way; it automatically activates all rules. 155 | 156 | ### Registering Custom Validation Rules 157 | 158 | The Validator, accessible through the 3rd parameter of strategy.createSchema, enables [registering custom validations](https://github.com/skaterdav85/validatorjs#registering-custom-validation-rules). 159 | 160 | ```javascript 161 | this.validatorTypes = strategy.createInactiveSchema({ 162 | username: 'required|usernameAvailable', 163 | }, { 164 | 165 | }, function(validator) { 166 | validator.constructor.registerAsync('usernameAvailable', function(username, attribute, req, passes) { 167 | // do your database/api checks here etc 168 | // then call the `passes` method where appropriate: 169 | passes(); // if username is available 170 | passes(false, 'Username has already been taken.'); 171 | }); 172 | } 173 | ``` 174 | 175 | ### On the server (e.g. in Express) 176 | 177 | The validation can also be used isomorphically both in the browser in React components and on the server. This is done by creating the schema in the same way and then calling `validateServer` which returns a promise; the rejection of which can be handled by an error handler. Because the rejection returns an instance of `strategy.Error` it can be easily identified. 178 | 179 | As an example in Express: 180 | 181 | ```javascript 182 | app.post('/contact', function (req, res, next) { 183 | var schema = strategy.createSchema(...); 184 | 185 | strategy.validateServer(req.body, schema).then(function () { 186 | // Submit the data 187 | }) 188 | .catch(next); 189 | } 190 | 191 | /** 192 | * If a validation error, output a 400 JSON response containing the error messages. 193 | * Otherwise, use the default error handler. 194 | */ 195 | app.use(function (err, req, res, next) { 196 | if (err instanceof strategy.Error) { 197 | res.status(400).json(err.errors); 198 | } else { 199 | next(err, req, res); 200 | } 201 | }); 202 | ``` 203 | 204 | Using this method also activates all rules if `createInactiveSchema` was used. 205 | 206 | ## Testing 207 | 208 | Simply clone the repository, run `npm install` and then run `npm test`. The tests are in `tests/strategySpec.js`. 209 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-validatorjs-strategy", 3 | "description": "Strategy for using validatorjs with react-validation-mixin", 4 | "authors": [ 5 | "Dan Moore" 6 | ], 7 | "dependencies": { 8 | "validatorjs": "3.9.0" 9 | }, 10 | "license": "Unlicense", 11 | "keywords": [ 12 | "react", 13 | "validation", 14 | "strategy" 15 | ], 16 | "homepage": "https://github.com/TheChech/react-validatorjs-strategy", 17 | "moduleType": [ 18 | "amd", 19 | "globals", 20 | "node" 21 | ], 22 | "ignore": [ 23 | "**/.*", 24 | "node_modules", 25 | "bower_components", 26 | "tests" 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /dist/strategy.min.js: -------------------------------------------------------------------------------- 1 | (function(global,factory){if(typeof define==="function"&&define.amd){define(["module","validatorjs"],factory)}else if(typeof exports!=="undefined"){factory(module,require("validatorjs"))}else{var mod={exports:{}};factory(mod,global.validatorjs);global.strategy=mod.exports}})(this,function(module,Validator){"use strict";module.exports={createSchema:function(rules,messages,callback){return{rules:rules,messages:messages,callback:callback}},createInactiveSchema:function(rules,messages,callback){var schema=this.createSchema(rules,messages,callback);schema.activeRules=[];return schema},activateRule:function(schema,rule){if(typeof schema.activeRules!=="undefined"&&schema.activeRules.indexOf(rule)===-1){schema.activeRules.push(rule)}},createValidator:function(data,schema,forceActive){var rules={};if(typeof schema.activeRules!=="undefined"){if(forceActive){schema.activeRules=Object.keys(schema.rules)}for(var i in schema.activeRules){var ruleName=schema.activeRules[i];rules[ruleName]=schema.rules[ruleName]}}else{rules=schema.rules}var validator=new Validator(data,rules,schema.messages);if(typeof schema.callback==="function"){schema.callback(validator)}return validator},validate:function(data,schema,options,callback){var forceActive=!options.key;var validator=this.createValidator(data,schema,forceActive);var getErrors=function(){if(options.key){options.prevErrors[options.key]=validator.errors.get(options.key);callback(options.prevErrors)}else{callback(validator.errors.all())}};validator.checkAsync(getErrors,getErrors)},validateServer:function(data,schema){var validator=this.createValidator(data,schema,true);var Error=this.Error;return new Promise(function(resolve,reject){validator.checkAsync(resolve,function(){var e=new Error("A validation error occurred");e.errors=validator.errors.all();reject(e)})})},Error:function(message){this.message=message;this.errors={}}}}); -------------------------------------------------------------------------------- /lib/strategy.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Validate using the validatorjs library as a strategy for react-validation-mixin 3 | * 4 | * @see https://github.com/skaterdav85/validatorjs 5 | * @see https://jurassix.gitbooks.io/docs-react-validation-mixin/content/overview/strategies.html 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var Validator = require('validatorjs'); 11 | 12 | module.exports = { 13 | /** 14 | * Used to create this.validatorTypes in a React component and to be passed to validate or validateServer 15 | * 16 | * @param {Object} rules List of rules as specified by validatorjs 17 | * @param {Object} messages Optional list of custom messages as specified by validatorjs 18 | * @param {Function} callback if specified, called to allow customisation of validator 19 | * @returns {Object} 20 | */ 21 | createSchema: function (rules, messages, callback) { 22 | return { 23 | rules: rules, 24 | messages: messages, 25 | callback: callback 26 | }; 27 | }, 28 | /** 29 | * Same as createSchema, but the rules are disabled until activateRule is called 30 | * 31 | * @param {Object} rules List of rules as specified by validatorjs 32 | * @param {Object} messages Optional list of custom messages as specified by validatorjs 33 | * @param {Function} callback if specified, called to allow customisation of validator 34 | * @returns {Object} 35 | */ 36 | createInactiveSchema: function (rules, messages, callback) { 37 | var schema = this.createSchema(rules, messages, callback); 38 | schema.activeRules = []; 39 | 40 | return schema; 41 | }, 42 | /** 43 | * Active a specific rule 44 | * 45 | * @param {Object} schema As created by createInactiveSchema 46 | * @param {Object} rule Name of the rule as a key in schema.rules 47 | */ 48 | activateRule: function (schema, rule) { 49 | if (typeof schema.activeRules !== 'undefined' && schema.activeRules.indexOf(rule) === -1) { 50 | schema.activeRules.push(rule); 51 | } 52 | }, 53 | /** 54 | * Create a validator from submitted data and a schema 55 | * 56 | * @param {Object} data The data submitted 57 | * @param {Object} schema Contains rules and custom error messages 58 | * @param {Boolean} forceActive Whether to force all rules to be active even if not activated 59 | * @returns {Validator} 60 | */ 61 | createValidator: function (data, schema, forceActive) { 62 | var rules = {}; 63 | 64 | // Only add active rules to the validator if an initially inactive schema has been created. 65 | if (typeof schema.activeRules !== 'undefined') { 66 | // Force all rules to be active if specified 67 | if (forceActive) { 68 | schema.activeRules = Object.keys(schema.rules); 69 | } 70 | 71 | for (var i in schema.activeRules) { 72 | var ruleName = schema.activeRules[i]; 73 | 74 | rules[ruleName] = schema.rules[ruleName]; 75 | } 76 | } else { 77 | rules = schema.rules; 78 | } 79 | 80 | var validator = new Validator(data, rules, schema.messages); 81 | 82 | // If a callback has been specified on the schema, call it to allow customisation of the validator 83 | if (typeof schema.callback === 'function') { 84 | schema.callback(validator); 85 | } 86 | 87 | return validator; 88 | }, 89 | /** 90 | * Called by react-validation-mixin 91 | * 92 | * @param {Object} data The data submitted 93 | * @param {Object} schema Contains rules and custom error messages 94 | * @param {Object} options Contains name of element being validated and previous errors 95 | * @param {Function} callback Called and passed the errors after validation 96 | */ 97 | validate: function (data, schema, options, callback) { 98 | // If the whole form has been submitted, then activate all rules 99 | var forceActive = !options.key; 100 | var validator = this.createValidator(data, schema, forceActive); 101 | 102 | var getErrors = function () { 103 | // If a single element is being validated, just get those errors. 104 | // Otherwise get all of them. 105 | if (options.key) { 106 | options.prevErrors[options.key] = validator.errors.get(options.key); 107 | callback(options.prevErrors); 108 | } else { 109 | callback(validator.errors.all()); 110 | } 111 | }; 112 | 113 | // Run the validator asynchronously in case any async rules have been a`dded 114 | validator.checkAsync(getErrors, getErrors); 115 | }, 116 | /** 117 | * Validate server-side returning a Promise to easier handle results. 118 | * All inactive rules will be forced to activate. 119 | * 120 | * @param {Object} data The data submitted 121 | * @param {Object} schema Contains rules and custom error messages 122 | * @returns {Promise} 123 | */ 124 | validateServer: function (data, schema) { 125 | var validator = this.createValidator(data, schema, true); 126 | var Error = this.Error; 127 | 128 | return new Promise(function (resolve, reject) { 129 | validator.checkAsync(resolve, function () { 130 | var e = new Error('A validation error occurred'); 131 | e.errors = validator.errors.all(); 132 | 133 | reject(e); 134 | }); 135 | }); 136 | }, 137 | /** 138 | * Error class. Created by validateServer when validation fails. 139 | * Exists so that middleware can check it with instanceof: if (err instanceof strategy.Error) 140 | * 141 | * @property {Object} errors Contains the error messages by field name. 142 | */ 143 | Error: function (message) { 144 | this.message = message; 145 | this.errors = {}; 146 | } 147 | }; 148 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-validatorjs-strategy", 3 | "version": "0.2.0", 4 | "description": "Strategy for using validatorjs with react-validation-mixin", 5 | "main": "./lib/strategy.js", 6 | "dependencies": { 7 | "validatorjs": "3.9.0" 8 | }, 9 | "devDependencies": { 10 | "babel-cli": "^6.5.1", 11 | "babel-plugin-transform-es2015-modules-umd": "^6.5.0", 12 | "jasmine-core": "^2.2.0", 13 | "jasmine-node": "*", 14 | "onchange": "^2.0.0", 15 | "uglify-js": "^2.6.1" 16 | }, 17 | "scripts": { 18 | "start": "npm install && npm run build && npm run watch", 19 | "watch": "onchange './lib/strategy.js' -- npm run build", 20 | "test": "jasmine-node tests", 21 | "build": "npm run build:umd", 22 | "build:umd": "babel ./lib/strategy.js --plugins transform-es2015-modules-umd | uglifyjs -o ./dist/strategy.min.js --" 23 | }, 24 | "repository": { 25 | "type": "git", 26 | "url": "https://github.com/TheChech/react-validatorjs-strategy.git" 27 | }, 28 | "keywords": [ 29 | "react", 30 | "validation", 31 | "strategy" 32 | ], 33 | "author": "Dan Moore", 34 | "license": "Unlicense", 35 | "bugs": { 36 | "url": "https://github.com/TheChech/react-validatorjs-strategy/issues" 37 | }, 38 | "homepage": "https://github.com/TheChech/react-validatorjs-strategy#readme", 39 | "engines": { 40 | "node": ">= 4.0.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tests/strategySpec.js: -------------------------------------------------------------------------------- 1 | var Validator = require('validatorjs'); 2 | 3 | describe('strategy', function() { 4 | beforeEach(function () { 5 | this.strategy = require('../lib/strategy'); 6 | 7 | this.rules = { 8 | name: 'required', 9 | email: 'required|email', 10 | confirm_email: 'required|email' 11 | }; 12 | 13 | this.messages = { 14 | 'email.email': 'This is not a valid email address' 15 | }; 16 | 17 | this.schemaCallback = jasmine.createSpy('schema.callback'); 18 | 19 | this.validateCallback = jasmine.createSpy('validateCallback'); 20 | }); 21 | 22 | it('should create a schema containing the rules, messages and callback', function () { 23 | var schema = this.strategy.createSchema(this.rules, this.messages, this.schemaCallback); 24 | 25 | expect(schema.rules).toEqual(this.rules); 26 | expect(schema.messages).toEqual(this.messages); 27 | expect(schema.callback).toEqual(this.schemaCallback); 28 | expect(schema.activeRules).not.toBeDefined(); 29 | }); 30 | 31 | it('should create an inactive schema containing the rules, messages and schemaCallback', function () { 32 | var schema = this.strategy.createInactiveSchema(this.rules, this.messages, this.schemaCallback); 33 | 34 | expect(schema.rules).toEqual(this.rules); 35 | expect(schema.messages).toEqual(this.messages); 36 | expect(schema.callback).toEqual(this.schemaCallback); 37 | expect(schema.activeRules).toEqual([]); 38 | }); 39 | 40 | it('should activate a rule', function () { 41 | var schema = this.strategy.createInactiveSchema(this.rules, this.messages, this.schemaCallback); 42 | 43 | this.strategy.activateRule(schema, 'name'); 44 | expect(schema.activeRules).toEqual(['name']); 45 | 46 | this.strategy.activateRule(schema, 'email'); 47 | expect(schema.activeRules).toEqual(['name', 'email']); 48 | }); 49 | 50 | describe('validation', function () { 51 | beforeEach(function () { 52 | this.data = { 53 | name: 'Valid name', 54 | email: 'not-an-email-address', 55 | confirm_email: 'also-invalid' 56 | }; 57 | }); 58 | 59 | afterEach(function () { 60 | expect(this.schemaCallback).toHaveBeenCalledWith(jasmine.any(Validator)); 61 | }); 62 | 63 | describe('client-side', function () { 64 | describe('with a normal schema', function () { 65 | beforeEach(function () { 66 | this.schema = this.strategy.createSchema(this.rules, this.messages, this.schemaCallback); 67 | }); 68 | 69 | it('should validate a single invalid element with the custom message', function () { 70 | this.strategy.validate( 71 | this.data, 72 | this.schema, 73 | { 74 | key: 'email', 75 | prevErrors: {} 76 | }, 77 | this.validateCallback 78 | ); 79 | 80 | expect(this.validateCallback).toHaveBeenCalledWith({ 81 | email: ['This is not a valid email address'] 82 | }); 83 | }); 84 | 85 | it('should validate a single valid element', function () { 86 | this.data.email = 'test@example.com'; 87 | 88 | this.strategy.validate( 89 | this.data, 90 | this.schema, 91 | { 92 | key: 'email', 93 | prevErrors: {} 94 | }, 95 | this.validateCallback 96 | ); 97 | 98 | expect(this.validateCallback).toHaveBeenCalledWith({ 99 | email: [] 100 | }); 101 | }); 102 | 103 | it('should validate all elements with invalid values', function () { 104 | this.strategy.validate( 105 | this.data, 106 | this.schema, 107 | { 108 | key: undefined, 109 | prevErrors: {} 110 | }, 111 | this.validateCallback 112 | ); 113 | 114 | expect(this.validateCallback).toHaveBeenCalledWith({ 115 | email: ['This is not a valid email address'], 116 | confirm_email: ['The confirm email format is invalid.'] 117 | }); 118 | }); 119 | 120 | it('should validate all elements with valid values', function () { 121 | this.data.email = 'test@example.com'; 122 | this.data.confirm_email = 'test@example.com'; 123 | 124 | this.strategy.validate( 125 | this.data, 126 | this.schema, 127 | { 128 | key: undefined, 129 | prevErrors: {} 130 | }, 131 | this.validateCallback 132 | ); 133 | 134 | expect(this.validateCallback).toHaveBeenCalledWith({}); 135 | }); 136 | }); 137 | 138 | describe('with an inactive schema', function () { 139 | beforeEach(function () { 140 | this.schema = this.strategy.createInactiveSchema(this.rules, this.messages, this.schemaCallback); 141 | }); 142 | 143 | it('should ignore an element if it has not been activated', function () { 144 | this.strategy.validate( 145 | this.data, 146 | this.schema, 147 | { 148 | key: 'email', 149 | prevErrors: {} 150 | }, 151 | this.validateCallback 152 | ); 153 | 154 | expect(this.validateCallback).toHaveBeenCalledWith({ 155 | email: [] 156 | }); 157 | }); 158 | 159 | it('should validate an element if its rule has been activated', function () { 160 | this.strategy.activateRule(this.schema, 'email'); 161 | 162 | this.strategy.validate( 163 | this.data, 164 | this.schema, 165 | { 166 | key: 'email', 167 | prevErrors: {} 168 | }, 169 | this.validateCallback 170 | ); 171 | 172 | expect(this.validateCallback).toHaveBeenCalledWith({ 173 | email: ['This is not a valid email address'] 174 | }); 175 | }); 176 | 177 | it('should ignore an element if another element\'s rule has been activated', function () { 178 | this.strategy.activateRule(this.schema, 'confirm_email'); 179 | 180 | this.strategy.validate( 181 | this.data, 182 | this.schema, 183 | { 184 | key: 'email', 185 | prevErrors: {} 186 | }, 187 | this.validateCallback 188 | ); 189 | 190 | expect(this.validateCallback).toHaveBeenCalledWith({ 191 | email: [] 192 | }); 193 | }); 194 | 195 | it('should validate all elements even if none of them have been activated', function () { 196 | this.strategy.validate( 197 | this.data, 198 | this.schema, 199 | { 200 | key: undefined, 201 | prevErrors: {} 202 | }, 203 | this.validateCallback 204 | ); 205 | 206 | expect(this.validateCallback).toHaveBeenCalledWith({ 207 | email: ['This is not a valid email address'], 208 | confirm_email: ['The confirm email format is invalid.'] 209 | }); 210 | }); 211 | }); 212 | }); 213 | 214 | describe('server-side', function () { 215 | beforeEach(function () { 216 | // An inactive schema to demonstrate that rules are activated automatically server-side 217 | this.schema = this.strategy.createInactiveSchema(this.rules, this.messages, this.schemaCallback); 218 | }); 219 | 220 | it('should reject a promise with error messages with invalid input', function (done) { 221 | this.strategy.validateServer(this.data, this.schema).catch((error) => { 222 | expect(error).toEqual(jasmine.any(this.strategy.Error)); 223 | 224 | expect(error.errors).toEqual({ 225 | email: ['This is not a valid email address'], 226 | confirm_email: ['The confirm email format is invalid.'] 227 | }); 228 | 229 | done(); 230 | }); 231 | }); 232 | 233 | it('should resolve a promise with no errors with valid input', function (done) { 234 | this.data.email = 'valid@gmail.com'; 235 | this.data.confirm_email = 'valid@gmail.com'; 236 | 237 | this.strategy.validateServer(this.data, this.schema).then(() => { 238 | done(); 239 | }); 240 | }); 241 | }); 242 | }); 243 | }); 244 | --------------------------------------------------------------------------------