@fluid-topics/ft-wc-utils 2.0.36 → 2.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,10 +4,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
4
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
};
|
|
7
|
-
import {
|
|
7
|
+
import { processValidationRules, } from "./ValidationRules";
|
|
8
|
+
import { property, state, } from "lit/decorators.js";
|
|
8
9
|
import { repeat } from "lit/directives/repeat.js";
|
|
9
10
|
import { when } from "lit/directives/when.js";
|
|
10
|
-
import { html } from "lit";
|
|
11
|
+
import { html, } from "lit";
|
|
11
12
|
import { applyMixinOnce } from "../helpers";
|
|
12
13
|
export var ValidateOn;
|
|
13
14
|
(function (ValidateOn) {
|
|
@@ -66,8 +67,8 @@ export const toFtInput = applyMixinOnce(Symbol("toFtInput"), function (ElementCl
|
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
triggerValidation() {
|
|
69
|
-
this.errorMessages = this.
|
|
70
|
-
this.warningMessages = this.
|
|
70
|
+
this.errorMessages = processValidationRules(this.value, ([...this.errorRules, ...this.additionalErrorRules]));
|
|
71
|
+
this.warningMessages = processValidationRules(this.value, ([...this.warningRules, ...this.additionalWarningRules]));
|
|
71
72
|
this.status = this.resolveStatus();
|
|
72
73
|
this.dispatchEvent(new InputValidationEvent(this.status));
|
|
73
74
|
}
|
|
@@ -96,11 +97,6 @@ export const toFtInput = applyMixinOnce(Symbol("toFtInput"), function (ElementCl
|
|
|
96
97
|
: this.warningMessages.length ? Status.warning
|
|
97
98
|
: Status.default;
|
|
98
99
|
}
|
|
99
|
-
processRules(rules) {
|
|
100
|
-
return rules.map((r) => r(this.value))
|
|
101
|
-
.filter((s) => s !== true)
|
|
102
|
-
.map((e) => e);
|
|
103
|
-
}
|
|
104
100
|
}
|
|
105
101
|
__decorate([
|
|
106
102
|
property()
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export type ValidationRule<V> = (value?: V) => true | string;
|
|
2
2
|
export declare const ValidationRules: {
|
|
3
3
|
required: (message: string) => ValidationRule<string>;
|
|
4
|
+
regex: (regex: RegExp, message: string) => ValidationRule<string>;
|
|
4
5
|
email: (message: string) => ValidationRule<string>;
|
|
5
6
|
url: (message: string) => ValidationRule<string>;
|
|
6
7
|
};
|
|
8
|
+
export declare function processValidationRules<V>(value: V | undefined, rules: ValidationRule<V>[]): string[];
|
|
@@ -1,13 +1,30 @@
|
|
|
1
1
|
const URL_REGEX = /^[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/;
|
|
2
2
|
const EMAIL_REGEX = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
|
|
3
|
+
function testRegex(regex, message, v) {
|
|
4
|
+
if (!v) {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
if (!regex.test(v)) {
|
|
8
|
+
return message;
|
|
9
|
+
}
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
3
12
|
export const ValidationRules = {
|
|
4
13
|
required: function (message) {
|
|
5
14
|
return (v) => (!!v || message);
|
|
6
15
|
},
|
|
16
|
+
regex: function (regex, message) {
|
|
17
|
+
return (v) => testRegex(regex, message, v);
|
|
18
|
+
},
|
|
7
19
|
email: function (message) {
|
|
8
|
-
return (v) => (
|
|
20
|
+
return (v) => testRegex(EMAIL_REGEX, message, v);
|
|
9
21
|
},
|
|
10
22
|
url: function (message) {
|
|
11
|
-
return (v) => (
|
|
23
|
+
return (v) => testRegex(URL_REGEX, message, v);
|
|
12
24
|
},
|
|
13
25
|
};
|
|
26
|
+
export function processValidationRules(value, rules) {
|
|
27
|
+
return rules.map((r) => r(value))
|
|
28
|
+
.filter((s) => s !== true)
|
|
29
|
+
.map((e) => e);
|
|
30
|
+
}
|