diff --git a/dist/amd/can-define-validate-validatejs.js b/dist/amd/can-define-validate-validatejs.js new file mode 100644 index 0000000..4bfb4fe --- /dev/null +++ b/dist/amd/can-define-validate-validatejs.js @@ -0,0 +1,86 @@ +/*can-define-validate-validatejs@0.1.1#can-define-validate-validatejs*/ +define([ + 'require', + 'exports', + 'module', + 'can-validate', + 'can-validate-validatejs', + 'can-define', + 'can-util/js/each', + 'can-util/js/assign', + 'can-util/js/is-empty-object' +], function (require, exports, module) { + var formatErrors = require('can-validate').formatErrors; + var validate = require('can-validate-validatejs'); + var define = require('can-define'); + var each = require('can-util/js/each'); + var assign = require('can-util/js/assign'); + var isEmptyObject = require('can-util/js/is-empty-object'); + var getMapConstraints = function (Map) { + var constraints = {}; + each(Map.prototype._define.definitions, function (prop, key) { + if (prop.validate && !isEmptyObject(prop.validate)) { + constraints[key] = prop.validate; + } + }); + return constraints; + }; + var validateMap = function (Map, validator) { + var mapDefinition = Map.prototype._define; + Map.prototype.testSet = function () { + var values = {}; + var useNewObject = false; + if (arguments.length) { + if (typeof arguments[0] === 'object' && Boolean(arguments[0])) { + values = arguments[0]; + useNewObject = Boolean(arguments[1]); + } + if (typeof arguments[0] === 'string') { + values[arguments[0]] = arguments[1]; + } + if (useNewObject) { + values = new Map(values); + } else { + var mapClone = this.serialize(); + assign(mapClone, values); + values = mapClone; + } + return validator(values); + } else { + return this.errors(); + } + }; + Map.prototype.errors = function () { + var _errors = this._errors; + var errors; + if (arguments.length) { + var errorsObj = formatErrors(_errors, 'errors-object'); + errors = []; + each(arguments, function (key) { + [].push.apply(errors, errorsObj ? errorsObj[key] : []); + }); + errors = errors.length > 0 ? errors : undefined; + } else { + errors = _errors; + } + return errors; + }; + define.property(Map.prototype, '_errors', { + get: function () { + var errors = validator(this); + return errors; + } + }, mapDefinition.dataInitializers, mapDefinition.computedInitializers); + }; + var decorator = function (Map) { + var constraints = getMapConstraints(Map); + var validator = validate.many(constraints); + validateMap(Map, function (map) { + var errors = validator(map); + return formatErrors(errors, 'errors'); + }); + }; + decorator.validatejs = validate.validatejs; + module.exports = decorator; +}); +//# sourceMappingURL=can-define-validate-validatejs.js.map \ No newline at end of file diff --git a/dist/amd/can-define-validate-validatejs.js.map b/dist/amd/can-define-validate-validatejs.js.map new file mode 100644 index 0000000..023d358 --- /dev/null +++ b/dist/amd/can-define-validate-validatejs.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../../can-define-validate-validatejs.js"],"names":["formatErrors","require","validate","define","each","assign","isEmptyObject","getMapConstraints","Map","constraints","prototype","_define","definitions","prop","key","validateMap","validator","mapDefinition","testSet","values","useNewObject","arguments","length","Boolean","mapClone","serialize","errors","_errors","errorsObj","push","apply","undefined","property","get","dataInitializers","computedInitializers","decorator","many","map","validatejs","module","exports"],"mappings":";;;;;;;;;;;;IAAA,IAAIA,YAAA,GAAeC,OAAA,CAAQ,cAAR,EAAwBD,YAA3C,C;IACA,IAAIE,QAAA,GAAWD,OAAA,CAAQ,yBAAR,CAAf,C;IACA,IAAIE,MAAA,GAASF,OAAA,CAAQ,YAAR,CAAb,C;IACA,IAAIG,IAAA,GAAOH,OAAA,CAAQ,kBAAR,CAAX,C;IACA,IAAII,MAAA,GAASJ,OAAA,CAAQ,oBAAR,CAAb,C;IACA,IAAIK,aAAA,GAAgBL,OAAA,CAAQ,6BAAR,CAApB,C;IAEA,IAAIM,iBAAA,GAAoB,UAAUC,GAAV,EAAe;AAAA,QACnC,IAAIC,WAAA,GAAc,EAAlB,CADmC;AAAA,QAEnCL,IAAA,CAAKI,GAAA,CAAIE,SAAJ,CAAcC,OAAd,CAAsBC,WAA3B,EAAwC,UAAUC,IAAV,EAAgBC,GAAhB,EAAqB;AAAA,YACzD,IAAID,IAAA,CAAKX,QAAL,IAAiB,CAACI,aAAA,CAAcO,IAAA,CAAKX,QAAnB,CAAtB,EAAoD;AAAA,gBAChDO,WAAA,CAAYK,GAAZ,IAAmBD,IAAA,CAAKX,QAAxB,CADgD;AAAA,aADK;AAAA,SAA7D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,CAAlB,CAD0B;AAAA,QAE1B,IAAIQ,SAAA,GAAYd,QAAA,CAASmC,IAAT,CAAc5B,WAAd,CAAhB,CAF0B;AAAA,QAI1BM,WAAA,CAAYP,GAAZ,EAAiB,UAAU8B,GAAV,EAAe;AAAA,YAC5B,IAAIZ,MAAA,GAASV,SAAA,CAAUsB,GAAV,CAAb,CAD4B;AAAA,YAE5B,OAAOtC,YAAA,CAAa0B,MAAb,EAAqB,QAArB,CAAP,CAF4B;AAAA,SAAhC,EAJ0B;AAAA,KAA9B,C;IAUAU,SAAA,CAAUG,UAAV,GAAuBrC,QAAA,CAASqC,UAAhC,C;IAEAC,MAAA,CAAOC,OAAP,GAAiBL,SAAjB,C","file":"can-define-validate-validatejs.js"} \ No newline at end of file diff --git a/dist/cjs/can-define-validate-validatejs.js b/dist/cjs/can-define-validate-validatejs.js new file mode 100644 index 0000000..1def603 --- /dev/null +++ b/dist/cjs/can-define-validate-validatejs.js @@ -0,0 +1,74 @@ +/*can-define-validate-validatejs@0.1.1#can-define-validate-validatejs*/ +var formatErrors = require('can-validate').formatErrors; +var validate = require('can-validate-validatejs'); +var define = require('can-define'); +var each = require('can-util/js/each/each'); +var assign = require('can-util/js/assign/assign'); +var isEmptyObject = require('can-util/js/is-empty-object/is-empty-object'); +var getMapConstraints = function (Map) { + var constraints = {}; + each(Map.prototype._define.definitions, function (prop, key) { + if (prop.validate && !isEmptyObject(prop.validate)) { + constraints[key] = prop.validate; + } + }); + return constraints; +}; +var validateMap = function (Map, validator) { + var mapDefinition = Map.prototype._define; + Map.prototype.testSet = function () { + var values = {}; + var useNewObject = false; + if (arguments.length) { + if (typeof arguments[0] === 'object' && Boolean(arguments[0])) { + values = arguments[0]; + useNewObject = Boolean(arguments[1]); + } + if (typeof arguments[0] === 'string') { + values[arguments[0]] = arguments[1]; + } + if (useNewObject) { + values = new Map(values); + } else { + var mapClone = this.serialize(); + assign(mapClone, values); + values = mapClone; + } + return validator(values); + } else { + return this.errors(); + } + }; + Map.prototype.errors = function () { + var _errors = this._errors; + var errors; + if (arguments.length) { + var errorsObj = formatErrors(_errors, 'errors-object'); + errors = []; + each(arguments, function (key) { + [].push.apply(errors, errorsObj ? errorsObj[key] : []); + }); + errors = errors.length > 0 ? errors : undefined; + } else { + errors = _errors; + } + return errors; + }; + define.property(Map.prototype, '_errors', { + get: function () { + var errors = validator(this); + return errors; + } + }, mapDefinition.dataInitializers, mapDefinition.computedInitializers); +}; +var decorator = function (Map) { + var constraints = getMapConstraints(Map); + var validator = validate.many(constraints); + validateMap(Map, function (map) { + var errors = validator(map); + return formatErrors(errors, 'errors'); + }); +}; +decorator.validatejs = validate.validatejs; +module.exports = decorator; +//# sourceMappingURL=can-define-validate-validatejs.js.map \ No newline at end of file diff --git a/dist/cjs/can-define-validate-validatejs.js.map b/dist/cjs/can-define-validate-validatejs.js.map new file mode 100644 index 0000000..42e575c --- /dev/null +++ b/dist/cjs/can-define-validate-validatejs.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../../can-define-validate-validatejs.js"],"names":["formatErrors","require","validate","define","each","assign","isEmptyObject","getMapConstraints","Map","constraints","prototype","_define","definitions","prop","key","validateMap","validator","mapDefinition","testSet","values","useNewObject","arguments","length","Boolean","mapClone","serialize","errors","_errors","errorsObj","push","apply","undefined","property","get","dataInitializers","computedInitializers","decorator","many","map","validatejs","module","exports"],"mappings":";AAAA,IAAIA,YAAA,GAAeC,OAAA,CAAQ,cAAR,EAAwBD,YAA3C;AACA,IAAIE,QAAA,GAAWD,OAAA,CAAQ,yBAAR,CAAf,CADA;AAEA,IAAIE,MAAA,GAASF,OAAA,CAAQ,YAAR,CAAb,CAFA;AAGA,IAAIG,IAAA,GAAOH,OAAA,CAAQ,uBAAR,CAAX,CAHA;AAIA,IAAII,MAAA,GAASJ,OAAA,CAAQ,2BAAR,CAAb,CAJA;AAKA,IAAIK,aAAA,GAAgBL,OAAA,CAAQ,6CAAR,CAApB,CALA;AAOA,IAAIM,iBAAA,GAAoB,UAAU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EAA+B,SAA/B,EAA0C;AAAA,QACtCuB,GAAA,EAAK,YAAU;AAAA,YACX,IAAIP,MAAA,GAASV,SAAA,CAAU,IAAV,CAAb,CADW;AAAA,YAEX,OAAOU,MAAP,CAFW;AAAA,SADuB;AAAA,KAA1C,EAKGT,aAAA,CAAciB,gBALjB,EAKmCjB,aAAA,CAAckB,oBALjD,EAlDwC;AAAA,CAA5C,CAjBA;AA2EA,IAAIC,SAAA,GAAY,UAAS5B,GAAT,EAAc;AAAA,IAC1B,IAAIC,WAAA,GAAcF,iBAAA,CAAkBC,GAAlB,CAAlB,CAD0B;AAAA,IAE1B,IAAIQ,SAAA,GAAYd,QAAA,CAASmC,IAAT,CAAc5B,WAAd,CAAhB,CAF0B;AAAA,IAI1BM,WAAA,CAAYP,GAAZ,EAAiB,UAAU8B,GAAV,EAAe;AAAA,QAC5B,IAAIZ,MAAA,GAASV,SAAA,CAAUsB,GAAV,CAAb,CAD4B;AAAA,QAE5B,OAAOtC,YAAA,CAAa0B,MAAb,EAAqB,QAArB,CAAP,CAF4B;AAAA,KAAhC,EAJ0B;AAAA,CAA9B,CA3EA;AAqFAU,SAAA,CAAUG,UAAV,GAAuBrC,QAAA,CAASqC,UAAhC,CArFA;AAuFAC,MAAA,CAAOC,OAAP,GAAiBL,SAAjB","file":"can-define-validate-validatejs.js"} \ No newline at end of file diff --git a/dist/global/can-define-validate-validatejs.js b/dist/global/can-define-validate-validatejs.js new file mode 100644 index 0000000..22c09b0 --- /dev/null +++ b/dist/global/can-define-validate-validatejs.js @@ -0,0 +1,221 @@ +/*[global-shim-start]*/ +(function(exports, global, doEval) { + // jshint ignore:line + var origDefine = global.define; + + var get = function(name) { + var parts = name.split("."), + cur = global, + i; + for (i = 0; i < parts.length; i++) { + if (!cur) { + break; + } + cur = cur[parts[i]]; + } + return cur; + }; + var set = function(name, val) { + var parts = name.split("."), + cur = global, + i, + part, + next; + for (i = 0; i < parts.length - 1; i++) { + part = parts[i]; + next = cur[part]; + if (!next) { + next = cur[part] = {}; + } + cur = next; + } + part = parts[parts.length - 1]; + cur[part] = val; + }; + var useDefault = function(mod) { + if (!mod || !mod.__esModule) return false; + var esProps = { __esModule: true, default: true }; + for (var p in mod) { + if (!esProps[p]) return false; + } + return true; + }; + + var hasCjsDependencies = function(deps) { + return ( + deps[0] === "require" && deps[1] === "exports" && deps[2] === "module" + ); + }; + + var modules = + (global.define && global.define.modules) || + (global._define && global._define.modules) || + {}; + var ourDefine = (global.define = function(moduleName, deps, callback) { + var module; + if (typeof deps === "function") { + callback = deps; + deps = []; + } + var args = [], + i; + for (i = 0; i < deps.length; i++) { + args.push( + exports[deps[i]] + ? get(exports[deps[i]]) + : modules[deps[i]] || get(deps[i]) + ); + } + // CJS has no dependencies but 3 callback arguments + if (hasCjsDependencies(deps) || (!deps.length && callback.length)) { + module = { exports: {} }; + args[0] = function(name) { + return exports[name] ? get(exports[name]) : modules[name]; + }; + args[1] = module.exports; + args[2] = module; + } else if (!args[0] && deps[0] === "exports") { + // Babel uses the exports and module object. + module = { exports: {} }; + args[0] = module.exports; + if (deps[1] === "module") { + args[1] = module; + } + } else if (!args[0] && deps[0] === "module") { + args[0] = { id: moduleName }; + } + + global.define = origDefine; + var result = callback ? callback.apply(null, args) : undefined; + global.define = ourDefine; + + // Favor CJS module.exports over the return value + result = module && module.exports ? module.exports : result; + modules[moduleName] = result; + + // Set global exports + var globalExport = exports[moduleName]; + if (globalExport && !get(globalExport)) { + if (useDefault(result)) { + result = result["default"]; + } + set(globalExport, result); + } + }); + global.define.orig = origDefine; + global.define.modules = modules; + global.define.amd = true; + ourDefine("@loader", [], function() { + // shim for @@global-helpers + var noop = function() {}; + return { + get: function() { + return { prepareGlobal: noop, retrieveGlobal: noop }; + }, + global: global, + __exec: function(__load) { + doEval(__load.source, global); + } + }; + }); +})( + {}, + typeof self == "object" && self.Object == Object ? self : window, + function(__$source__, __$global__) { + // jshint ignore:line + eval("(function() { " + __$source__ + " \n }).call(__$global__);"); + } +); + +/*can-define-validate-validatejs@0.1.1#can-define-validate-validatejs*/ +define('can-define-validate-validatejs', [ + 'require', + 'exports', + 'module', + 'can-validate', + 'can-validate-validatejs', + 'can-define', + 'can-util/js/each/each', + 'can-util/js/assign/assign', + 'can-util/js/is-empty-object/is-empty-object' +], function (require, exports, module) { + var formatErrors = require('can-validate').formatErrors; + var validate = require('can-validate-validatejs'); + var define = require('can-define'); + var each = require('can-util/js/each/each'); + var assign = require('can-util/js/assign/assign'); + var isEmptyObject = require('can-util/js/is-empty-object/is-empty-object'); + var getMapConstraints = function (Map) { + var constraints = {}; + each(Map.prototype._define.definitions, function (prop, key) { + if (prop.validate && !isEmptyObject(prop.validate)) { + constraints[key] = prop.validate; + } + }); + return constraints; + }; + var validateMap = function (Map, validator) { + var mapDefinition = Map.prototype._define; + Map.prototype.testSet = function () { + var values = {}; + var useNewObject = false; + if (arguments.length) { + if (typeof arguments[0] === 'object' && Boolean(arguments[0])) { + values = arguments[0]; + useNewObject = Boolean(arguments[1]); + } + if (typeof arguments[0] === 'string') { + values[arguments[0]] = arguments[1]; + } + if (useNewObject) { + values = new Map(values); + } else { + var mapClone = this.serialize(); + assign(mapClone, values); + values = mapClone; + } + return validator(values); + } else { + return this.errors(); + } + }; + Map.prototype.errors = function () { + var _errors = this._errors; + var errors; + if (arguments.length) { + var errorsObj = formatErrors(_errors, 'errors-object'); + errors = []; + each(arguments, function (key) { + [].push.apply(errors, errorsObj ? errorsObj[key] : []); + }); + errors = errors.length > 0 ? errors : undefined; + } else { + errors = _errors; + } + return errors; + }; + define.property(Map.prototype, '_errors', { + get: function () { + var errors = validator(this); + return errors; + } + }, mapDefinition.dataInitializers, mapDefinition.computedInitializers); + }; + var decorator = function (Map) { + var constraints = getMapConstraints(Map); + var validator = validate.many(constraints); + validateMap(Map, function (map) { + var errors = validator(map); + return formatErrors(errors, 'errors'); + }); + }; + decorator.validatejs = validate.validatejs; + module.exports = decorator; +}); +/*[global-shim-end]*/ +(function(global) { // jshint ignore:line + global._define = global.define; + global.define = global.define.orig; +} +)(typeof self == "object" && self.Object == Object ? self : window); +//# sourceMappingURL=can-define-validate-validatejs.js.map \ No newline at end of file diff --git a/dist/global/can-define-validate-validatejs.js.map b/dist/global/can-define-validate-validatejs.js.map new file mode 100644 index 0000000..4bffcc3 --- /dev/null +++ b/dist/global/can-define-validate-validatejs.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../../can-define-validate-validatejs.js"],"names":["formatErrors","require","validate","define","each","assign","isEmptyObject","getMapConstraints","Map","constraints","prototype","_define","definitions","prop","key","validateMap","validator","mapDefinition","testSet","values","useNewObject","arguments","length","Boolean","mapClone","serialize","errors","_errors","errorsObj","push","apply","undefined","property","get","dataInitializers","computedInitializers","decorator","many","map","validatejs","module","exports"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAAA,IAAIA,YAAA,GAAeC,OAAA,CAAQ,cAAR,EAAwBD,YAA3C,C;IACA,IAAIE,QAAA,GAAWD,OAAA,CAAQ,yBAAR,CAAf,C;IACA,IAAIE,MAAA,GAASF,OAAA,CAAQ,YAAR,CAAb,C;IACA,IAAIG,IAAA,GAAOH,OAAA,CAAQ,uBAAR,CAAX,C;IACA,IAAII,MAAA,GAASJ,OAAA,CAAQ,2BAAR,CAAb,C;IACA,IAAIK,aAAA,GAAgBL,OAAA,CAAQ,6CAAR,CAApB,C;IAEA,IAAIM,iBAAA,GAAoB,UAAUC,GAAV,EAAe;AAAA,QACnC,IAAIC,WAAA,GAAc,EAAlB,CADmC;AAAA,QAEnCL,IAAA,CAAKI,GAAA,CAAIE,SAAJ,CAAcC,OAAd,CAAsBC,WAA3B,EAAwC,UAAUC,IAAV,EAAgBC,GAAhB,EAAqB;AAAA,YACzD,IAAID,IAAA,CAAKX,QAAL,IA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