@ciwergrp/nuxid 1.7.2 → 1.8.0

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/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ciwergrp/nuxid",
3
3
  "configKey": "nuxid",
4
- "version": "1.7.2",
4
+ "version": "1.8.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
@@ -1,6 +1,6 @@
1
- const createSameAsValidator = (otherField, formState, message) => {
1
+ const createSameAsValidator = (otherField, getFormState, message) => {
2
2
  return (_rule, value, callback) => {
3
- const otherValue = getNestedValue(formState, otherField);
3
+ const otherValue = getNestedValue(getFormState(), otherField);
4
4
  if (value !== otherValue) {
5
5
  callback(new Error(message));
6
6
  } else {
@@ -206,9 +206,9 @@ const createIntegerValidator = (message) => {
206
206
  }
207
207
  };
208
208
  };
209
- const createComparisonValidator = (otherField, formState, message, compare) => {
209
+ const createComparisonValidator = (otherField, getFormState, message, compare) => {
210
210
  return (_rule, value, callback) => {
211
- const otherValue = getNestedValue(formState, otherField);
211
+ const otherValue = getNestedValue(getFormState(), otherField);
212
212
  if (value && otherValue && !compare(value, otherValue)) {
213
213
  callback(new Error(message));
214
214
  } else {
@@ -331,10 +331,14 @@ export const createValidationRules = (definitions, formState, options = {}) => {
331
331
  if (authorize && !authorize()) {
332
332
  throw new Error("This action is unauthorized.");
333
333
  }
334
- let dataToValidate = { ...formState };
335
- if (prepareForValidation) {
336
- dataToValidate = prepareForValidation(dataToValidate);
337
- }
334
+ const getValidationData = () => {
335
+ const nextData = { ...formState };
336
+ if (prepareForValidation) {
337
+ return prepareForValidation(nextData);
338
+ }
339
+ return nextData;
340
+ };
341
+ const dataToValidate = getValidationData();
338
342
  const rules = {};
339
343
  const getMessage = (fieldName, rule, defaultMessage) => {
340
344
  const customMessageKey = `${fieldName}.${rule}`;
@@ -397,7 +401,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
397
401
  const expectedValues = paramValues.slice(1);
398
402
  fieldRules.push({
399
403
  validator: (_rule, value, callback) => {
400
- const otherValue = getNestedValue(dataToValidate, otherField);
404
+ const otherValue = getNestedValue(getValidationData(), otherField);
401
405
  const shouldRequire = expectedValues.some((expected) => normalizeComparisonValue(otherValue, expected));
402
406
  if (!shouldRequire) {
403
407
  return callback();
@@ -428,7 +432,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
428
432
  case "filled":
429
433
  fieldRules.push({
430
434
  validator: (_rule, value, callback) => {
431
- if (!hasNestedKey(dataToValidate, fieldName)) {
435
+ if (!hasNestedKey(getValidationData(), fieldName)) {
432
436
  return callback();
433
437
  }
434
438
  if (!isFilledValue(value)) {
@@ -442,7 +446,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
442
446
  case "present":
443
447
  fieldRules.push({
444
448
  validator: (_rule, _value, callback) => {
445
- if (!hasNestedKey(dataToValidate, fieldName)) {
449
+ if (!hasNestedKey(getValidationData(), fieldName)) {
446
450
  return callback(new Error(getMessage(fieldName, "present", `${attributeName} must be present`)));
447
451
  }
448
452
  callback();
@@ -458,7 +462,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
458
462
  const expectedValues = paramValues.slice(1);
459
463
  fieldRules.push({
460
464
  validator: (_rule, value, callback) => {
461
- const otherValue = getNestedValue(dataToValidate, otherField);
465
+ const otherValue = getNestedValue(getValidationData(), otherField);
462
466
  const shouldRequire = expectedValues.some((expected) => normalizeComparisonValue(otherValue, expected));
463
467
  if (!shouldRequire) {
464
468
  return callback();
@@ -480,7 +484,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
480
484
  const expectedValues = paramValues.slice(1);
481
485
  fieldRules.push({
482
486
  validator: (_rule, value, callback) => {
483
- const otherValue = getNestedValue(dataToValidate, otherField);
487
+ const otherValue = getNestedValue(getValidationData(), otherField);
484
488
  const isExcluded = expectedValues.some((expected) => normalizeComparisonValue(otherValue, expected));
485
489
  if (isExcluded) {
486
490
  return callback();
@@ -501,7 +505,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
501
505
  const otherFields = paramValues;
502
506
  fieldRules.push({
503
507
  validator: (_rule, value, callback) => {
504
- if (!hasAnyFilled(dataToValidate, otherFields)) {
508
+ if (!hasAnyFilled(getValidationData(), otherFields)) {
505
509
  return callback();
506
510
  }
507
511
  if (!isFilledValue(value)) {
@@ -520,7 +524,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
520
524
  const otherFields = paramValues;
521
525
  fieldRules.push({
522
526
  validator: (_rule, value, callback) => {
523
- if (!hasAllFilled(dataToValidate, otherFields)) {
527
+ if (!hasAllFilled(getValidationData(), otherFields)) {
524
528
  return callback();
525
529
  }
526
530
  if (!isFilledValue(value)) {
@@ -539,7 +543,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
539
543
  const otherFields = paramValues;
540
544
  fieldRules.push({
541
545
  validator: (_rule, value, callback) => {
542
- if (hasAnyFilled(dataToValidate, otherFields)) {
546
+ if (hasAnyFilled(getValidationData(), otherFields)) {
543
547
  return callback();
544
548
  }
545
549
  if (!isFilledValue(value)) {
@@ -558,7 +562,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
558
562
  const otherFields = paramValues;
559
563
  fieldRules.push({
560
564
  validator: (_rule, value, callback) => {
561
- if (hasAllFilled(dataToValidate, otherFields)) {
565
+ if (hasAllFilled(getValidationData(), otherFields)) {
562
566
  return callback();
563
567
  }
564
568
  if (!isFilledValue(value)) {
@@ -589,7 +593,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
589
593
  const expectedValues = paramValues.slice(1);
590
594
  fieldRules.push({
591
595
  validator: (_rule, value, callback) => {
592
- const otherValue = getNestedValue(dataToValidate, otherField);
596
+ const otherValue = getNestedValue(getValidationData(), otherField);
593
597
  const shouldProhibit = expectedValues.some((expected) => normalizeComparisonValue(otherValue, expected));
594
598
  if (!shouldProhibit) {
595
599
  return callback();
@@ -611,7 +615,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
611
615
  const expectedValues = paramValues.slice(1);
612
616
  fieldRules.push({
613
617
  validator: (_rule, value, callback) => {
614
- const otherValue = getNestedValue(dataToValidate, otherField);
618
+ const otherValue = getNestedValue(getValidationData(), otherField);
615
619
  const isAllowed = expectedValues.some((expected) => normalizeComparisonValue(otherValue, expected));
616
620
  if (isAllowed) {
617
621
  return callback();
@@ -635,7 +639,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
635
639
  if (!isFilledValue(value)) {
636
640
  return callback();
637
641
  }
638
- const hasAny = otherFields.some((field) => isFilledValue(getNestedValue(dataToValidate, field)));
642
+ const hasAny = otherFields.some((field) => isFilledValue(getNestedValue(getValidationData(), field)));
639
643
  if (hasAny) {
640
644
  return callback(new Error(getMessage(fieldName, "prohibits", `${attributeName} prohibits ${otherFields.join(", ")}`)));
641
645
  }
@@ -749,7 +753,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
749
753
  }
750
754
  const sameOtherAttr = attributes[firstParam] || firstParam;
751
755
  fieldRules.push({
752
- validator: createSameAsValidator(firstParam, dataToValidate, getMessage(fieldName, "same", `${attributeName} must match ${sameOtherAttr}`)),
756
+ validator: createSameAsValidator(firstParam, getValidationData, getMessage(fieldName, "same", `${attributeName} must match ${sameOtherAttr}`)),
753
757
  trigger: pickTrigger(fieldName, "blur")
754
758
  });
755
759
  break;
@@ -760,7 +764,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
760
764
  }
761
765
  const diffOtherAttr = attributes[firstParam] || firstParam;
762
766
  fieldRules.push({
763
- validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "different", `${attributeName} must be different from ${diffOtherAttr}`), (a, b) => a !== b),
767
+ validator: createComparisonValidator(firstParam, getValidationData, getMessage(fieldName, "different", `${attributeName} must be different from ${diffOtherAttr}`), (a, b) => a !== b),
764
768
  trigger: pickTrigger(fieldName, "blur")
765
769
  });
766
770
  break;
@@ -896,7 +900,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
896
900
  if (isEmptyValue(value)) {
897
901
  return callback();
898
902
  }
899
- const haystack = getNestedValue(dataToValidate, otherField);
903
+ const haystack = getNestedValue(getValidationData(), otherField);
900
904
  if (!Array.isArray(haystack)) {
901
905
  return callback(new Error(getMessage(fieldName, "in_array", `${attributeName} must be one of the values in ${otherField}`)));
902
906
  }
@@ -1153,7 +1157,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
1153
1157
  if (isEmptyValue(value)) {
1154
1158
  return callback();
1155
1159
  }
1156
- const confirmationValue = getNestedValue(dataToValidate, confirmationField);
1160
+ const confirmationValue = getNestedValue(getValidationData(), confirmationField);
1157
1161
  if (value !== confirmationValue) {
1158
1162
  return callback(new Error(getMessage(fieldName, "confirmed", `${attributeName} confirmation does not match`)));
1159
1163
  }
@@ -1210,7 +1214,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
1210
1214
  }
1211
1215
  const gtOtherAttr = attributes[firstParam] || firstParam;
1212
1216
  fieldRules.push({
1213
- validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "gt", `${attributeName} must be greater than ${gtOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x > y)),
1217
+ validator: createComparisonValidator(firstParam, getValidationData, getMessage(fieldName, "gt", `${attributeName} must be greater than ${gtOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x > y)),
1214
1218
  trigger: pickTrigger(fieldName, "blur")
1215
1219
  });
1216
1220
  break;
@@ -1221,7 +1225,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
1221
1225
  }
1222
1226
  const gteOtherAttr = attributes[firstParam] || firstParam;
1223
1227
  fieldRules.push({
1224
- validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "gte", `${attributeName} must be greater than or equal to ${gteOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x >= y)),
1228
+ validator: createComparisonValidator(firstParam, getValidationData, getMessage(fieldName, "gte", `${attributeName} must be greater than or equal to ${gteOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x >= y)),
1225
1229
  trigger: "blur"
1226
1230
  });
1227
1231
  break;
@@ -1232,7 +1236,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
1232
1236
  }
1233
1237
  const ltOtherAttr = attributes[firstParam] || firstParam;
1234
1238
  fieldRules.push({
1235
- validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "lt", `${attributeName} must be less than ${ltOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x < y)),
1239
+ validator: createComparisonValidator(firstParam, getValidationData, getMessage(fieldName, "lt", `${attributeName} must be less than ${ltOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x < y)),
1236
1240
  trigger: "blur"
1237
1241
  });
1238
1242
  break;
@@ -1243,7 +1247,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
1243
1247
  }
1244
1248
  const lteOtherAttr = attributes[firstParam] || firstParam;
1245
1249
  fieldRules.push({
1246
- validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "lte", `${attributeName} must be less than or equal to ${lteOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x <= y)),
1250
+ validator: createComparisonValidator(firstParam, getValidationData, getMessage(fieldName, "lte", `${attributeName} must be less than or equal to ${lteOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x <= y)),
1247
1251
  trigger: "blur"
1248
1252
  });
1249
1253
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ciwergrp/nuxid",
3
- "version": "1.7.2",
3
+ "version": "1.8.0",
4
4
  "description": "All-in-one essential modules for Nuxt",
5
5
  "repository": {
6
6
  "type": "git",