inviton-powerduck 0.0.158 → 0.0.159
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.
package/common/validation.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Validation } from '@vuelidate/core';
|
|
2
2
|
import type { PowerduckViewModelBase } from './base-component';
|
|
3
3
|
import type { IValidationSet, ValidationRuleset, ValidationState } from './static-wrappers/interfaces/validation-interface';
|
|
4
|
+
import { Temporal } from '@js-temporal/polyfill';
|
|
4
5
|
import {
|
|
5
6
|
alpha,
|
|
6
7
|
alphaNum,
|
|
@@ -21,10 +22,11 @@ import {
|
|
|
21
22
|
requiredUnless,
|
|
22
23
|
url,
|
|
23
24
|
} from '@vuelidate/validators';
|
|
25
|
+
import { unref } from 'vue';
|
|
24
26
|
import PowerduckState from '../app/powerduck-state';
|
|
27
|
+
import { capitalize } from './extensions/string-extensions';
|
|
25
28
|
import LocalizedValueHelper from './localized-value-helper';
|
|
26
29
|
import { isNullOrEmpty } from './utils/is-null-or-empty';
|
|
27
|
-
import { capitalize } from './extensions/string-extensions';
|
|
28
30
|
|
|
29
31
|
const getFirstUnsattisfiedValidatorName = (valProp: Validation): string | null => {
|
|
30
32
|
const errors = valProp?.$errors || [];
|
|
@@ -134,6 +136,43 @@ export class ValidationHelper {
|
|
|
134
136
|
}
|
|
135
137
|
}
|
|
136
138
|
|
|
139
|
+
const customReq = (value: any): boolean => {
|
|
140
|
+
value = unref(value);
|
|
141
|
+
|
|
142
|
+
if (Array.isArray(value)) {
|
|
143
|
+
return !!value.length;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (value === undefined || value === null) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (value === false) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (value instanceof Date) {
|
|
155
|
+
return !isNaN(value.getTime());
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (typeof Temporal !== 'undefined') {
|
|
159
|
+
if (value instanceof Temporal.PlainDateTime || value instanceof Temporal.PlainDate || value instanceof Temporal.ZonedDateTime || value instanceof Temporal.Instant) {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (typeof value === 'object') {
|
|
165
|
+
// eslint-disable-next-line no-unreachable-loop
|
|
166
|
+
for (const _ in value) {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return !!String(value).length;
|
|
174
|
+
};
|
|
175
|
+
|
|
137
176
|
export class ValidationBuilder {
|
|
138
177
|
private _validationArgs: any = {};
|
|
139
178
|
|
|
@@ -150,8 +189,30 @@ export class ValidationBuilder {
|
|
|
150
189
|
return this;
|
|
151
190
|
}
|
|
152
191
|
|
|
153
|
-
requiredIf
|
|
154
|
-
|
|
192
|
+
requiredIf(field: string | ((vm: any, parentVm?: PowerduckViewModelBase) => boolean)): ValidationBuilder {
|
|
193
|
+
const requireIf = (propOrFunction: boolean | string | ((...args: any[]) => boolean)) => {
|
|
194
|
+
const validate = (prop: any, val: any) => prop ? customReq(typeof val === 'string' ? val.trim() : val) : true;
|
|
195
|
+
|
|
196
|
+
return function (
|
|
197
|
+
this: any,
|
|
198
|
+
value: any,
|
|
199
|
+
parentVM: any,
|
|
200
|
+
) {
|
|
201
|
+
if (typeof propOrFunction != 'function') {
|
|
202
|
+
return validate((propOrFunction), value);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const result = propOrFunction.call(
|
|
206
|
+
this,
|
|
207
|
+
value,
|
|
208
|
+
parentVM,
|
|
209
|
+
);
|
|
210
|
+
return validate(result, value);
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
this._validationArgs.requiredIf = requireIf(field);
|
|
215
|
+
|
|
155
216
|
return this;
|
|
156
217
|
}
|
|
157
218
|
|
|
@@ -29,7 +29,7 @@ function getFlaggedResult(h, state, originator): void {
|
|
|
29
29
|
if (originator == 'mobile') {
|
|
30
30
|
return (<span>{state.text}</span>);
|
|
31
31
|
} else {
|
|
32
|
-
return (<span><span class={`fi fi-${state.
|
|
32
|
+
return (<span><span class={`fi fi-${state.twoLetters}`}></span> {state.text}</span>);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|