You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Platform/wwwroot/lib/expressive.annotations.vali.../expressive.annotations.vali...

7 lines
15 KiB

/* expressive.annotations.validate.js - v2.7.0
* Client-side component of ExpressiveAnnotations - annotation-based conditional validation library.
* https://github.com/jwaliszko/ExpressiveAnnotations
*
* Copyright (c) 2014 Jarosław Waliszko
* Licensed MIT: http://opensource.org/licenses/MIT */
!function(e,t){"use strict";var n=t.ea,r={settings:{debug:!1,optimize:!0,enumsAsNumbers:!0,dependencyTriggers:"change keyup",apply:function(t){function n(){if(!s.isBool(r.settings.debug))throw"debug value must be a boolean (true or false)";if(!s.isBool(r.settings.optimize))throw"optimize value must be a boolean (true or false)";if(!s.isBool(r.settings.enumsAsNumbers))throw"enumsAsNumbers value must be a boolean (true or false)";if(!s.isString(r.settings.dependencyTriggers)&&null!==r.settings.dependencyTriggers&&void 0!==r.settings.dependencyTriggers)throw"dependencyTriggers value must be a string (multiple event types can be bound at once by including each one separated by a space), null or undefined"}function i(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])}i(r.settings,t),n(),e("form").each(function(){e(this).find("input, select, textarea").off(".expressive.annotations"),u.bindFields(this,!0)})}},addMethod:function(e,t){a.addMethod(e,t)},addValueParser:function(e,t){s.addValueParser(e,t)},noConflict:function(){return t.ea===this&&(t.ea=n),this}},i={dump:function(e){r.settings.debug&&console&&"function"==typeof console.log&&console.log(e)},warn:function(e){console&&"function"==typeof console.warn&&console.warn(e)},fail:function(e){console&&"function"==typeof console.error&&console.error(e)}},a={methods:{},addMethod:function(e,t){var n=this.methods[e];this.methods[e]=function(){return t.length===arguments.length?t.apply(this,arguments):"function"==typeof n?n.apply(this,arguments):t.apply(this,arguments)}},registerMethods:function(e){var t,n;this.initialize();for(t in this.methods)this.methods.hasOwnProperty(t)&&(n=this.methods[t],e[t]=n)},initialize:function(){this.addMethod("Now",function(){return Date.now()}),this.addMethod("Today",function(){return new Date((new Date).setHours(0,0,0,0)).getTime()}),this.addMethod("ToDate",function(e){return Date.parse(e)}),this.addMethod("Date",function(e,t,n){return new Date(new Date(e,t-1,n).setFullYear(e)).getTime()}),this.addMethod("Date",function(e,t,n,r,i,a){return new Date(new Date(e,t-1,n,r,i,a).setFullYear(e)).getTime()}),this.addMethod("TimeSpan",function(e,t,n,r){return 1e3*r+6e4*n+36e5*t+864e5*e}),this.addMethod("Length",function(e){return null!==e&&void 0!==e?e.length:0}),this.addMethod("Trim",function(t){return null!==t&&void 0!==t?e.trim(t):null}),this.addMethod("Concat",function(e,t){return[e,t].join("")}),this.addMethod("Concat",function(e,t,n){return[e,t,n].join("")}),this.addMethod("CompareOrdinal",function(e,t){return e===t?0:null!==e&&null===t?1:null===e&&null!==t?-1:e>t?1:-1}),this.addMethod("CompareOrdinalIgnoreCase",function(e,t){return e=null!==e&&void 0!==e?e.toLowerCase():null,t=null!==t&&void 0!==t?t.toLowerCase():null,this.CompareOrdinal(e,t)}),this.addMethod("StartsWith",function(e,t){return null!==e&&void 0!==e&&null!==t&&void 0!==t&&e.slice(0,t.length)===t}),this.addMethod("StartsWithIgnoreCase",function(e,t){return e=null!==e&&void 0!==e?e.toLowerCase():null,t=null!==t&&void 0!==t?t.toLowerCase():null,this.StartsWith(e,t)}),this.addMethod("EndsWith",function(e,t){return null!==e&&void 0!==e&&null!==t&&void 0!==t&&e.slice(-t.length)===t}),this.addMethod("EndsWithIgnoreCase",function(e,t){return e=null!==e&&void 0!==e?e.toLowerCase():null,t=null!==t&&void 0!==t?t.toLowerCase():null,this.EndsWith(e,t)}),this.addMethod("Contains",function(e,t){return null!==e&&void 0!==e&&null!==t&&void 0!==t&&e.indexOf(t)>-1}),this.addMethod("ContainsIgnoreCase",function(e,t){return e=null!==e&&void 0!==e?e.toLowerCase():null,t=null!==t&&void 0!==t?t.toLowerCase():null,this.Contains(e,t)}),this.addMethod("IsNullOrWhiteSpace",function(e){return null===e||!/\S/.test(e)}),this.addMethod("IsDigitChain",function(e){return/^[0-9]+$/.test(e)}),this.addMethod("IsNumber",function(e){return/^[+-]?(?:(?:[0-9]+)|(?:[0-9]+[eE][+-]?[0-9]+)|(?:[0-9]*\.[0-9]+(?:[eE][+-]?[0-9]+)?))$/.test(e)}),this.addMethod("IsEmail",function(e){return/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(e)}),this.addMethod("IsPhone",function(e){return/^(\+\s?)?((?!\+.*)\(\+?\d+([\s\-\.]?\d+)?\)|\d+)([\s\-\.]?(\(\d+([\s\-\.]?\d+)?\)|\d+))*(\s?(x|ext\.?)\s?\d+)?$/.test(e)}),this.addMethod("IsUrl",function(e){return/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/i.test(e)}),this.addMethod("IsRegexMatch",function(e,t){return null!==e&&void 0!==e&&null!==t&&void 0!==t&&new RegExp(t).test(e)}),this.addMethod("Guid",function(e){var t=s.guid.tryParse(e);if(t.error)throw t.msg;return t}),this.addMethod("Min",function(e){if(0===arguments.length)throw"no arguments";if(1===arguments.length&&s.isArray(e)){if(0===e.length)throw"empty sequence";return Math.min.apply(null,e)}return Math.min.apply(null,arguments)}),this.addMethod("Max",function(e){if(0===arguments.length)throw"no arguments";if(1===arguments.length&&s.isArray(e)){if(0===e.length)throw"empty sequence";return Math.max.apply(null,e)}return Math.max.apply(null,arguments)}),this.addMethod("Sum",function(e){if(0===arguments.length)throw"no arguments";var t,n,r=0;if(1===arguments.length&&s.isArray(e)){if(0===e.length)throw"empty sequence";for(t=0,n=e.length;t<n;t++)r+=parseFloat(e[t]);return r}for(t=0,n=arguments.length;t<n;t++)r+=parseFloat(arguments[t]);return r}),this.addMethod("Average",function(e){if(0===arguments.length)throw"no arguments";var t,n,r,i=new Array;if(1===arguments.length&&s.isArray(e)){if(0===e.length)throw"empty sequence";return t=this.Sum(e),t/e.length}for(n=0,r=arguments.length;n<r;n++)i.push(arguments[n]);return t=this.Sum(i),t/arguments.length})}},s={parsers:{},addValueParser:function(t,n){e.each(t.split(/\s+/),function(e,t){/\S/.test(t)&&(s.parsers[t]=n)})},array:{contains:function(e,t){for(var n=e.length;n--;)if(e[n]===t)return!0;return!1}},object:{keys:function(e){var t,n=[];for(t in e)e.hasOwnProperty(t)&&n.push(t);return n},tryParse:function(t){try{return e.parseJSON(t)}catch(e){return{error:!0,msg:"Given value was not recognized as a valid JSON. "+e}}}},string:{format:function(e,t){function n(e){return e=s.isObject(e)?JSON.stringify(e,null,4):e,e=s.isString(e)?e.replace(/\$/g,"$$$$"):e}function r(e,t,n){return e.replace(new RegExp("\\{"+n+"\\}","gm"),t)}var i;if(t instanceof Array){for(i=0;i<t.length;i++)e=r(e,n(t[i]),i);return e}for(i=0;i<arguments.length-1;i++)e=r(e,n(arguments[i+1]),i);return e},tryParse:function(e){return s.isString(e)?e:void 0!==e&&null!==e?e.toString():{error:!0,msg:"Given value was not recognized as a valid string."}}},bool:{tryParse:function(t){return s.isBool(t)?t:s.isString(t)&&(t=e.trim(t).toLowerCase(),"true"===t||"false"===t)?"true"===t:{error:!0,msg:"Given value was not recognized as a valid boolean."}}},number:{tryParse:function(e){function t(e){return s.isNumeric(parseFloat(e))&&isFinite(e)}return t(e)?parseFloat(e):{error:!0,msg:"Given value was not recognized as a valid number."}}},timespan:{tryParse:function(e){if(s.isTimeSpan(e)){var t=2,n=3,r=4,i=5,a=6,o=/(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/.exec(e),u="-"===o[1]?-1:1,d={days:s.number.tryParse(o[t]||0)*u,hours:s.number.tryParse(o[n]||0)*u,minutes:s.number.tryParse(o[r]||0)*u,seconds:s.number.tryParse(o[i]||0)*u,milliseconds:s.number.tryParse(o[a]||0)*u},l=d.milliseconds+1e3*d.seconds+6e4*d.minutes+36e5*d.hours+864e5*d.days;return l}return{error:!0,msg:"Given value was not recognized as a valid .NET style timespan string."}}},datetime:{tryParse:function(e){if(s.isDate(e))return e.getTime();if(s.isString(e)){var t=Date.parse(e);if(s.isNumeric(t))return t}return{error:!0,msg:"Given value was not recognized as a valid RFC 2822 or ISO 8601 date."}}},guid:{tryParse:function(e){return s.isGuid(e)?e.toUpperCase():{error:!0,msg:"Given value was not recognized as a valid guid - guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."}}},enumeration:{tryParse:function(e){return r.settings.enumsAsNumbers?s.number.tryParse(e):s.string.tryParse(e)}},isTimeSpan:function(e){return/(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/.test(e)},isNumeric:function(e){return"number"==typeof e&&!isNaN(e)},isDate:function(e){return e instanceof Date},isObject:function(e){return"object"==typeof e||e instanceof Object},isString:function(e){return"string"==typeof e||e instanceof String},isBool:function(e){return"boolean"==typeof e||e instanceof Boolean},isGuid:function(e){return/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(e)},isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)},tryParse:function(e,t,n,r){var a;if(null!==r&&void 0!==r){if(a=s.findValueParser(n,r),!a.error)return a(e,n);i.warn(a.msg)}return a=s.findValueParser(n,t),a.error?s.tryAutoParse(e,t):(i.warn(s.string.format("Overriden {0} type parsing runs for {1} field. All fields of {0} type are going to be parsed using your value parser. If such a behavior is unintentional, change the name of your value parser to one, which does not indicate at {0} (or any other) type name.",t,n)),a(e,n))},tryAutoParse:function(e,t){return s.hasOwnProperty(t)?s[t].tryParse(e):s.object.tryParse(e)},findValueParser:function(e,t){var n=s.parsers[t];return"function"==typeof n?n:{error:!0,msg:s.string.format("Custom value parser {0} not found. Consider its registration with ea.addValueParser(), or remove redundant ValueParser attribute from {1} model field.",t,e)}}},o={getPrefix:function(e){return void 0!==e&&null!==e?e.substr(0,e.lastIndexOf(".")+1):""},extractValue:function(t,n,r,a,o){function u(e){var t=e.attr("type");switch(t){case"checkbox":return e.length>2&&i.warn(s.string.format("DOM field {0} is ambiguous (unless custom value parser is provided).",e.attr("name"))),e.is(":checked");case"radio":return e.filter(":checked").val();default:return e.length>1&&i.warn(s.string.format("DOM field {0} is ambiguous (unless custom value parser is provided).",e.attr("name"))),e.val()}}var d,l,f,c;if(l=r+n,d=e(t).find(s.string.format(':input[name="{0}"]',l)),0===d.length)throw s.string.format("DOM field {0} not found.",l);if(f=u(d),null===f||void 0===f||""===f)return null;if(c=s.tryParse(f,a,l,o),null!==c&&void 0!==c&&c.error)throw s.string.format("DOM field {0} value conversion to {1} failed. {2}",l,a,c.msg);return c},deserializeObject:function(e,t,n,i,a,s){function o(e,t,n){var r,i,a,s,o;for(r=e.split("."),i=n,a=0;a<r.length-1;a++)e=r[a],s=/^([a-z_0-9]+)\[([0-9]+)\]$/i.exec(e),s?(e=s[1],o=s[2],i.hasOwnProperty(e)||(i[e]={}),i[e][o]={},i=i[e][o]):(i.hasOwnProperty(e)||(i[e]={}),i=i[e]);e=r[r.length-1],i[e]=t}var u,d,l,f,c={};for(u in t)t.hasOwnProperty(u)&&(d=t[u],f=a[u],l=this.extractValue(e,u,s,d,f),o(u,l,c));for(u in n)n.hasOwnProperty(u)&&(l=n[u],o(u,l,c));for(u in i)i.hasOwnProperty(u)&&(l=r.settings.enumsAsNumbers?i[u]:u.split(".").pop(),o(u,l,c));return c},adjustGivenValue:function(e,t,n){e="checkbox"===t.type?t.checked:e;var r=t.name.replace(n.prefix,""),a=n.parsersMap[r];if(null!==a&&void 0!==a){var o=s.findValueParser(t.name,a);if(!o.error)return o(e,t.name);i.warn(o.msg)}return e},ctxEval:function(e,t){return new Function("expression","context","with(context){return eval(expression)}")(e,t)}},u={referencesMap:[],collectReferences:function(e,t,n){var r,i;for(r=0;r<e.length;r++)i=n+e[r],i!==t&&(this.referencesMap[i]=this.referencesMap[i]||[],s.array.contains(this.referencesMap[i],t)||this.referencesMap[i].push(t))},validateReferences:function(t,n){var r,a,o,u;if(u=e(n).validate(),o=this.referencesMap[t],void 0!==o&&null!==o)for(i.dump(s.string.format("Validation triggered for following {0} dependencies: {1}.",t,o.join(", "))),r=o.length;r--;)a=e(n).find(s.string.format(':input[data-val][name="{0}"]',o[r])).not(u.settings.ignore),0!==a.length&&a.valid();else i.dump(s.string.format("No dependencies of {0} field detected.",t))},bindFields:function(t,n){if(null!==r.settings.dependencyTriggers&&void 0!==r.settings.dependencyTriggers&&""!==r.settings.dependencyTriggers){var a=[];e.each(r.settings.dependencyTriggers.split(/\s+/),function(e,t){/\S/.test(t)&&a.push(s.string.format("{0}.expressive.annotations",t))}),e(t).find("input, select, textarea").not(function(t,r){var i=e(r).hasClass("ea-triggers-bound");return e(r).addClass("ea-triggers-bound"),!n&&i}).on(a.join(" "),function(n){var r=e(this).attr("name");i.dump(s.string.format("Dependency validation trigger - {0} event, handled.",n.type)),u.validateReferences(r,t)})}}},d=function(t,n){var r={prefix:o.getPrefix(n.element.name),form:n.form};for(var i in n.params)n.params.hasOwnProperty(i)&&(r[i]=void 0!==n.params[i]?e.parseJSON(n.params[i]):{});n.message&&(n.messages[t]=function(e,t){var r,i,a,s;r=n.message;for(i in e.errFieldsMap)if(e.errFieldsMap.hasOwnProperty(i)){a=e.errFieldsMap[i],s=o.extractValue(e.form,i,e.prefix,"string",null);var u=new RegExp(a,"g");r=r.replace(u,s)}return r}),u.bindFields(n.form),u.collectReferences(s.object.keys(r.fieldsMap),n.element.name,r.prefix),n.rules[t]=r},l=function(e,t,n){if(e=o.adjustGivenValue(e,t,n),void 0!==e&&null!==e&&""!==e){var r=o.deserializeObject(n.form,n.fieldsMap,n.constsMap,n.enumsMap,n.parsersMap,n.prefix);return a.registerMethods(r),i.dump(s.string.format("AssertThat expression of {0} field:\n{1}\nwill be executed within following context (methods hidden):\n{2}",t.name,n.expression,r)),o.ctxEval(n.expression,r)}return!0},f=function(e,t,n){e=o.adjustGivenValue(e,t,n);var u,d,l="RequiredIf expression of {0} field:\n{1}\nwill be executed within following context (methods hidden):\n{2}";return r.settings.optimize||(d=o.deserializeObject(n.form,n.fieldsMap,n.constsMap,n.enumsMap,n.parsersMap,n.prefix),a.registerMethods(d),i.dump(s.string.format(l,t.name,n.expression,d)),u=o.ctxEval(n.expression,d)),void 0===e||null===e||""===e||!/\S/.test(e)&&!n.allowEmpty?void 0!==u?{valid:!u,condition:u}:(d=o.deserializeObject(n.form,n.fieldsMap,n.constsMap,n.enumsMap,n.parsersMap,n.prefix),a.registerMethods(d),i.dump(s.string.format(l,t.name,n.expression,d)),u=o.ctxEval(n.expression,d),{valid:!u,condition:u}):{valid:!0,condition:u}},c=" abcdefghijklmnopqrstuvwxyz";e.each(c.split(""),function(){var t=s.string.format("assertthat{0}",e.trim(this));e.validator.unobtrusive.adapters.add(t,["expression","fieldsMap","constsMap","enumsMap","parsersMap","errFieldsMap"],function(e){d(t,e)})}),e.each(c.split(""),function(){var t=s.string.format("requiredif{0}",e.trim(this));e.validator.unobtrusive.adapters.add(t,["expression","fieldsMap","constsMap","enumsMap","parsersMap","errFieldsMap","allowEmpty"],function(e){d(t,e)})}),e.each(c.split(""),function(){var t=s.string.format("assertthat{0}",e.trim(this));e.validator.addMethod(t,function(t,n,r){try{var a=l(t,n,r);return e(n).trigger("eavalid",["assertthat",a,r.expression]),a}catch(e){i.fail(e)}},"")}),e.each(c.split(""),function(){var t=s.string.format("requiredif{0}",e.trim(this));e.validator.addMethod(t,function(t,n,r){try{var a=f(t,n,r);return e(n).trigger("eavalid",["requiredif",a.valid,r.expression,a.condition]),a.valid}catch(e){i.fail(e)}},"")}),t.ea=r}(jQuery,window);