@coveo/bueno
Advanced tools
Comparing version 0.1.0-alpha.45 to 0.1.0-alpha.47
@@ -6,2 +6,13 @@ # Change Log | ||
# [0.1.0-alpha.47](https://bitbucket.org/coveord/ui-kit/compare/v0.1.0-alpha.46...v0.1.0-alpha.47) (2020-11-11) | ||
### Features | ||
* **bueno:** add constrainTo option to StringValue ([8f56ff1](https://bitbucket.org/coveord/ui-kit/commits/8f56ff1f3330ac258840bff5d9b3fc24710ebf1c)) | ||
# [0.1.0-alpha.45](https://bitbucket.org/coveord/ui-kit/compare/v0.1.0-alpha.44...v0.1.0-alpha.45) (2020-11-11) | ||
@@ -8,0 +19,0 @@ |
@@ -1,2 +0,2 @@ | ||
class t extends Error{constructor(t){super(t),this.name="SchemaValidationError"}}class e{constructor(t){this.definition=t}validate(e={},n=""){const i={...this.default,...e},r=[];for(const t in this.definition){const e=this.definition[t].validate(i[t]);e&&r.push(`${t}: ${e}`)}if(r.length)throw function(e,n){const i=`\n The following properties are invalid:\n\n ${e.join("\n\t")}\n \n ${n}\n `;return new t(i)}(r,n);return i}get default(){const t={};for(const e in this.definition){const n=this.definition[e].default;void 0!==n&&(t[e]=n)}return t}}class n{constructor(t={}){this.baseConfig=t}validate(t){return this.baseConfig.required&&u(t)?"value is required.":null}get default(){return this.baseConfig.default instanceof Function?this.baseConfig.default():this.baseConfig.default}required(){return!0===this.baseConfig.required}}function i(t){return void 0===t}function r(t){return null===t}function u(t){return i(t)||r(t)}class a{constructor(t={}){this.config=t,this.value=new n(t)}validate(t){const e=this.value.validate(t);return e||(l(t)?t<this.config.min?`minimum value of ${this.config.min} not respected.`:t>this.config.max?`maximum value of ${this.config.max} not respected.`:null:"value is not a number.")}get default(){return this.value.default}get required(){return this.value.required()}}function l(t){return i(t)||s(t)}function s(t){return"number"==typeof t}class o{constructor(t={}){this.value=new n(t)}validate(t){const e=this.value.validate(t);return e||(f(t)?null:"value is not a boolean.")}get default(){return this.value.default}get required(){return this.value.required()}}function f(t){return i(t)||c(t)}function c(t){return"boolean"==typeof t}const d={emptyAllowed:!0,url:!1},h=/^(?:(?:(?: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;class v{constructor(t={}){this.config={...d,...t},this.value=new n(this.config)}validate(t){const e=this.value.validate(t);return e||(i(t)?null:g(t)?this.config.emptyAllowed||t.length?this.config.url&&!h.test(t)?"value is not a valid URL.":null:"value is an empty string.":"value is not a string.")}get default(){return this.value.default}get required(){return this.value.required()}}function g(t){return"[object String]"===Object.prototype.toString.call(t)}const m={options:{required:!1}};class p{constructor(t={}){this.config=t,this.config={...m,...t}}validate(t){var e;if(i(t))return(null===(e=this.config.options)||void 0===e?void 0:e.required)?"value is required and is currently undefined":null;if(!b(t))return"value is not an object";if(Object.keys(t).length>Object.keys(this.config.values).length)return"value contains unknown keys";for(const[e,n]of Object.entries(this.config.values))if(n.required&&u(t[e]))return"value does not contain "+e;let n="";for(const[e,i]of Object.entries(t)){const t=new v({required:!0}).validate(e);if(null!==t)return t;let r=null;(c(i)||g(i)||s(i)||y(i))&&(r=this.config.values[e].validate(i)),null!==r&&(n+=" "+r)}return""===n?null:n}get default(){}required(){return!1}}function b(t){return void 0!==t&&"object"==typeof t}class q{constructor(t={}){this.config=t,this.value=new n(this.config)}validate(t){if(!u(t)&&!Array.isArray(t))return"value is not an array";const e=this.value.validate(t);if(null!==e)return e;if(u(t))return null;if(void 0!==this.config.max&&t.length>this.config.max)return"value contains more than "+this.config.max;if(void 0!==this.config.min&&t.length<this.config.min)return"value contains less than "+this.config.min;if(void 0!==this.config.each){let e="";return t.forEach((n=>{this.config.each.required&&u(n)&&(e="value is null or undefined: "+t.join(","));const i=this.validatePrimitiveValue(n,this.config.each);null!==i&&(e+=" "+i)})),""===e?null:e}return null}validatePrimitiveValue(t,e){return c(t)||g(t)||s(t)||b(t)?e.validate(t):"value is not a primitive value"}get default(){}get required(){return this.value.required()}}function y(t){return Array.isArray(t)}class w{constructor(t){this.config=t,this.value=new n(t)}validate(t){const e=this.value.validate(t);if(null!==e)return e;if(i(t))return null;return Object.values(this.config.enum).find((e=>e===t))?null:"value is not in enum."}get default(){return this.value.default}}export{q as ArrayValue,o as BooleanValue,w as EnumValue,a as NumberValue,p as RecordValue,e as Schema,t as SchemaValidationError,v as StringValue,n as Value,y as isArray,c as isBoolean,f as isBooleanOrUndefined,r as isNull,u as isNullOrUndefined,s as isNumber,l as isNumberOrUndefined,b as isRecord,g as isString,i as isUndefined}; | ||
class t extends Error{constructor(t){super(t),this.name="SchemaValidationError"}}class e{constructor(t){this.definition=t}validate(e={},n=""){const i={...this.default,...e},r=[];for(const t in this.definition){const e=this.definition[t].validate(i[t]);e&&r.push(`${t}: ${e}`)}if(r.length)throw function(e,n){const i=`\n The following properties are invalid:\n\n ${e.join("\n\t")}\n \n ${n}\n `;return new t(i)}(r,n);return i}get default(){const t={};for(const e in this.definition){const n=this.definition[e].default;void 0!==n&&(t[e]=n)}return t}}class n{constructor(t={}){this.baseConfig=t}validate(t){return this.baseConfig.required&&u(t)?"value is required.":null}get default(){return this.baseConfig.default instanceof Function?this.baseConfig.default():this.baseConfig.default}required(){return!0===this.baseConfig.required}}function i(t){return void 0===t}function r(t){return null===t}function u(t){return i(t)||r(t)}class a{constructor(t={}){this.config=t,this.value=new n(t)}validate(t){const e=this.value.validate(t);return e||(l(t)?t<this.config.min?`minimum value of ${this.config.min} not respected.`:t>this.config.max?`maximum value of ${this.config.max} not respected.`:null:"value is not a number.")}get default(){return this.value.default}get required(){return this.value.required()}}function l(t){return i(t)||o(t)}function o(t){return"number"==typeof t}class s{constructor(t={}){this.value=new n(t)}validate(t){const e=this.value.validate(t);return e||(f(t)?null:"value is not a boolean.")}get default(){return this.value.default}get required(){return this.value.required()}}function f(t){return i(t)||c(t)}function c(t){return"boolean"==typeof t}const d=/^(?:(?:(?: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;class h{constructor(t={}){this.config={emptyAllowed:!0,url:!1,...t},this.value=new n(this.config)}validate(t){const{emptyAllowed:e,url:n,constrainTo:r}=this.config,u=this.value.validate(t);if(u)return u;if(i(t))return null;if(!v(t))return"value is not a string.";if(!e&&!t.length)return"value is an empty string.";if(n&&!d.test(t))return"value is not a valid URL.";if(r&&!r.includes(t)){return`value should be one of: ${r.join(", ")}.`}return null}get default(){return this.value.default}get required(){return this.value.required()}}function v(t){return"[object String]"===Object.prototype.toString.call(t)}const g={options:{required:!1}};class m{constructor(t={}){this.config=t,this.config={...g,...t}}validate(t){var e;if(i(t))return(null===(e=this.config.options)||void 0===e?void 0:e.required)?"value is required and is currently undefined":null;if(!b(t))return"value is not an object";if(Object.keys(t).length>Object.keys(this.config.values).length)return"value contains unknown keys";for(const[e,n]of Object.entries(this.config.values))if(n.required&&u(t[e]))return"value does not contain "+e;let n="";for(const[e,i]of Object.entries(t)){const t=new h({required:!0}).validate(e);if(null!==t)return t;let r=null;(c(i)||v(i)||o(i)||q(i))&&(r=this.config.values[e].validate(i)),null!==r&&(n+=" "+r)}return""===n?null:n}get default(){}required(){return!1}}function b(t){return void 0!==t&&"object"==typeof t}class p{constructor(t={}){this.config=t,this.value=new n(this.config)}validate(t){if(!u(t)&&!Array.isArray(t))return"value is not an array";const e=this.value.validate(t);if(null!==e)return e;if(u(t))return null;if(void 0!==this.config.max&&t.length>this.config.max)return"value contains more than "+this.config.max;if(void 0!==this.config.min&&t.length<this.config.min)return"value contains less than "+this.config.min;if(void 0!==this.config.each){let e="";return t.forEach((n=>{this.config.each.required&&u(n)&&(e="value is null or undefined: "+t.join(","));const i=this.validatePrimitiveValue(n,this.config.each);null!==i&&(e+=" "+i)})),""===e?null:e}return null}validatePrimitiveValue(t,e){return c(t)||v(t)||o(t)||b(t)?e.validate(t):"value is not a primitive value"}get default(){}get required(){return this.value.required()}}function q(t){return Array.isArray(t)}class y{constructor(t){this.config=t,this.value=new n(t)}validate(t){const e=this.value.validate(t);if(null!==e)return e;if(i(t))return null;return Object.values(this.config.enum).find((e=>e===t))?null:"value is not in enum."}get default(){return this.value.default}}export{p as ArrayValue,s as BooleanValue,y as EnumValue,a as NumberValue,m as RecordValue,e as Schema,t as SchemaValidationError,h as StringValue,n as Value,q as isArray,c as isBoolean,f as isBooleanOrUndefined,r as isNull,u as isNullOrUndefined,o as isNumber,l as isNumberOrUndefined,b as isRecord,v as isString,i as isUndefined}; | ||
//# sourceMappingURL=bueno.esm.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).Bueno={})}(this,(function(e){"use strict";class t extends Error{constructor(e){super(e),this.name="SchemaValidationError"}}class n{constructor(e={}){this.baseConfig=e}validate(e){return this.baseConfig.required&&u(e)?"value is required.":null}get default(){return this.baseConfig.default instanceof Function?this.baseConfig.default():this.baseConfig.default}required(){return!0===this.baseConfig.required}}function i(e){return void 0===e}function r(e){return null===e}function u(e){return i(e)||r(e)}function a(e){return i(e)||o(e)}function o(e){return"number"==typeof e}function l(e){return i(e)||s(e)}function s(e){return"boolean"==typeof e}const f={emptyAllowed:!0,url:!1},c=/^(?:(?:(?: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;class d{constructor(e={}){this.config={...f,...e},this.value=new n(this.config)}validate(e){const t=this.value.validate(e);return t||(i(e)?null:h(e)?this.config.emptyAllowed||e.length?this.config.url&&!c.test(e)?"value is not a valid URL.":null:"value is an empty string.":"value is not a string.")}get default(){return this.value.default}get required(){return this.value.required()}}function h(e){return"[object String]"===Object.prototype.toString.call(e)}const v={options:{required:!1}};function g(e){return void 0!==e&&"object"==typeof e}function m(e){return Array.isArray(e)}e.ArrayValue=class{constructor(e={}){this.config=e,this.value=new n(this.config)}validate(e){if(!u(e)&&!Array.isArray(e))return"value is not an array";const t=this.value.validate(e);if(null!==t)return t;if(u(e))return null;if(void 0!==this.config.max&&e.length>this.config.max)return"value contains more than "+this.config.max;if(void 0!==this.config.min&&e.length<this.config.min)return"value contains less than "+this.config.min;if(void 0!==this.config.each){let t="";return e.forEach((n=>{this.config.each.required&&u(n)&&(t="value is null or undefined: "+e.join(","));const i=this.validatePrimitiveValue(n,this.config.each);null!==i&&(t+=" "+i)})),""===t?null:t}return null}validatePrimitiveValue(e,t){return s(e)||h(e)||o(e)||g(e)?t.validate(e):"value is not a primitive value"}get default(){}get required(){return this.value.required()}},e.BooleanValue=class{constructor(e={}){this.value=new n(e)}validate(e){const t=this.value.validate(e);return t||(l(e)?null:"value is not a boolean.")}get default(){return this.value.default}get required(){return this.value.required()}},e.EnumValue=class{constructor(e){this.config=e,this.value=new n(e)}validate(e){const t=this.value.validate(e);if(null!==t)return t;if(i(e))return null;return Object.values(this.config.enum).find((t=>t===e))?null:"value is not in enum."}get default(){return this.value.default}},e.NumberValue=class{constructor(e={}){this.config=e,this.value=new n(e)}validate(e){const t=this.value.validate(e);return t||(a(e)?e<this.config.min?`minimum value of ${this.config.min} not respected.`:e>this.config.max?`maximum value of ${this.config.max} not respected.`:null:"value is not a number.")}get default(){return this.value.default}get required(){return this.value.required()}},e.RecordValue=class{constructor(e={}){this.config=e,this.config={...v,...e}}validate(e){var t;if(i(e))return(null===(t=this.config.options)||void 0===t?void 0:t.required)?"value is required and is currently undefined":null;if(!g(e))return"value is not an object";if(Object.keys(e).length>Object.keys(this.config.values).length)return"value contains unknown keys";for(const[t,n]of Object.entries(this.config.values))if(n.required&&u(e[t]))return"value does not contain "+t;let n="";for(const[t,i]of Object.entries(e)){const e=new d({required:!0}).validate(t);if(null!==e)return e;let r=null;(s(i)||h(i)||o(i)||m(i))&&(r=this.config.values[t].validate(i)),null!==r&&(n+=" "+r)}return""===n?null:n}get default(){}required(){return!1}},e.Schema=class{constructor(e){this.definition=e}validate(e={},n=""){const i={...this.default,...e},r=[];for(const e in this.definition){const t=this.definition[e].validate(i[e]);t&&r.push(`${e}: ${t}`)}if(r.length)throw function(e,n){const i=`\n The following properties are invalid:\n\n ${e.join("\n\t")}\n \n ${n}\n `;return new t(i)}(r,n);return i}get default(){const e={};for(const t in this.definition){const n=this.definition[t].default;void 0!==n&&(e[t]=n)}return e}},e.SchemaValidationError=t,e.StringValue=d,e.Value=n,e.isArray=m,e.isBoolean=s,e.isBooleanOrUndefined=l,e.isNull=r,e.isNullOrUndefined=u,e.isNumber=o,e.isNumberOrUndefined=a,e.isRecord=g,e.isString=h,e.isUndefined=i,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).Bueno={})}(this,(function(e){"use strict";class t extends Error{constructor(e){super(e),this.name="SchemaValidationError"}}class n{constructor(e={}){this.baseConfig=e}validate(e){return this.baseConfig.required&&u(e)?"value is required.":null}get default(){return this.baseConfig.default instanceof Function?this.baseConfig.default():this.baseConfig.default}required(){return!0===this.baseConfig.required}}function i(e){return void 0===e}function r(e){return null===e}function u(e){return i(e)||r(e)}function a(e){return i(e)||o(e)}function o(e){return"number"==typeof e}function l(e){return i(e)||s(e)}function s(e){return"boolean"==typeof e}const f=/^(?:(?:(?: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;class d{constructor(e={}){this.config={emptyAllowed:!0,url:!1,...e},this.value=new n(this.config)}validate(e){const{emptyAllowed:t,url:n,constrainTo:r}=this.config,u=this.value.validate(e);if(u)return u;if(i(e))return null;if(!c(e))return"value is not a string.";if(!t&&!e.length)return"value is an empty string.";if(n&&!f.test(e))return"value is not a valid URL.";if(r&&!r.includes(e)){return`value should be one of: ${r.join(", ")}.`}return null}get default(){return this.value.default}get required(){return this.value.required()}}function c(e){return"[object String]"===Object.prototype.toString.call(e)}const h={options:{required:!1}};function v(e){return void 0!==e&&"object"==typeof e}function g(e){return Array.isArray(e)}e.ArrayValue=class{constructor(e={}){this.config=e,this.value=new n(this.config)}validate(e){if(!u(e)&&!Array.isArray(e))return"value is not an array";const t=this.value.validate(e);if(null!==t)return t;if(u(e))return null;if(void 0!==this.config.max&&e.length>this.config.max)return"value contains more than "+this.config.max;if(void 0!==this.config.min&&e.length<this.config.min)return"value contains less than "+this.config.min;if(void 0!==this.config.each){let t="";return e.forEach((n=>{this.config.each.required&&u(n)&&(t="value is null or undefined: "+e.join(","));const i=this.validatePrimitiveValue(n,this.config.each);null!==i&&(t+=" "+i)})),""===t?null:t}return null}validatePrimitiveValue(e,t){return s(e)||c(e)||o(e)||v(e)?t.validate(e):"value is not a primitive value"}get default(){}get required(){return this.value.required()}},e.BooleanValue=class{constructor(e={}){this.value=new n(e)}validate(e){const t=this.value.validate(e);return t||(l(e)?null:"value is not a boolean.")}get default(){return this.value.default}get required(){return this.value.required()}},e.EnumValue=class{constructor(e){this.config=e,this.value=new n(e)}validate(e){const t=this.value.validate(e);if(null!==t)return t;if(i(e))return null;return Object.values(this.config.enum).find((t=>t===e))?null:"value is not in enum."}get default(){return this.value.default}},e.NumberValue=class{constructor(e={}){this.config=e,this.value=new n(e)}validate(e){const t=this.value.validate(e);return t||(a(e)?e<this.config.min?`minimum value of ${this.config.min} not respected.`:e>this.config.max?`maximum value of ${this.config.max} not respected.`:null:"value is not a number.")}get default(){return this.value.default}get required(){return this.value.required()}},e.RecordValue=class{constructor(e={}){this.config=e,this.config={...h,...e}}validate(e){var t;if(i(e))return(null===(t=this.config.options)||void 0===t?void 0:t.required)?"value is required and is currently undefined":null;if(!v(e))return"value is not an object";if(Object.keys(e).length>Object.keys(this.config.values).length)return"value contains unknown keys";for(const[t,n]of Object.entries(this.config.values))if(n.required&&u(e[t]))return"value does not contain "+t;let n="";for(const[t,i]of Object.entries(e)){const e=new d({required:!0}).validate(t);if(null!==e)return e;let r=null;(s(i)||c(i)||o(i)||g(i))&&(r=this.config.values[t].validate(i)),null!==r&&(n+=" "+r)}return""===n?null:n}get default(){}required(){return!1}},e.Schema=class{constructor(e){this.definition=e}validate(e={},n=""){const i={...this.default,...e},r=[];for(const e in this.definition){const t=this.definition[e].validate(i[e]);t&&r.push(`${e}: ${t}`)}if(r.length)throw function(e,n){const i=`\n The following properties are invalid:\n\n ${e.join("\n\t")}\n \n ${n}\n `;return new t(i)}(r,n);return i}get default(){const e={};for(const t in this.definition){const n=this.definition[t].default;void 0!==n&&(e[t]=n)}return e}},e.SchemaValidationError=t,e.StringValue=d,e.Value=n,e.isArray=g,e.isBoolean=s,e.isBooleanOrUndefined=l,e.isNull=r,e.isNullOrUndefined=u,e.isNumber=o,e.isNumberOrUndefined=a,e.isRecord=v,e.isString=c,e.isUndefined=i,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=bueno.js.map |
@@ -6,2 +6,3 @@ import { ValueConfig } from './value'; | ||
url?: boolean; | ||
constrainTo?: string[]; | ||
} | ||
@@ -12,3 +13,3 @@ export declare class StringValue implements SchemaValue<string> { | ||
constructor(config?: StringValueConfig); | ||
validate(value: string): "value is required." | "value is not a string." | "value is an empty string." | "value is not a valid URL." | null; | ||
validate(value: string): string | null; | ||
get default(): string | undefined; | ||
@@ -15,0 +16,0 @@ get required(): boolean; |
@@ -140,6 +140,2 @@ function buildSchemaValidationError(errors, context) { | ||
const defaultConfig = { | ||
emptyAllowed: true, | ||
url: false, | ||
}; | ||
// Source: https://github.com/jquery-validation/jquery-validation/blob/c1db10a34c0847c28a5bd30e3ee1117e137ca834/src/core.js#L1349 | ||
@@ -150,3 +146,4 @@ const urlRegex = /^(?:(?:(?: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; | ||
this.config = { | ||
...defaultConfig, | ||
emptyAllowed: true, | ||
url: false, | ||
...config, | ||
@@ -157,2 +154,3 @@ }; | ||
validate(value) { | ||
const { emptyAllowed, url, constrainTo } = this.config; | ||
const valueValidation = this.value.validate(value); | ||
@@ -168,8 +166,12 @@ if (valueValidation) { | ||
} | ||
if (!this.config.emptyAllowed && !value.length) { | ||
if (!emptyAllowed && !value.length) { | ||
return 'value is an empty string.'; | ||
} | ||
if (this.config.url && !urlRegex.test(value)) { | ||
if (url && !urlRegex.test(value)) { | ||
return 'value is not a valid URL.'; | ||
} | ||
if (constrainTo && !constrainTo.includes(value)) { | ||
const values = constrainTo.join(', '); | ||
return `value should be one of: ${values}.`; | ||
} | ||
return null; | ||
@@ -188,3 +190,3 @@ } | ||
const defaultConfig$1 = { | ||
const defaultConfig = { | ||
options: { required: false }, | ||
@@ -196,3 +198,3 @@ }; | ||
this.config = { | ||
...defaultConfig$1, | ||
...defaultConfig, | ||
...config, | ||
@@ -199,0 +201,0 @@ }; |
@@ -144,6 +144,2 @@ 'use strict'; | ||
const defaultConfig = { | ||
emptyAllowed: true, | ||
url: false, | ||
}; | ||
// Source: https://github.com/jquery-validation/jquery-validation/blob/c1db10a34c0847c28a5bd30e3ee1117e137ca834/src/core.js#L1349 | ||
@@ -154,3 +150,4 @@ const urlRegex = /^(?:(?:(?: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; | ||
this.config = { | ||
...defaultConfig, | ||
emptyAllowed: true, | ||
url: false, | ||
...config, | ||
@@ -161,2 +158,3 @@ }; | ||
validate(value) { | ||
const { emptyAllowed, url, constrainTo } = this.config; | ||
const valueValidation = this.value.validate(value); | ||
@@ -172,8 +170,12 @@ if (valueValidation) { | ||
} | ||
if (!this.config.emptyAllowed && !value.length) { | ||
if (!emptyAllowed && !value.length) { | ||
return 'value is an empty string.'; | ||
} | ||
if (this.config.url && !urlRegex.test(value)) { | ||
if (url && !urlRegex.test(value)) { | ||
return 'value is not a valid URL.'; | ||
} | ||
if (constrainTo && !constrainTo.includes(value)) { | ||
const values = constrainTo.join(', '); | ||
return `value should be one of: ${values}.`; | ||
} | ||
return null; | ||
@@ -192,3 +194,3 @@ } | ||
const defaultConfig$1 = { | ||
const defaultConfig = { | ||
options: { required: false }, | ||
@@ -200,3 +202,3 @@ }; | ||
this.config = { | ||
...defaultConfig$1, | ||
...defaultConfig, | ||
...config, | ||
@@ -203,0 +205,0 @@ }; |
@@ -6,2 +6,3 @@ import { ValueConfig } from './value'; | ||
url?: boolean; | ||
constrainTo?: string[]; | ||
} | ||
@@ -12,3 +13,3 @@ export declare class StringValue implements SchemaValue<string> { | ||
constructor(config?: StringValueConfig); | ||
validate(value: string): "value is required." | "value is not a string." | "value is an empty string." | "value is not a valid URL." | null; | ||
validate(value: string): string | null; | ||
get default(): string | undefined; | ||
@@ -15,0 +16,0 @@ get required(): boolean; |
@@ -12,3 +12,3 @@ { | ||
"license": "Apache-2.0", | ||
"version": "0.1.0-alpha.45", | ||
"version": "0.1.0-alpha.47", | ||
"files": [ | ||
@@ -15,0 +15,0 @@ "dist/" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
96694
1003