Skip to content

Commit 97354b7

Browse files
committed
Stable Version 1.2.0.
Closes #5.
1 parent beef500 commit 97354b7

File tree

6 files changed

+76
-57
lines changed

6 files changed

+76
-57
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
##### 1.2.0 - 31 March 2015
2+
3+
###### Backwards compatible API changes
4+
- #5 - feature: supporting "composite rules"
5+
16
##### 1.1.1 - 27 March 2015
27

38
###### Backwards compatible bug fixes

dist/js-data-schema.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* js-data-schema
3-
* @version 1.1.1 - Homepage <https://github.com/js-data/js-data-schema/>
3+
* @version 1.2.0 - Homepage <https://github.com/js-data/js-data-schema/>
44
* @author Jason Dobry <[email protected]>
55
* @copyright (c) 2013-2015 Jason Dobry
66
* @license MIT <https://github.com/js-data/js-data-schema/blob/master/LICENSE>
@@ -498,20 +498,21 @@ return /******/ (function(modules) { // webpackBootstrap
498498

499499
function _executeRulesSync(targetKey, options, errors, value, key) {
500500
var _this = this;
501+
501502
var nestedKey = targetKey + (targetKey.length ? "." : "") + key;
502503

503504
if (utils.isObject(value)) {
504-
var err = _validateSync.apply(_this, [nestedKey, value, options]);
505+
var err = _validateSync.apply(this, [nestedKey, value, options]);
505506
if (err) {
506507
errors[key] = err;
507508
}
508509
} else {
509-
var schemaRules = utils.get(_this.schema, nestedKey);
510+
var schemaRules = utils.get(this.schema, nestedKey);
510511
if (!utils.isObject(schemaRules)) {
511512
return;
512513
} else if (schemaRules.nullable === true) {
513514
var nullable = this.parent.rules.nullable || defaultRules.nullable;
514-
var nErr = nullable(value, true);
515+
var nErr = nullable.call(options.ctx, value, true);
515516

516517
if (nErr === null) {
517518
return;
@@ -520,7 +521,7 @@ return /******/ (function(modules) { // webpackBootstrap
520521
utils.forOwn(schemaRules, function (ruleValue, ruleKey) {
521522
var rule = _this.parent.rules[ruleKey] || defaultRules[ruleKey];
522523
if (!rule.async) {
523-
var err = rule(value, ruleValue);
524+
var err = rule.call(options.ctx, value, ruleValue);
524525
if (err) {
525526
if (!errors[key]) {
526527
errors[key] = {
@@ -538,17 +539,18 @@ return /******/ (function(modules) { // webpackBootstrap
538539
* @see Schema#validateSync
539540
*/
540541
function _validateSync(targetKey, attrs, options) {
541-
var errors = {};
542542
var _this = this;
543543

544+
var errors = {};
545+
544546
try {
545547
// Validate present attributes
546548
utils.forOwn(attrs, function (value, key) {
547549
_executeRulesSync.call(_this, targetKey, options, errors, value, key);
548550
});
549551
// Validate missing attributes
550552
if (!options.ignoreMissing) {
551-
var schema = targetKey ? utils.get(_this.schema, targetKey) || {} : _this.schema;
553+
var schema = targetKey ? utils.get(this.schema, targetKey) || {} : this.schema;
552554
var missing = utils.difference(utils.keys(schema), utils.keys(attrs));
553555
missing = utils.pick(this.schema, missing);
554556
missing = utils.map(missing, function () {
@@ -570,6 +572,7 @@ return /******/ (function(modules) { // webpackBootstrap
570572

571573
function _executeRules(options, value, key, prefix, errors, deepQueue, ruleQueue) {
572574
var _this = this;
575+
573576
var nestedKey = prefix + key;
574577

575578
if (utils.isObject(value)) {
@@ -580,12 +583,12 @@ return /******/ (function(modules) { // webpackBootstrap
580583
};
581584
})(nestedKey, value);
582585
} else {
583-
var schemaRules = utils.get(_this.schema, nestedKey);
586+
var schemaRules = utils.get(this.schema, nestedKey);
584587
if (!utils.isObject(schemaRules)) {
585588
return;
586589
} else if (schemaRules.nullable === true) {
587-
var nullable = this.parent.rules.nullable || defaultRules.nullable,
588-
nErr = nullable(value, true);
590+
var nullable = this.parent.rules.nullable || defaultRules.nullable;
591+
var nErr = nullable.call(options.ctx, value, true);
589592

590593
if (nErr === null) {
591594
return;
@@ -595,16 +598,16 @@ return /******/ (function(modules) { // webpackBootstrap
595598
var rule = _this.parent.rules[ruleKey] || defaultRules[ruleKey];
596599
// Asynchronous rules get added to the queue
597600
if (rule.async) {
598-
ruleQueue[ruleKey + "_" + ruleValue] = (function (r, key, val, rVal) {
601+
ruleQueue["" + ruleKey + "_" + ruleValue] = (function (r, key, val, rVal) {
599602
return function (next) {
600-
r(val, rVal, function (err) {
603+
r.call(options.ctx, val, rVal, function (err) {
601604
next(null, { err: err, key: key });
602605
});
603606
};
604607
})(rule, key, value, ruleValue);
605608
} else {
606609
// Get results of synchronous rules immediately
607-
var err = rule(value, ruleValue);
610+
var err = rule.call(options.ctx, value, ruleValue);
608611
if (err) {
609612
if (!errors[key]) {
610613
errors[key] = {
@@ -622,8 +625,9 @@ return /******/ (function(modules) { // webpackBootstrap
622625
* @see Schema#validate
623626
*/
624627
function _validate(targetKey, attrs, options, cb) {
625-
var errors = {};
626628
var _this = this;
629+
630+
var errors = {};
627631
var prefix = targetKey + (targetKey.length ? "." : "");
628632
var deepQueue = {};
629633
var ruleQueue = {};
@@ -637,7 +641,7 @@ return /******/ (function(modules) { // webpackBootstrap
637641

638642
// Validate missing attributes
639643
if (!options.ignoreMissing) {
640-
var schema = targetKey ? utils.get(_this.schema, targetKey) || {} : _this.schema;
644+
var schema = targetKey ? utils.get(this.schema, targetKey) || {} : this.schema;
641645
var missing = utils.difference(utils.keys(schema), utils.keys(attrs));
642646

643647
missing = utils.pick(this.schema, missing);
@@ -726,6 +730,11 @@ return /******/ (function(modules) { // webpackBootstrap
726730
});
727731
}
728732

733+
var errors = {
734+
a: "Schema#validateSync(attrs[, options]): ",
735+
b: "Schema#validate(attrs[, options], cb): "
736+
};
737+
729738
var Schema = (function () {
730739
function Schema(name, schema, parent) {
731740
_classCallCheck(this, Schema);
@@ -745,11 +754,12 @@ return /******/ (function(modules) { // webpackBootstrap
745754
value: function validateSync(attrs, options) {
746755
options = options ? options === true ? { ignoreMissing: true } : options : {};
747756
if (!utils.isObject(attrs)) {
748-
throw new Error("Schema#validateSync(attrs[, options]): attrs: Must be an object!");
757+
throw new Error("" + errors.a + "attrs: Must be an object!");
749758
} else if (!utils.isObject(options)) {
750-
throw new Error("Schema#validateSync(attrs[, options]): options: Must be an object!");
759+
throw new Error("" + errors.a + "options: Must be an object!");
751760
}
752-
return _validateSync.apply(this, ["", attrs, options]);
761+
options.ctx = attrs;
762+
return _validateSync.call(this, "", attrs, options);
753763
}
754764
},
755765
validate: {
@@ -760,14 +770,15 @@ return /******/ (function(modules) { // webpackBootstrap
760770
options = {};
761771
}
762772
if (!utils.isFunction(cb)) {
763-
throw new Error("Schema#validate(attrs[, options], cb): cb: Must be a function!");
773+
throw new Error("" + errors.b + "cb: Must be a function!");
764774
} else if (!utils.isObject(attrs)) {
765-
return cb(new Error("Schema#validate(attrs[, options], cb): attrs: Must be an object!"));
775+
return cb(new Error("" + errors.b + "attrs: Must be an object!"));
766776
} else if (!utils.isObject(options)) {
767-
return cb(new Error("Schema#validate(attrs[, options], cb): options: Must be an object!"));
777+
return cb(new Error("" + errors.b + "options: Must be an object!"));
768778
}
769779
options.first = true;
770-
_validate.apply(this, ["", attrs, options, cb]);
780+
options.ctx = attrs;
781+
_validate.call(this, "", attrs, options, cb);
771782
}
772783
},
773784
addDefaultsToTarget: {

0 commit comments

Comments
 (0)