@regle/rules 1.14.7 → 1.15.1
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/dist/regle-rules.d.ts +78 -1
- package/dist/regle-rules.js +140 -3
- package/dist/regle-rules.min.js +1 -1
- package/package.json +2 -2
package/dist/regle-rules.d.ts
CHANGED
|
@@ -786,6 +786,43 @@ declare const exactLength: RegleRuleWithParamsDefinition<string | any[] | Record
|
|
|
786
786
|
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#exactvalue Documentation}
|
|
787
787
|
*/
|
|
788
788
|
declare const exactValue: RegleRuleWithParamsDefinition<number, [count: number], false, boolean, MaybeInput<number>>;
|
|
789
|
+
/**
|
|
790
|
+
* Requires a value to be a native `File` constructor.
|
|
791
|
+
*
|
|
792
|
+
* Mainly used for typing with `InferInput`.
|
|
793
|
+
*
|
|
794
|
+
* @example
|
|
795
|
+
* ```ts
|
|
796
|
+
* import { type InferInput } from '@regle/core';
|
|
797
|
+
* import { file } from '@regle/rules';
|
|
798
|
+
*
|
|
799
|
+
* const rules = {
|
|
800
|
+
* file: { file },
|
|
801
|
+
* }
|
|
802
|
+
*
|
|
803
|
+
* const state = ref<InferInput<typeof rules>>({});
|
|
804
|
+
* ```
|
|
805
|
+
*
|
|
806
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#file Documentation}
|
|
807
|
+
*/
|
|
808
|
+
declare const file: RegleRuleDefinition<unknown, [], false, boolean, MaybeInput<File>, unknown>;
|
|
809
|
+
/**
|
|
810
|
+
* Requires a value to be a file with a specific type.
|
|
811
|
+
*
|
|
812
|
+
* @example
|
|
813
|
+
* ```ts
|
|
814
|
+
* import { type InferInput } from '@regle/core';
|
|
815
|
+
* import { fileType } from '@regle/rules';
|
|
816
|
+
*
|
|
817
|
+
* const {r$} = useRegle({ file: null as File | null }, {
|
|
818
|
+
* file: { fileType: fileType(['image/png', 'image/jpeg']) },
|
|
819
|
+
* })
|
|
820
|
+
*
|
|
821
|
+
* ```
|
|
822
|
+
*
|
|
823
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#filetype Documentation}
|
|
824
|
+
*/
|
|
825
|
+
declare const fileType: RegleRuleWithParamsDefinition<File, [accept: string[]], false, boolean, unknown, File>;
|
|
789
826
|
/**
|
|
790
827
|
* Validates hexadecimal values.
|
|
791
828
|
*
|
|
@@ -869,6 +906,26 @@ declare function literal<const TValue$1 extends string | number>(literal: MaybeR
|
|
|
869
906
|
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#macaddress Documentation}
|
|
870
907
|
*/
|
|
871
908
|
declare const macAddress: RegleRuleWithParamsDefinition<string, [separator?: string | undefined], false, boolean, MaybeInput<string>>;
|
|
909
|
+
/**
|
|
910
|
+
* Requires a value to be a file with a maximum size.
|
|
911
|
+
*
|
|
912
|
+
* @example
|
|
913
|
+
* ```ts
|
|
914
|
+
* import { type InferInput } from '@regle/core';
|
|
915
|
+
* import { maxFileSize } from '@regle/rules';
|
|
916
|
+
*
|
|
917
|
+
* const {r$} = useRegle({ file: null as File | null }, {
|
|
918
|
+
* file: { maxFileSize: maxFileSize(10_000_000) }, // 10 MB
|
|
919
|
+
* })
|
|
920
|
+
*
|
|
921
|
+
* ```
|
|
922
|
+
*
|
|
923
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#maxfilesize Documentation}
|
|
924
|
+
*/
|
|
925
|
+
declare const maxFileSize: RegleRuleWithParamsDefinition<File, [maxSize: number], false, true | {
|
|
926
|
+
$valid: boolean;
|
|
927
|
+
fileSize: number;
|
|
928
|
+
}, unknown, File>;
|
|
872
929
|
/**
|
|
873
930
|
* Requires the input value to have a maximum specified length, inclusive. Works with arrays, objects and strings.
|
|
874
931
|
*
|
|
@@ -921,6 +978,26 @@ declare const maxLength: RegleRuleWithParamsDefinition<string | any[] | Record<P
|
|
|
921
978
|
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#maxvalue Documentation}
|
|
922
979
|
*/
|
|
923
980
|
declare const maxValue: RegleRuleWithParamsDefinition<number | string, [max: number | string, options?: CommonComparisonOptions], false, boolean, MaybeInput<number | string>>;
|
|
981
|
+
/**
|
|
982
|
+
* Requires a value to be a file with a minimum size.
|
|
983
|
+
*
|
|
984
|
+
* @example
|
|
985
|
+
* ```ts
|
|
986
|
+
* import { type InferInput } from '@regle/core';
|
|
987
|
+
* import { minFileSize } from '@regle/rules';
|
|
988
|
+
*
|
|
989
|
+
* const {r$} = useRegle({ file: null as File | null }, {
|
|
990
|
+
* file: { minFileSize: minFileSize(1_000_000) }, // 1 MB
|
|
991
|
+
* })
|
|
992
|
+
*
|
|
993
|
+
* ```
|
|
994
|
+
*
|
|
995
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#minfilesize Documentation}
|
|
996
|
+
*/
|
|
997
|
+
declare const minFileSize: RegleRuleWithParamsDefinition<File, [minSize: number], false, true | {
|
|
998
|
+
$valid: boolean;
|
|
999
|
+
fileSize: number;
|
|
1000
|
+
}, unknown, File>;
|
|
924
1001
|
/**
|
|
925
1002
|
* Requires the input value to have a minimum specified length, inclusive. Works with arrays, objects and strings.
|
|
926
1003
|
*
|
|
@@ -1246,4 +1323,4 @@ declare function type<T>(): RegleRuleDefinition<unknown, [], false, boolean, May
|
|
|
1246
1323
|
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#url Documentation}
|
|
1247
1324
|
*/
|
|
1248
1325
|
declare const url: RegleRuleDefinition<string, [], false, boolean, MaybeInput<string>>;
|
|
1249
|
-
export { EnumLike, alpha, alphaNum, and, applyIf, assignIf, between, boolean, checked, contains, date, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, getSize, hexadecimal, integer, ipv4Address, isDate, isEmpty, isFilled, isNumber, literal, macAddress, matchRegex, maxLength, maxValue, minLength, minValue, nativeEnum, not, number, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, string, toDate, toNumber, type, url, withAsync, withMessage, withParams, withTooltip };
|
|
1326
|
+
export { EnumLike, alpha, alphaNum, and, applyIf, assignIf, between, boolean, checked, contains, date, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, file, fileType, getSize, hexadecimal, integer, ipv4Address, isDate, isEmpty, isFilled, isNumber, literal, macAddress, matchRegex, maxFileSize, maxLength, maxValue, minFileSize, minLength, minValue, nativeEnum, not, number, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, string, toDate, toNumber, type, url, withAsync, withMessage, withParams, withTooltip };
|
package/dist/regle-rules.js
CHANGED
|
@@ -170,7 +170,7 @@ function applyIf(_condition, rule) {
|
|
|
170
170
|
* Server side friendly way of checking for a File
|
|
171
171
|
*/
|
|
172
172
|
function isFile(value) {
|
|
173
|
-
return value?.constructor?.name == "File"
|
|
173
|
+
return value?.constructor?.name == "File";
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
/**
|
|
@@ -286,6 +286,19 @@ function toDate(argument) {
|
|
|
286
286
|
else return /* @__PURE__ */ new Date(NaN);
|
|
287
287
|
}
|
|
288
288
|
|
|
289
|
+
/**
|
|
290
|
+
* Formats a file size in bytes to a human readable string.
|
|
291
|
+
* @param size - The size in bytes
|
|
292
|
+
* @returns The formatted size
|
|
293
|
+
*/
|
|
294
|
+
function formatFileSize(size) {
|
|
295
|
+
if (size === void 0) return "0 bytes";
|
|
296
|
+
if (size < 1024) return `${size} bytes`;
|
|
297
|
+
if (size < 1024 * 1024) return `${(size / 1024).toFixed(2)} kb`;
|
|
298
|
+
if (size < 1024 * 1024 * 1024) return `${(size / 1024 / 1024).toFixed(2)} mb`;
|
|
299
|
+
return `${(size / 1024 / 1024 / 1024).toFixed(2)} gb`;
|
|
300
|
+
}
|
|
301
|
+
|
|
289
302
|
/**
|
|
290
303
|
* Checks if any value you provide is defined (including arrays and objects).
|
|
291
304
|
* This is almost a must-have for optional fields when writing custom rules.
|
|
@@ -938,7 +951,7 @@ const date = createRule({
|
|
|
938
951
|
if (isFilled(value)) return value instanceof Date;
|
|
939
952
|
return true;
|
|
940
953
|
},
|
|
941
|
-
message: "The value must be a native Date
|
|
954
|
+
message: "The value must be a native Date"
|
|
942
955
|
});
|
|
943
956
|
|
|
944
957
|
function getUserLocale() {
|
|
@@ -1230,6 +1243,64 @@ const exactValue = createRule({
|
|
|
1230
1243
|
}
|
|
1231
1244
|
});
|
|
1232
1245
|
|
|
1246
|
+
/**
|
|
1247
|
+
* Requires a value to be a native `File` constructor.
|
|
1248
|
+
*
|
|
1249
|
+
* Mainly used for typing with `InferInput`.
|
|
1250
|
+
*
|
|
1251
|
+
* @example
|
|
1252
|
+
* ```ts
|
|
1253
|
+
* import { type InferInput } from '@regle/core';
|
|
1254
|
+
* import { file } from '@regle/rules';
|
|
1255
|
+
*
|
|
1256
|
+
* const rules = {
|
|
1257
|
+
* file: { file },
|
|
1258
|
+
* }
|
|
1259
|
+
*
|
|
1260
|
+
* const state = ref<InferInput<typeof rules>>({});
|
|
1261
|
+
* ```
|
|
1262
|
+
*
|
|
1263
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#file Documentation}
|
|
1264
|
+
*/
|
|
1265
|
+
const file = createRule({
|
|
1266
|
+
type: "file",
|
|
1267
|
+
validator: (value) => {
|
|
1268
|
+
if (isFilled(value)) return isFile(value);
|
|
1269
|
+
return true;
|
|
1270
|
+
},
|
|
1271
|
+
message: "The value must be a native File"
|
|
1272
|
+
});
|
|
1273
|
+
|
|
1274
|
+
/**
|
|
1275
|
+
* Requires a value to be a file with a specific type.
|
|
1276
|
+
*
|
|
1277
|
+
* @example
|
|
1278
|
+
* ```ts
|
|
1279
|
+
* import { type InferInput } from '@regle/core';
|
|
1280
|
+
* import { fileType } from '@regle/rules';
|
|
1281
|
+
*
|
|
1282
|
+
* const {r$} = useRegle({ file: null as File | null }, {
|
|
1283
|
+
* file: { fileType: fileType(['image/png', 'image/jpeg']) },
|
|
1284
|
+
* })
|
|
1285
|
+
*
|
|
1286
|
+
* ```
|
|
1287
|
+
*
|
|
1288
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#filetype Documentation}
|
|
1289
|
+
*/
|
|
1290
|
+
const fileType = createRule({
|
|
1291
|
+
type: "fileType",
|
|
1292
|
+
validator: (value, accept) => {
|
|
1293
|
+
if (isFilled(value)) {
|
|
1294
|
+
if (isFile(value)) return accept.includes(value.type);
|
|
1295
|
+
return false;
|
|
1296
|
+
}
|
|
1297
|
+
return true;
|
|
1298
|
+
},
|
|
1299
|
+
message({ $params: [accept] }) {
|
|
1300
|
+
return `File type is not allowed. Allowed types are: ${accept.map((type$1) => type$1.split("/")[1]).join(", ")}.`;
|
|
1301
|
+
}
|
|
1302
|
+
});
|
|
1303
|
+
|
|
1233
1304
|
const hexadecimalRegex = /^[a-fA-F0-9]*$/;
|
|
1234
1305
|
/**
|
|
1235
1306
|
* Validates hexadecimal values.
|
|
@@ -1365,6 +1436,39 @@ const macAddress = createRule({
|
|
|
1365
1436
|
});
|
|
1366
1437
|
const hexValid = (hex) => hex.toLowerCase().match(/^[0-9a-f]{2}$/);
|
|
1367
1438
|
|
|
1439
|
+
/**
|
|
1440
|
+
* Requires a value to be a file with a maximum size.
|
|
1441
|
+
*
|
|
1442
|
+
* @example
|
|
1443
|
+
* ```ts
|
|
1444
|
+
* import { type InferInput } from '@regle/core';
|
|
1445
|
+
* import { maxFileSize } from '@regle/rules';
|
|
1446
|
+
*
|
|
1447
|
+
* const {r$} = useRegle({ file: null as File | null }, {
|
|
1448
|
+
* file: { maxFileSize: maxFileSize(10_000_000) }, // 10 MB
|
|
1449
|
+
* })
|
|
1450
|
+
*
|
|
1451
|
+
* ```
|
|
1452
|
+
*
|
|
1453
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#maxfilesize Documentation}
|
|
1454
|
+
*/
|
|
1455
|
+
const maxFileSize = createRule({
|
|
1456
|
+
type: "maxFileSize",
|
|
1457
|
+
validator: (value, maxSize) => {
|
|
1458
|
+
if (isFilled(value)) {
|
|
1459
|
+
if (isFile(value)) return {
|
|
1460
|
+
$valid: value.size <= maxSize,
|
|
1461
|
+
fileSize: value.size
|
|
1462
|
+
};
|
|
1463
|
+
return true;
|
|
1464
|
+
}
|
|
1465
|
+
return true;
|
|
1466
|
+
},
|
|
1467
|
+
message({ $params: [maxSize], fileSize }) {
|
|
1468
|
+
return `File size (${formatFileSize(fileSize)}) cannot exceed ${formatFileSize(maxSize)}`;
|
|
1469
|
+
}
|
|
1470
|
+
});
|
|
1471
|
+
|
|
1368
1472
|
/**
|
|
1369
1473
|
* Requires the input value to have a maximum specified length, inclusive. Works with arrays, objects and strings.
|
|
1370
1474
|
*
|
|
@@ -1452,6 +1556,39 @@ const maxValue = createRule({
|
|
|
1452
1556
|
}
|
|
1453
1557
|
});
|
|
1454
1558
|
|
|
1559
|
+
/**
|
|
1560
|
+
* Requires a value to be a file with a minimum size.
|
|
1561
|
+
*
|
|
1562
|
+
* @example
|
|
1563
|
+
* ```ts
|
|
1564
|
+
* import { type InferInput } from '@regle/core';
|
|
1565
|
+
* import { minFileSize } from '@regle/rules';
|
|
1566
|
+
*
|
|
1567
|
+
* const {r$} = useRegle({ file: null as File | null }, {
|
|
1568
|
+
* file: { minFileSize: minFileSize(1_000_000) }, // 1 MB
|
|
1569
|
+
* })
|
|
1570
|
+
*
|
|
1571
|
+
* ```
|
|
1572
|
+
*
|
|
1573
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/built-in-rules#minfilesize Documentation}
|
|
1574
|
+
*/
|
|
1575
|
+
const minFileSize = createRule({
|
|
1576
|
+
type: "minFileSize",
|
|
1577
|
+
validator: (value, minSize) => {
|
|
1578
|
+
if (isFilled(value)) {
|
|
1579
|
+
if (isFile(value)) return {
|
|
1580
|
+
$valid: value.size >= minSize,
|
|
1581
|
+
fileSize: value.size
|
|
1582
|
+
};
|
|
1583
|
+
return true;
|
|
1584
|
+
}
|
|
1585
|
+
return true;
|
|
1586
|
+
},
|
|
1587
|
+
message({ $params: [minSize], fileSize }) {
|
|
1588
|
+
return `File size (${formatFileSize(fileSize)}) must be at least ${formatFileSize(minSize)}`;
|
|
1589
|
+
}
|
|
1590
|
+
});
|
|
1591
|
+
|
|
1455
1592
|
/**
|
|
1456
1593
|
* Requires the input value to have a minimum specified length, inclusive. Works with arrays, objects and strings.
|
|
1457
1594
|
*
|
|
@@ -1915,4 +2052,4 @@ const url = createRule({
|
|
|
1915
2052
|
message: "The value is not a valid URL address"
|
|
1916
2053
|
});
|
|
1917
2054
|
|
|
1918
|
-
export { alpha, alphaNum, and, applyIf, assignIf, between, boolean, checked, contains, date, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, getSize, hexadecimal, integer, ipv4Address, isDate, isEmpty, isFilled, isNumber, literal, macAddress, matchRegex, maxLength, maxValue, minLength, minValue, nativeEnum, not, number, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, string, toDate, toNumber, type, url, withAsync, withMessage, withParams, withTooltip };
|
|
2055
|
+
export { alpha, alphaNum, and, applyIf, assignIf, between, boolean, checked, contains, date, dateAfter, dateBefore, dateBetween, decimal, email, endsWith, exactLength, exactValue, file, fileType, getSize, hexadecimal, integer, ipv4Address, isDate, isEmpty, isFilled, isNumber, literal, macAddress, matchRegex, maxFileSize, maxLength, maxValue, minFileSize, minLength, minValue, nativeEnum, not, number, numeric, oneOf, or, regex, required, requiredIf, requiredUnless, sameAs, startsWith, string, toDate, toNumber, type, url, withAsync, withMessage, withParams, withTooltip };
|
package/dist/regle-rules.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{InternalRuleType as e,createRule as t,unwrapRuleParameters as n}from"@regle/core";import{computed as r,toValue as i,unref as a}from"vue";function o(n,r){let i,a,o,s,c=!1;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s,_async:c}=n;let l=t({type:i,validator:a,active:o,message:r,async:c}),u=[...s??[]];if(l._params=u,l._message_patched=!0,typeof l==`function`){if(s!=null){let e=l(...u);return e._message_patched=!0,e}return l}else return l}function s(n,r){let i,a,o,s,c,l=!1;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s,_message:c,_async:l}=n;let u=t({type:i,validator:a,active:o,message:c,tooltip:r,async:l}),d=[...s??[]];if(u._params=d,u._tooltip_patched=!0,typeof u==`function`){let e=u(...d);return u._tooltip_patched=!0,e}else return u}function c(n,r){let i,a,o=[],s=``;typeof n==`function`?(a=async(e,...t)=>n(e,...t),o=[r]):({_type:i,_message:s}=n,o=o=n._params?.concat(r),a=async(...e)=>n.validator(e));let c=t({type:i??e.Async,validator:a,message:s,async:!0});return c._params=c._params?.concat(o),c(...r??[])}function l(n,r){let i,a,o=[],s=``;typeof n==`function`?(e.Inline,a=(e,...t)=>n(e,...t),o=[r]):({_type:i,validator:a,_message:s}=n,o=o=n._params?.concat(r));let c=t({type:e.Inline,validator:a,message:s});return c._params=c._params?.concat(o),c(...r)}function u(r,i){let a,o,s=[],c=``;typeof i==`function`?(a=e.Inline,o=i,s=[r]):({_type:a,validator:o,_message:c}=i,s=i._params?.concat([r]));function l(e,...t){let[i]=n([r]);return i?o(e,...t):!0}function u(){let[e]=n([r]);return e}let d=t({type:a,validator:l,active:u,message:c}),f=[...s??[]];return d._params=f,typeof d==`function`?d(...f):d}function d(e){return e?.constructor?.name==`File`||e?.constructor?.name==`FileList`}function f(e,t=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):d(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:typeof e==`object`&&e?Object.keys(e).length===0:!String(e).length}function p(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function m(e){if(f(e))return!1;try{let t=null;if(e instanceof Date)t=e;else if(typeof e==`string`){let n=new Date(e);if(n.toString()===`Invalid Date`)return!1;t=n}return!!t}catch{return!1}}function h(e){let t=Object.prototype.toString.call(e);return e==null?new Date(NaN):e instanceof Date||typeof e==`object`&&t===`[object Date]`?new Date(e.getTime()):typeof e==`number`||t===`[object Number]`||typeof e==`string`||t===`[object String]`?new Date(e):new Date(NaN)}function g(e,t=!0){return!f(typeof e==`string`?e.trim():e,t)}function _(e){return e==null?!1:typeof e==`number`?!isNaN(e):!1}function v(e,...t){if(f(e))return!0;let n=typeof e==`number`?e.toString():e;return t.every(e=>(e.lastIndex=0,e.test(n)))}function y(e){let t=a(e);return Array.isArray(t)?t.length:typeof t==`object`?Object.keys(t).length:typeof t==`number`?isNaN(t)?0:t:String(t).length}function b(e){return typeof e==`number`?e:e==null?NaN:typeof e==`string`&&e.trim()===e?+e:NaN}function x(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>{if(typeof e==`function`)return null;{let t=e._params;return t?.length?t:[]}}).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e?.some(e=>typeof e!=`boolean`)?{$valid:e.every(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.every(e=>!!e)}let o;o=e.length?n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)):e=>!1;let s=t({type:`and`,validator:o,message:`The value does not match all of the provided validators`}),c=[...r??[]];return s._params=c,typeof s==`function`?s(...c):s}function ee(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>typeof e==`function`?null:e._params).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e.some(e=>typeof e!=`boolean`)?{$valid:e.some(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.some(e=>!!e)}let o=t({type:`or`,validator:n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)),message:`The value does not match any of the provided validators`}),s=[...r??[]];return o._params=s,typeof o==`function`?o(...s):o}function S(e,n){let r,i,a,o,s;typeof e==`function`?(i=e,s=e.constructor.name===`AsyncFunction`):({_type:r,validator:i,_params:o}=e,s=e._async),a=s?async(e,...t)=>g(e)?!await i(e,...t):!0:(e,...t)=>g(e)?!i(e,...t):!0;let c=t({type:`not`,validator:a,message:n??`Error`}),l=[...o??[]];return c._params=l,typeof c==`function`?c(...l):c}function C(e,t,n){return Object.entries(i(t)).map(([t,r])=>typeof r==`function`||p(r)&&`_validator`in r?[t,u(n?e:()=>!i(e),r)]:[t,r])}function w(e,t,n){return r(()=>{let r=C(e,t,!0),i=n?C(e,n,!1):[];return Object.fromEntries([...r,...i])})}const T=/^[a-zA-Z]*$/,E=/^[\w.]+$/,D=t({type:`alpha`,validator(e,t){return f(e)?!0:t?.allowSymbols?v(e,E):v(e,T)},message:`The value is not alphabetical`}),O=/^[a-zA-Z0-9]*$/,k=/^[a-zA-Z0-9_]*$/,A=t({type:`alphaNum`,validator(e,t){return f(e)?!0:t?.allowSymbols?v(e,k):v(e,O)},message:`The value must be alpha-numeric`}),j=t({type:`between`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};if(g(e)&&g(t)&&g(n)){let r=b(e),a=b(t),o=b(n);return _(r)&&_(a)&&_(o)?i?r>=a&&r<=o:r>a&&r<o:(console.warn(`[between] Value or parameters aren't numbers, got value: ${e}, min: ${t}, max: ${n}`),!1)}return!0},message:({$params:[e,t]})=>`The value must be between ${e} and ${t}`}),M=t({type:`boolean`,validator:e=>g(e)?typeof e==`boolean`:!0,message:`The value must be a native boolean`}),N=t({type:`checked`,validator:e=>g(e)?e===!0:!0,message:`The field must be checked`}),P=t({type:`contains`,validator(e,t){return g(e)&&g(t)?e.includes(t):!0},message({$params:[e]}){return`The value must contain ${e}`}}),F=t({type:`date`,validator:e=>g(e)?e instanceof Date:!0,message:`The value must be a native Date constructor`});function I(){return navigator.languages==null?navigator.language:navigator.languages[0]}function L(e){return e?new Intl.DateTimeFormat(I(),{dateStyle:`short`}).format(new Date(e)):`?`}const R=t({type:`dateAfter`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?m(e)&&m(t)?(r?h(e).getTime()>=h(t).getTime():h(e).getTime()>h(t).getTime())?!0:{$valid:!1,error:`date-not-after`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be after ${L(e)}`}),z=t({type:`dateBefore`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?m(e)&&m(t)?(r?h(e).getTime()<=h(t).getTime():h(e).getTime()<h(t).getTime())?!0:{$valid:!1,error:`date-not-before`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be before ${L(e)}`}),B=t({type:`dateBetween`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};return m(e)&&m(t)&&m(n)?i?h(e).getTime()>=h(t).getTime()&&h(e).getTime()<=h(n).getTime():h(e).getTime()>h(t).getTime()&&h(e).getTime()<h(n).getTime():!0},message:({$params:[e,t]})=>`The date must be between ${L(e)} and ${L(t)}`}),V=/^[-]?\d*(\.\d+)?$/,H=t({type:`decimal`,validator(e){return f(e)?!0:v(e,V)},message:`The value must be decimal`}),U=/^(?:[A-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]{2,}(?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i,W=t({type:`email`,validator(e){return f(e)?!0:v(e,U)},message:`The value must be an valid email address`}),G=t({type:`endsWith`,validator(e,t){return g(e)&&g(t)?e.endsWith(t):!0},message({$params:[e]}){return`The value must end with ${e}`}}),te=t({type:`exactLength`,validator:(e,t)=>g(e,!1)&&g(t)?_(t)?y(e)===t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$params:[e]})=>`The value should be exactly ${e} characters long`}),K=t({type:`exactValue`,validator:(e,t)=>g(e)&&g(t)?_(t)&&!isNaN(b(e))?b(e)===t:(console.warn(`[exactValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The value must be equal to ${e}`}),q=/^[a-fA-F0-9]*$/,J=t({type:`hexadecimal`,validator(e){return f(e)?!0:v(e,q)},message:`The value must be hexadecimal`}),Y=/(^[0-9]*$)|(^-[0-9]+$)/,X=t({type:`integer`,validator(e){return f(e)?!0:v(e,Y)},message:`The value must be an integer`});function Z(e){if(e.length>3||e.length===0||e[0]===`0`&&e!==`0`||!e.match(/^\d+$/))return!1;let t=e|0;return t>=0&&t<=255}const Q=t({type:`ipv4Address`,validator(e){if(f(e))return!0;if(typeof e!=`string`)return!1;let t=e.split(`.`);return t.length===4&&t.every(Z)},message:`The value is not a valid IPv4 address`});function ne(e){return o(l((e,t)=>g(e)&&g(t)?t===e:!0,[r(()=>i(e))]),({$params:[e]})=>`Value should be ${e}.`)}const re=t({type:`macAddress`,validator(e,t=`:`){if(f(e))return!0;if(typeof e!=`string`)return!1;let n=typeof t==`string`&&t!==``?e.split(t):e.length===12||e.length===16?e.match(/.{2}/g):null;return n!==null&&(n.length===6||n.length===8)&&n.every(ie)},message:`The value is not a valid MAC Address`}),ie=e=>e.toLowerCase().match(/^[0-9a-f]{2}$/),ae=t({type:`maxLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e,!1)&&g(t)?_(t)?r?y(e)<=t:y(e)<t:(console.warn(`[maxLength] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have maximum ${t} items`:`The value length should not exceed ${t}`}),oe=t({type:`maxValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?!isNaN(b(e))&&!isNaN(b(t))?r?b(e)<=b(t):b(e)<b(t):(console.warn(`[maxValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be less than or equal to ${e}`:`The value must be less than ${e}`}}),se=t({type:`minLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e,!1)&&g(t)?_(t)?r?y(e)>=t:y(e)>t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0},message:({$value:e,$params:[t]})=>Array.isArray(e)?`The list should have at least ${t} items`:`The value length should be at least ${t}`}),ce=t({type:`minValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return g(e)&&g(t)?!isNaN(b(e))&&!isNaN(b(t))?r?b(e)>=b(t):b(e)>b(t):(console.warn(`[minValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be greater than or equal to ${e}`:`The value must be greater than ${e}`}});function le(e){let t=Object.keys(e).filter(t=>typeof e[e[t]]!=`number`),n={};for(let r of t)n[r]=e[r];return Object.values(n)}function ue(e){return o(l((e,t)=>g(e)&&!f(t)?le(t).includes(e):!0,[r(()=>i(e))]),({$params:[e]})=>`The value should be one of those options: ${Object.values(e).join(`, `)}.`)}const de=t({type:`number`,validator:e=>g(e)?_(e):!0,message:`The value must be a native number`}),fe=/^\d*(\.\d+)?$/,pe=t({type:`numeric`,validator(e){return f(e)?!0:v(e,fe)},message:`The value must be numeric`}),$=t({type:`oneOf`,validator(e,t){return g(e)&&g(t,!1)?t.includes(e):!0},message:({$params:[e]})=>`The value should be one of those options: ${e.join(`, `)}.`}),me=t({type:`regex`,validator(e,t){return g(e)?v(e,...Array.isArray(t)?t:[t]):!0},message:`The value does not match the required pattern`}),he=t({type:`required`,validator:e=>g(e),message:`This field is required`}),ge=t({type:`required`,validator(e,t){return t?g(e):!0},message:`This field is required`,active({$params:[e]}){return e}}),_e=t({type:`required`,validator(e,t){return t?!0:g(e)},message:`This field is required`,active({$params:[e]}){return!e}}),ve=t({type:`sameAs`,validator(e,t,n){return f(e)?!0:e===t},message({$params:[e,t=`other`]}){return`The value must be equal to the ${t} value`}}),ye=t({type:`startsWith`,validator(e,t){return g(e)&&g(t)?e.startsWith(t):!0},message({$params:[e]}){return`The value must start with ${e}`}}),be=t({type:`string`,validator:e=>g(e)?typeof e==`string`:!0,message:`The value must be a string`});function xe(){return(()=>!0)}const Se=/^(?:(?:(?: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-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i,Ce=t({type:`url`,validator(e){return f(e)?!0:v(e,Se)},message:`The value is not a valid URL address`});export{D as alpha,A as alphaNum,x as and,u as applyIf,w as assignIf,j as between,M as boolean,N as checked,P as contains,F as date,R as dateAfter,z as dateBefore,B as dateBetween,H as decimal,W as email,G as endsWith,te as exactLength,K as exactValue,y as getSize,J as hexadecimal,X as integer,Q as ipv4Address,m as isDate,f as isEmpty,g as isFilled,_ as isNumber,ne as literal,re as macAddress,v as matchRegex,ae as maxLength,oe as maxValue,se as minLength,ce as minValue,ue as nativeEnum,S as not,de as number,pe as numeric,$ as oneOf,ee as or,me as regex,he as required,ge as requiredIf,_e as requiredUnless,ve as sameAs,ye as startsWith,be as string,h as toDate,b as toNumber,xe as type,Ce as url,c as withAsync,o as withMessage,l as withParams,s as withTooltip};
|
|
1
|
+
import{InternalRuleType as e,createRule as t,unwrapRuleParameters as n}from"@regle/core";import{computed as r,toValue as i,unref as a}from"vue";function o(n,r){let i,a,o,s,c=!1;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s,_async:c}=n;let l=t({type:i,validator:a,active:o,message:r,async:c}),u=[...s??[]];if(l._params=u,l._message_patched=!0,typeof l==`function`){if(s!=null){let e=l(...u);return e._message_patched=!0,e}return l}else return l}function s(n,r){let i,a,o,s,c,l=!1;typeof n==`function`&&!(`_validator`in n)?(i=e.Inline,a=n):{_type:i,validator:a,_active:o,_params:s,_message:c,_async:l}=n;let u=t({type:i,validator:a,active:o,message:c,tooltip:r,async:l}),d=[...s??[]];if(u._params=d,u._tooltip_patched=!0,typeof u==`function`){let e=u(...d);return u._tooltip_patched=!0,e}else return u}function c(n,r){let i,a,o=[],s=``;typeof n==`function`?(a=async(e,...t)=>n(e,...t),o=[r]):({_type:i,_message:s}=n,o=o=n._params?.concat(r),a=async(...e)=>n.validator(e));let c=t({type:i??e.Async,validator:a,message:s,async:!0});return c._params=c._params?.concat(o),c(...r??[])}function l(n,r){let i,a,o=[],s=``;typeof n==`function`?(e.Inline,a=(e,...t)=>n(e,...t),o=[r]):({_type:i,validator:a,_message:s}=n,o=o=n._params?.concat(r));let c=t({type:e.Inline,validator:a,message:s});return c._params=c._params?.concat(o),c(...r)}function u(r,i){let a,o,s=[],c=``;typeof i==`function`?(a=e.Inline,o=i,s=[r]):({_type:a,validator:o,_message:c}=i,s=i._params?.concat([r]));function l(e,...t){let[i]=n([r]);return i?o(e,...t):!0}function u(){let[e]=n([r]);return e}let d=t({type:a,validator:l,active:u,message:c}),f=[...s??[]];return d._params=f,typeof d==`function`?d(...f):d}function d(e){return e?.constructor?.name==`File`}function f(e,t=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):d(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:typeof e==`object`&&e?Object.keys(e).length===0:!String(e).length}function p(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function m(e){if(f(e))return!1;try{let t=null;if(e instanceof Date)t=e;else if(typeof e==`string`){let n=new Date(e);if(n.toString()===`Invalid Date`)return!1;t=n}return!!t}catch{return!1}}function h(e){let t=Object.prototype.toString.call(e);return e==null?new Date(NaN):e instanceof Date||typeof e==`object`&&t===`[object Date]`?new Date(e.getTime()):typeof e==`number`||t===`[object Number]`||typeof e==`string`||t===`[object String]`?new Date(e):new Date(NaN)}function g(e){return e===void 0?`0 bytes`:e<1024?`${e} bytes`:e<1024*1024?`${(e/1024).toFixed(2)} kb`:e<1024*1024*1024?`${(e/1024/1024).toFixed(2)} mb`:`${(e/1024/1024/1024).toFixed(2)} gb`}function _(e,t=!0){return!f(typeof e==`string`?e.trim():e,t)}function v(e){return e==null?!1:typeof e==`number`?!isNaN(e):!1}function y(e,...t){if(f(e))return!0;let n=typeof e==`number`?e.toString():e;return t.every(e=>(e.lastIndex=0,e.test(n)))}function b(e){let t=a(e);return Array.isArray(t)?t.length:typeof t==`object`?Object.keys(t).length:typeof t==`number`?isNaN(t)?0:t:String(t).length}function x(e){return typeof e==`number`?e:e==null?NaN:typeof e==`string`&&e.trim()===e?+e:NaN}function S(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>{if(typeof e==`function`)return null;{let t=e._params;return t?.length?t:[]}}).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e?.some(e=>typeof e!=`boolean`)?{$valid:e.every(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.every(e=>!!e)}let o;o=e.length?n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)):e=>!1;let s=t({type:`and`,validator:o,message:`The value does not match all of the provided validators`}),c=[...r??[]];return s._params=c,typeof s==`function`?s(...c):s}function ee(...e){let n=e.some(e=>typeof e==`function`?e.constructor.name===`AsyncFunction`:e._async),r=e.map(e=>typeof e==`function`?null:e._params).flat().filter(e=>!!e);function i(e,t,...n){let r=[],i=0;for(let a of e)if(typeof a==`function`)r.push(a(t)),i++;else{let e=a._params?.length??0;r.push(a.validator(t,...n.slice(i,e))),e&&(i+=e)}return r}function a(e){return e.some(e=>typeof e!=`boolean`)?{$valid:e.some(e=>typeof e==`boolean`?!!e:e.$valid),...e.reduce((e,t)=>{if(typeof t==`boolean`)return e;let{$valid:n,...r}=t;return{...e,...r}},{})}:e.some(e=>!!e)}let o=t({type:`or`,validator:n?async(t,...n)=>a(await Promise.all(i(e,t,...n))):(t,...n)=>a(i(e,t,...n)),message:`The value does not match any of the provided validators`}),s=[...r??[]];return o._params=s,typeof o==`function`?o(...s):o}function C(e,n){let r,i,a,o,s;typeof e==`function`?(i=e,s=e.constructor.name===`AsyncFunction`):({_type:r,validator:i,_params:o}=e,s=e._async),a=s?async(e,...t)=>_(e)?!await i(e,...t):!0:(e,...t)=>_(e)?!i(e,...t):!0;let c=t({type:`not`,validator:a,message:n??`Error`}),l=[...o??[]];return c._params=l,typeof c==`function`?c(...l):c}function w(e,t,n){return Object.entries(i(t)).map(([t,r])=>typeof r==`function`||p(r)&&`_validator`in r?[t,u(n?e:()=>!i(e),r)]:[t,r])}function T(e,t,n){return r(()=>{let r=w(e,t,!0),i=n?w(e,n,!1):[];return Object.fromEntries([...r,...i])})}const E=/^[a-zA-Z]*$/,D=/^[\w.]+$/,O=t({type:`alpha`,validator(e,t){return f(e)?!0:t?.allowSymbols?y(e,D):y(e,E)},message:`The value is not alphabetical`}),k=/^[a-zA-Z0-9]*$/,A=/^[a-zA-Z0-9_]*$/,j=t({type:`alphaNum`,validator(e,t){return f(e)?!0:t?.allowSymbols?y(e,A):y(e,k)},message:`The value must be alpha-numeric`}),M=t({type:`between`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};if(_(e)&&_(t)&&_(n)){let r=x(e),a=x(t),o=x(n);return v(r)&&v(a)&&v(o)?i?r>=a&&r<=o:r>a&&r<o:(console.warn(`[between] Value or parameters aren't numbers, got value: ${e}, min: ${t}, max: ${n}`),!1)}return!0},message:({$params:[e,t]})=>`The value must be between ${e} and ${t}`}),N=t({type:`boolean`,validator:e=>_(e)?typeof e==`boolean`:!0,message:`The value must be a native boolean`}),P=t({type:`checked`,validator:e=>_(e)?e===!0:!0,message:`The field must be checked`}),F=t({type:`contains`,validator(e,t){return _(e)&&_(t)?e.includes(t):!0},message({$params:[e]}){return`The value must contain ${e}`}}),I=t({type:`date`,validator:e=>_(e)?e instanceof Date:!0,message:`The value must be a native Date`});function L(){return navigator.languages==null?navigator.language:navigator.languages[0]}function R(e){return e?new Intl.DateTimeFormat(L(),{dateStyle:`short`}).format(new Date(e)):`?`}const z=t({type:`dateAfter`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e)&&_(t)?m(e)&&m(t)?(r?h(e).getTime()>=h(t).getTime():h(e).getTime()>h(t).getTime())?!0:{$valid:!1,error:`date-not-after`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be after ${R(e)}`}),B=t({type:`dateBefore`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e)&&_(t)?m(e)&&m(t)?(r?h(e).getTime()<=h(t).getTime():h(e).getTime()<h(t).getTime())?!0:{$valid:!1,error:`date-not-before`}:{$valid:!1,error:`value-or-parameter-not-a-date`}:!0},message:({$params:[e],error:t})=>t===`value-or-parameter-not-a-date`?`The values must be dates`:`The date must be before ${R(e)}`}),V=t({type:`dateBetween`,validator:(e,t,n,r)=>{let{allowEqual:i=!0}=r??{};return m(e)&&m(t)&&m(n)?i?h(e).getTime()>=h(t).getTime()&&h(e).getTime()<=h(n).getTime():h(e).getTime()>h(t).getTime()&&h(e).getTime()<h(n).getTime():!0},message:({$params:[e,t]})=>`The date must be between ${R(e)} and ${R(t)}`}),H=/^[-]?\d*(\.\d+)?$/,U=t({type:`decimal`,validator(e){return f(e)?!0:y(e,H)},message:`The value must be decimal`}),W=/^(?:[A-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]{2,}(?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i,G=t({type:`email`,validator(e){return f(e)?!0:y(e,W)},message:`The value must be an valid email address`}),K=t({type:`endsWith`,validator(e,t){return _(e)&&_(t)?e.endsWith(t):!0},message({$params:[e]}){return`The value must end with ${e}`}}),q=t({type:`exactLength`,validator:(e,t)=>_(e,!1)&&_(t)?v(t)?b(e)===t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0,message:({$params:[e]})=>`The value should be exactly ${e} characters long`}),J=t({type:`exactValue`,validator:(e,t)=>_(e)&&_(t)?v(t)&&!isNaN(x(e))?x(e)===t:(console.warn(`[exactValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!0):!0,message:({$params:[e]})=>`The value must be equal to ${e}`}),Y=t({type:`file`,validator:e=>_(e)?d(e):!0,message:`The value must be a native File`}),X=t({type:`fileType`,validator:(e,t)=>_(e)?d(e)?t.includes(e.type):!1:!0,message({$params:[e]}){return`File type is not allowed. Allowed types are: ${e.map(e=>e.split(`/`)[1]).join(`, `)}.`}}),te=/^[a-fA-F0-9]*$/,Z=t({type:`hexadecimal`,validator(e){return f(e)?!0:y(e,te)},message:`The value must be hexadecimal`}),Q=/(^[0-9]*$)|(^-[0-9]+$)/,ne=t({type:`integer`,validator(e){return f(e)?!0:y(e,Q)},message:`The value must be an integer`});function re(e){if(e.length>3||e.length===0||e[0]===`0`&&e!==`0`||!e.match(/^\d+$/))return!1;let t=e|0;return t>=0&&t<=255}const ie=t({type:`ipv4Address`,validator(e){if(f(e))return!0;if(typeof e!=`string`)return!1;let t=e.split(`.`);return t.length===4&&t.every(re)},message:`The value is not a valid IPv4 address`});function ae(e){return o(l((e,t)=>_(e)&&_(t)?t===e:!0,[r(()=>i(e))]),({$params:[e]})=>`Value should be ${e}.`)}const oe=t({type:`macAddress`,validator(e,t=`:`){if(f(e))return!0;if(typeof e!=`string`)return!1;let n=typeof t==`string`&&t!==``?e.split(t):e.length===12||e.length===16?e.match(/.{2}/g):null;return n!==null&&(n.length===6||n.length===8)&&n.every(se)},message:`The value is not a valid MAC Address`}),se=e=>e.toLowerCase().match(/^[0-9a-f]{2}$/),ce=t({type:`maxFileSize`,validator:(e,t)=>_(e)&&d(e)?{$valid:e.size<=t,fileSize:e.size}:!0,message({$params:[e],fileSize:t}){return`File size (${g(t)}) cannot exceed ${g(e)}`}}),le=t({type:`maxLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e,!1)&&_(t)?v(t)?r?b(e)<=t:b(e)<t:(console.warn(`[maxLength] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$value:e,$params:[t]})=>Array.isArray(e)?`This list should have maximum ${t} items`:`The value length should not exceed ${t}`}),ue=t({type:`maxValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e)&&_(t)?!isNaN(x(e))&&!isNaN(x(t))?r?x(e)<=x(t):x(e)<x(t):(console.warn(`[maxValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be less than or equal to ${e}`:`The value must be less than ${e}`}}),de=t({type:`minFileSize`,validator:(e,t)=>_(e)&&d(e)?{$valid:e.size>=t,fileSize:e.size}:!0,message({$params:[e],fileSize:t}){return`File size (${g(t)}) must be at least ${g(e)}`}}),fe=t({type:`minLength`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e,!1)&&_(t)?v(t)?r?b(e)>=t:b(e)>t:(console.warn(`[minLength] Parameter isn't a number, got parameter: ${t}`),!1):!0},message:({$value:e,$params:[t]})=>Array.isArray(e)?`The list should have at least ${t} items`:`The value length should be at least ${t}`}),pe=t({type:`minValue`,validator:(e,t,n)=>{let{allowEqual:r=!0}=n??{};return _(e)&&_(t)?!isNaN(x(e))&&!isNaN(x(t))?r?x(e)>=x(t):x(e)>x(t):(console.warn(`[minValue] Value or parameter isn't a number, got value: ${e}, parameter: ${t}`),!1):!0},message:({$params:[e,t]})=>{let{allowEqual:n=!0}=t??{};return n?`The value must be greater than or equal to ${e}`:`The value must be greater than ${e}`}});function me(e){let t=Object.keys(e).filter(t=>typeof e[e[t]]!=`number`),n={};for(let r of t)n[r]=e[r];return Object.values(n)}function he(e){return o(l((e,t)=>_(e)&&!f(t)?me(t).includes(e):!0,[r(()=>i(e))]),({$params:[e]})=>`The value should be one of those options: ${Object.values(e).join(`, `)}.`)}const ge=t({type:`number`,validator:e=>_(e)?v(e):!0,message:`The value must be a native number`}),_e=/^\d*(\.\d+)?$/,ve=t({type:`numeric`,validator(e){return f(e)?!0:y(e,_e)},message:`The value must be numeric`}),ye=t({type:`oneOf`,validator(e,t){return _(e)&&_(t,!1)?t.includes(e):!0},message:({$params:[e]})=>`The value should be one of those options: ${e.join(`, `)}.`}),be=t({type:`regex`,validator(e,t){return _(e)?y(e,...Array.isArray(t)?t:[t]):!0},message:`The value does not match the required pattern`}),$=t({type:`required`,validator:e=>_(e),message:`This field is required`}),xe=t({type:`required`,validator(e,t){return t?_(e):!0},message:`This field is required`,active({$params:[e]}){return e}}),Se=t({type:`required`,validator(e,t){return t?!0:_(e)},message:`This field is required`,active({$params:[e]}){return!e}}),Ce=t({type:`sameAs`,validator(e,t,n){return f(e)?!0:e===t},message({$params:[e,t=`other`]}){return`The value must be equal to the ${t} value`}}),we=t({type:`startsWith`,validator(e,t){return _(e)&&_(t)?e.startsWith(t):!0},message({$params:[e]}){return`The value must start with ${e}`}}),Te=t({type:`string`,validator:e=>_(e)?typeof e==`string`:!0,message:`The value must be a string`});function Ee(){return(()=>!0)}const De=/^(?:(?:(?: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-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i,Oe=t({type:`url`,validator(e){return f(e)?!0:y(e,De)},message:`The value is not a valid URL address`});export{O as alpha,j as alphaNum,S as and,u as applyIf,T as assignIf,M as between,N as boolean,P as checked,F as contains,I as date,z as dateAfter,B as dateBefore,V as dateBetween,U as decimal,G as email,K as endsWith,q as exactLength,J as exactValue,Y as file,X as fileType,b as getSize,Z as hexadecimal,ne as integer,ie as ipv4Address,m as isDate,f as isEmpty,_ as isFilled,v as isNumber,ae as literal,oe as macAddress,y as matchRegex,ce as maxFileSize,le as maxLength,ue as maxValue,de as minFileSize,fe as minLength,pe as minValue,he as nativeEnum,C as not,ge as number,ve as numeric,ye as oneOf,ee as or,be as regex,$ as required,xe as requiredIf,Se as requiredUnless,Ce as sameAs,we as startsWith,Te as string,h as toDate,x as toNumber,Ee as type,Oe as url,c as withAsync,o as withMessage,l as withParams,s as withTooltip};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/rules",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.1",
|
|
4
4
|
"description": "Collection of rules and helpers for Regle",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"type-fest": "5.2.0",
|
|
7
|
-
"@regle/core": "1.
|
|
7
|
+
"@regle/core": "1.15.1"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
10
|
"@types/node": "22.19.1",
|