Skip to content

Commit

Permalink
Merge pull request #4 from canjs/pass-function-for-config
Browse files Browse the repository at this point in the history
Pass function for config
  • Loading branch information
Macrofig committed Dec 15, 2015
2 parents e0383be + 909f7bb commit 0b98de1
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 238 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules
dist
docs
*.log
16 changes: 10 additions & 6 deletions can-validate.build.js
@@ -1,11 +1,15 @@
var canValidate = require('./can-validate/can-validate');
var validate = require('./can-validate/map/validate/validate');
var validateJsShim = require('./can-validate/shims/validatejs.shim');

module.exports = {
'can-validate': {
'can-validate': require('./can-validate/can-validate')
'can-validate': canValidate
},
map: {
validate: require('./can-validate/map/validate/validate')
'map': {
validate: validate
},
shims: {
'validatejs.shim': require('./can-validate/shims/validatejs.shim')
'shims': {
'validatejs.shim': validateJsShim
}
}
};
142 changes: 71 additions & 71 deletions can-validate/can-validate.js
Expand Up @@ -37,9 +37,8 @@
import can from 'can';

var processMapDefine = function (targetMap) {
var targetDefine = targetMap.define,
resp = {values: {}, opts: {}};

var targetDefine = targetMap.define;
var resp = {values: {}, opts: {}};
can.each(targetDefine, function (item, prop) {
resp.values[prop] = targetMap.attr(prop);
resp.opts[prop] = item.validate;
Expand All @@ -48,8 +47,8 @@ var processMapDefine = function (targetMap) {
return resp;
};

//add methods to can
var Validate = can.validate = can.Construct.extend({
// add methods to can
var Validate = can.Construct.extend({

/*
* @description The current validator ID to use when can.validate methods are called.
Expand All @@ -62,103 +61,103 @@ var Validate = can.validate = can.Construct.extend({
_validators: {},

/**
* @function can-validate.validator Validator
* @parent can-validate.utils
* @description Registers a library with can.validate. The last one registered is the default library.
* Override the default by changing `_validatorId` to the key of the desired registered library.
* ```js
* Validate.register('validatejs',validatejs);
* ```
* @param {string} id The key name of the validator library.
* @param {object|function} validator The validator libarary. Only pass instances.
*/
* @function can-validate.validator Validator
* @parent can-validate.utils
* @description Registers a library with can.validate. The last one registered is the default library.
* Override the default by changing `_validatorId` to the key of the desired registered library.
* ```js
* Validate.register('validatejs',validatejs);
* ```
* @param {string} id The key name of the validator library.
* @param {object|function} validator The validator libarary. Only pass instances.
*/
validator: function () {
return this._validators[this._validatorId];
return this._validators[this._validatorId];
},

/**
* @function can-validate.register Register
* @parent can-validate.utils
* @description Registers a library with can.validate. The last one registered is the default library.
* Override the default by changing `_validatorId` to the key of the desired registered library.
* ```js
* Validate.register('validatejs',validatejs);
* ```
* @param {string} id The key name of the validator library.
* @param {object|function} validator The validator libarary. Only pass instances.
*/
* @function can-validate.register Register
* @parent can-validate.utils
* @description Registers a library with can.validate. The last one registered is the default library.
* Override the default by changing `_validatorId` to the key of the desired registered library.
* ```js
* Validate.register('validatejs',validatejs);
* ```
* @param {string} id The key name of the validator library.
* @param {object|function} validator The validator libarary. Only pass instances.
*/
register: function (id, validator) {
this._validatorId = id;
this._validators[id] = validator;
},

/**
* @function can-validate.isValid Is Valid
* @parent can-validate.validators
* @description Registers a library with can.validate. The last one registered is the default library.
* Override the default by changing `_validatorId` to the key of the desired registered library.
* ```js
* Validate.isValid('', {}, 'myVal');
* ```
* @param {*} value A value of any type to validate.
* @param {object} options Validation config object, structure depends on the
* validation library used.
* @param {string} name Optional. The key name of the value.
*/
* @function can-validate.isValid Is Valid
* @parent can-validate.validators
* @description Registers a library with can.validate. The last one registered is the default library.
* Override the default by changing `_validatorId` to the key of the desired registered library.
* ```js
* Validate.isValid('', {}, 'myVal');
* ```
* @param {*} value A value of any type to validate.
* @param {object} options Validation config object, structure depends on the
* validation library used.
* @param {string} name Optional. The key name of the value.
*/
isValid: function () {
//!steal-remove-start
if (!this._validatorId ) {
can.dev.warn("A validator library is required for can.validate to work properly.");
if (!this._validatorId) {
can.dev.warn('A validator library is required for can.validate to work properly.');
}
//!steal-remove-end
return this.validator().isValid.apply(this, arguments);
},

/**
* @function can-validate.once Once
* @parent can-validate.validators
* @description Registers a library with can.validate. The last one registered is the default library.
* Override the default by changing `_validatorId` to the key of the desired registered library.
* ```js
* Validate.once('', {}, 'myVal');
* ```
* @param {*} value A value of any type to validate.
* @param {object} options Validation config object, structure depends on the
* validation library used.
* @param {string} name Optional. The key name of the value.
*/
* @function can-validate.once Once
* @parent can-validate.validators
* @description Registers a library with can.validate. The last one registered is the default library.
* Override the default by changing `_validatorId` to the key of the desired registered library.
* ```js
* Validate.once('', {}, 'myVal');
* ```
* @param {*} value A value of any type to validate.
* @param {object} options Validation config object, structure depends on the
* validation library used.
* @param {string} name Optional. The key name of the value.
*/
once: function () {
//!steal-remove-start
if (!this._validatorId ) {
can.dev.warn("A validator library is required for can.validate to work properly.");
if (!this._validatorId) {
can.dev.warn('A validator library is required for can.validate to work properly.');
}
//!steal-remove-end
return this.validator().once.apply(this, arguments);
},

/**
* @function can-validate.validate Validate
* @parent can-validate.validators
* @description
* ```js
* Validate.validate({},{});
* ```
* @param {object} values A map of the different objects to validate.
* @param {object} options Validation config object, structure depends on the
* validation library used.
*/
* @function can-validate.validate Validate
* @parent can-validate.validators
* @description
* ```js
* Validate.validate({},{});
* ```
* @param {object} values A map of the different objects to validate.
* @param {object} options Validation config object, structure depends on the
* validation library used.
*/
validate: function () {
var validateArgs = arguments;
//!steal-remove-start
if (!this._validatorId ) {
can.dev.warn("A validator library is required for can.validate to work properly.");
if (!this._validatorId) {
can.dev.warn('A validator library is required for can.validate to work properly.');
}
if ( typeof arguments[0] !== 'object' ) {
can.dev.warn("Attempting to pass single value to validate, use can.validator.once instead.");
if (typeof arguments[0] !== 'object') {
can.dev.warn('Attempting to pass single value to validate, use can.validator.once instead.');
}
//!steal-remove-end

//check if can.map was passed, verify if there are validate options
// check if can.map was passed, verify if there are validate options
// defined then validate all applicable props.
if (arguments[0] instanceof can.Map && arguments[0].define) {
var mapOptions = processMapDefine(arguments[0]);
Expand All @@ -167,8 +166,9 @@ var Validate = can.validate = can.Construct.extend({
validateArgs.push(mapOptions.opts);
}
return this.validator().validate.apply(this, validateArgs);

}
},{});
}, {});

can.validate = Validate;

export default can;
20 changes: 14 additions & 6 deletions can-validate/map/validate/demo.html
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>Memory Leak Test</title>
<title>can-validate Map Plugin Demo</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<style>
#app {
Expand Down Expand Up @@ -30,9 +30,13 @@ <h3 class="panel-title">Validation Test</h3>
<div class="panel-body">
<p>Validation happens when a property&rsquo;s setter is called.
In the following examples, this happens when the field&rsquo;s value changes.</p>
<div class="form-group">
<label for="test1">MyVal required?</label>
<input class="form-control" id="test" type="checkbox" can-value="myMap.isRequired">
</div>
<div class="form-group">
<label for="test1">MyVal</label>
<input class="form-control" id="test" type="text" can-value="myMap.myVal">
<input class="form-control" id="test1" type="text" can-value="myMap.myVal">
</div>
<div class="form-group">
<label for="test2">MyNum</label>
Expand Down Expand Up @@ -65,9 +69,8 @@ <h3 class="panel-title">Validation Test</h3>
myVal: {
value: 'hello',
validate: {
required: true,
length: {
minimum: 6
required: function () {
return this.attr('isRequired') || false;
}
}
},
Expand All @@ -77,8 +80,13 @@ <h3 class="panel-title">Validation Test</h3>
required: true,
numericality: {
greaterThan: 5
}
},
validateOnInit: true
}
},
isRequired: {
value: false,
type: 'boolean'
}
}
});
Expand Down

0 comments on commit 0b98de1

Please sign in to comment.