@projectcaluma/ember-form 11.0.0-beta.6 → 11.0.0-beta.7

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ # [@projectcaluma/ember-form-v11.0.0-beta.7](https://github.com/projectcaluma/ember-caluma/compare/@projectcaluma/ember-form-v11.0.0-beta.6...@projectcaluma/ember-form-v11.0.0-beta.7) (2022-02-03)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **validation:** sync format validator validation with backend ([ee66968](https://github.com/projectcaluma/ember-caluma/commit/ee66968230b9f0e4c5a4df8bdb3f8e58b44b5b82))
7
+
8
+
9
+ ### BREAKING CHANGES
10
+
11
+ * **validation:** Use the `formatValidators` property of the backend to store and read
12
+ format validators instead of the `meta.formatValidators` so the backend
13
+ validates as well. For more information on how to migrate check the
14
+ migration guide to v11.
15
+
1
16
  # [@projectcaluma/ember-form-v11.0.0-beta.6](https://github.com/projectcaluma/ember-caluma/compare/@projectcaluma/ember-form-v11.0.0-beta.5...@projectcaluma/ember-form-v11.0.0-beta.6) (2022-02-02)
2
17
 
3
18
 
@@ -19,6 +19,15 @@ fragment SimpleQuestion on Question {
19
19
  value
20
20
  }
21
21
  placeholder
22
+ formatValidators {
23
+ edges {
24
+ node {
25
+ slug
26
+ regex
27
+ errorMsg
28
+ }
29
+ }
30
+ }
22
31
  }
23
32
  ... on TextareaQuestion {
24
33
  textareaMinLength: minLength
@@ -28,6 +37,15 @@ fragment SimpleQuestion on Question {
28
37
  value
29
38
  }
30
39
  placeholder
40
+ formatValidators {
41
+ edges {
42
+ node {
43
+ slug
44
+ regex
45
+ errorMsg
46
+ }
47
+ }
48
+ }
31
49
  }
32
50
  ... on IntegerQuestion {
33
51
  integerMinValue: minValue
@@ -3,6 +3,7 @@ import { assert } from "@ember/debug";
3
3
  import { associateDestroyableChild } from "@ember/destroyable";
4
4
  import { inject as service } from "@ember/service";
5
5
  import { camelize } from "@ember/string";
6
+ import { isEmpty } from "@ember/utils";
6
7
  import { tracked } from "@glimmer/tracking";
7
8
  import { queryManager } from "ember-apollo-client";
8
9
  import { restartableTask, lastValue, dropTask } from "ember-concurrency";
@@ -63,7 +64,6 @@ const fieldIsHiddenOrEmpty = (field) => {
63
64
  */
64
65
  export default class Field extends Base {
65
66
  @service intl;
66
- @service validator;
67
67
 
68
68
  @queryManager apollo;
69
69
 
@@ -628,6 +628,35 @@ export default class Field extends Base {
628
628
  this._errors = errors;
629
629
  }
630
630
 
631
+ /**
632
+ * Validate the value against the regexes of the given format validators.
633
+ *
634
+ * @method _validateFormatValidators
635
+ * @return {Array<Boolean|Object>} An array of error objects or `true`
636
+ * @private
637
+ */
638
+ _validateFormatValidators() {
639
+ const validators =
640
+ this.question.raw.formatValidators?.edges.map((edge) => edge.node) ?? [];
641
+ const value = this.answer.value;
642
+
643
+ if (isEmpty(value)) {
644
+ // empty values should not be validated since they are handled by the
645
+ // requiredness validation
646
+ return validators.map(() => true);
647
+ }
648
+
649
+ return validators.map((validator) => {
650
+ return (
651
+ new RegExp(validator.regex).test(value) || {
652
+ type: "format",
653
+ context: { errorMsg: validator.errorMsg },
654
+ value,
655
+ }
656
+ );
657
+ });
658
+ }
659
+
631
660
  /**
632
661
  * Method to validate if a question is required or not.
633
662
  *
@@ -647,15 +676,12 @@ export default class Field extends Base {
647
676
  * predefined by the question.
648
677
  *
649
678
  * @method _validateTextQuestion
650
- * @return {Promise<Boolean|Object>} A promise which resolves into an object if invalid or true if valid
679
+ * @return {Array<Boolean|Object>} An array of error objects or `true`
651
680
  * @private
652
681
  */
653
- async _validateTextQuestion() {
682
+ _validateTextQuestion() {
654
683
  return [
655
- ...(await this.validator.validate(
656
- this.answer.value,
657
- this.question.raw.meta.formatValidators ?? []
658
- )),
684
+ ...this._validateFormatValidators(),
659
685
  validate("length", this.answer.value, {
660
686
  min: this.question.raw.textMinLength || 0,
661
687
  max: this.question.raw.textMaxLength || Number.POSITIVE_INFINITY,
@@ -668,15 +694,12 @@ export default class Field extends Base {
668
694
  * than predefined by the question.
669
695
  *
670
696
  * @method _validateTextareaQuestion
671
- * @return {Promise<Boolean|Object>} A promise which resolves into an object if invalid or true if valid
697
+ * @return {Array<Boolean|Object>} An array of error objects or `true`
672
698
  * @private
673
699
  */
674
- async _validateTextareaQuestion() {
700
+ _validateTextareaQuestion() {
675
701
  return [
676
- ...(await this.validator.validate(
677
- this.answer.value,
678
- this.question.raw.meta.formatValidators ?? []
679
- )),
702
+ ...this._validateFormatValidators(),
680
703
  validate("length", this.answer.value, {
681
704
  min: this.question.raw.textareaMinLength || 0,
682
705
  max: this.question.raw.textareaMaxLength || Number.POSITIVE_INFINITY,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projectcaluma/ember-form",
3
- "version": "11.0.0-beta.6",
3
+ "version": "11.0.0-beta.7",
4
4
  "description": "Ember addon for rendering Caluma forms.",
5
5
  "keywords": [
6
6
  "ember-addon"
@@ -16,7 +16,7 @@
16
16
  "dependencies": {
17
17
  "@glimmer/component": "^1.0.4",
18
18
  "@glimmer/tracking": "^1.0.4",
19
- "@projectcaluma/ember-core": "^11.0.0-beta.2",
19
+ "@projectcaluma/ember-core": "^11.0.0-beta.3",
20
20
  "ember-apollo-client": "^3.2.0",
21
21
  "ember-auto-import": "^2.4.0",
22
22
  "ember-autoresize-modifier": "^0.5.0",
@@ -45,7 +45,7 @@
45
45
  "@ember/test-helpers": "2.6.0",
46
46
  "@embroider/test-setup": "1.0.0",
47
47
  "@faker-js/faker": "6.0.0-alpha.5",
48
- "@projectcaluma/ember-testing": "10.2.0-beta.2",
48
+ "@projectcaluma/ember-testing": "11.0.0-beta.1",
49
49
  "@projectcaluma/ember-workflow": "11.0.0-beta.3",
50
50
  "broccoli-asset-rev": "3.0.0",
51
51
  "ember-cli": "3.28.5",