@sprucelabs/schema 34.0.7 → 34.1.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.
@@ -129,9 +129,13 @@ class StaticSchemaEntityImpl extends AbstractEntity_1.default {
129
129
  const isMissingRequiredOrMinValues = field.isRequired &&
130
130
  field.isArray &&
131
131
  (!value || valueAsArray.length < (field.minArrayLength ?? 1));
132
+ const hasMaxArrayLength = !!field.maxArrayLength &&
133
+ valueAsArray.length > field.maxArrayLength;
132
134
  const isMissing = !value;
133
135
  const shouldBeArrayButIsnt = !wasArray && !isMissing && field.isArray;
134
- if (isMissingRequiredOrMinValues || shouldBeArrayButIsnt) {
136
+ if (isMissingRequiredOrMinValues ||
137
+ shouldBeArrayButIsnt ||
138
+ hasMaxArrayLength) {
135
139
  const missingRequiredError = `${field.label ? `'${field.label}'` : 'This'} is required!`;
136
140
  const missingMinValuesError = `${field.label ? `'${field.label}'` : 'You'} must ${field.label ? 'have' : 'select'} at least ${field.minArrayLength} value${field.minArrayLength === 1 ? '' : 's'}. I found ${valueAsArray.length}!`;
137
141
  const mustBeArrayError = `${field.label ? `'${field.label}'` : 'This'} must be an array!`;
@@ -93,9 +93,13 @@ class StaticSchemaEntityImpl extends AbstractEntity {
93
93
  const isMissingRequiredOrMinValues = field.isRequired &&
94
94
  field.isArray &&
95
95
  (!value || valueAsArray.length < ((_a = field.minArrayLength) !== null && _a !== void 0 ? _a : 1));
96
+ const hasMaxArrayLength = !!field.maxArrayLength &&
97
+ valueAsArray.length > field.maxArrayLength;
96
98
  const isMissing = !value;
97
99
  const shouldBeArrayButIsnt = !wasArray && !isMissing && field.isArray;
98
- if (isMissingRequiredOrMinValues || shouldBeArrayButIsnt) {
100
+ if (isMissingRequiredOrMinValues ||
101
+ shouldBeArrayButIsnt ||
102
+ hasMaxArrayLength) {
99
103
  const missingRequiredError = `${field.label ? `'${field.label}'` : 'This'} is required!`;
100
104
  const missingMinValuesError = `${field.label ? `'${field.label}'` : 'You'} must ${field.label ? 'have' : 'select'} at least ${field.minArrayLength} value${field.minArrayLength === 1 ? '' : 's'}. I found ${valueAsArray.length}!`;
101
105
  const mustBeArrayError = `${field.label ? `'${field.label}'` : 'This'} must be an array!`;
@@ -24,6 +24,7 @@ export default abstract class AbstractField<F extends FieldDefinitions> implemen
24
24
  get label(): string | undefined;
25
25
  get hint(): string | undefined;
26
26
  get minArrayLength(): number;
27
+ get maxArrayLength(): number | undefined;
27
28
  validate(value: any, _?: ValidateOptions<F>): FieldError[];
28
29
  /** Transform any value to the value type of this field */
29
30
  toValueType(value: any, _: any): any;
@@ -52,6 +52,9 @@ public static generateTemplateDetails(
52
52
  var _b;
53
53
  return (_b = this.definition.minArrayLength) !== null && _b !== void 0 ? _b : 1;
54
54
  }
55
+ get maxArrayLength() {
56
+ return this.definition.maxArrayLength;
57
+ }
55
58
  validate(value, _) {
56
59
  const errors = [];
57
60
  if (isUndefinedOrNull(value) && this.isRequired) {
@@ -34,6 +34,7 @@ export type FieldDefinition<Value = any, DefaultValue = Value, ArrayValue = Valu
34
34
  hint?: string;
35
35
  isRequired?: boolean;
36
36
  minArrayLength?: number;
37
+ maxArrayLength?: number;
37
38
  } & ({
38
39
  isArray: true;
39
40
  defaultValue?: DefaultArrayValue | null;
@@ -52,6 +53,7 @@ export interface Field<F extends FieldDefinitions> {
52
53
  readonly isPrivate: F['isPrivate'];
53
54
  readonly isArray: F['isArray'];
54
55
  readonly minArrayLength: F['minArrayLength'];
56
+ readonly maxArrayLength: F['maxArrayLength'];
55
57
  readonly label: F['label'];
56
58
  readonly hint: F['hint'];
57
59
  name: string;
@@ -24,6 +24,7 @@ export default abstract class AbstractField<F extends FieldDefinitions> implemen
24
24
  get label(): string | undefined;
25
25
  get hint(): string | undefined;
26
26
  get minArrayLength(): number;
27
+ get maxArrayLength(): number | undefined;
27
28
  validate(value: any, _?: ValidateOptions<F>): FieldError[];
28
29
  /** Transform any value to the value type of this field */
29
30
  toValueType(value: any, _: any): any;
@@ -56,6 +56,9 @@ public static generateTemplateDetails(
56
56
  get minArrayLength() {
57
57
  return this.definition.minArrayLength ?? 1;
58
58
  }
59
+ get maxArrayLength() {
60
+ return this.definition.maxArrayLength;
61
+ }
59
62
  validate(value, _) {
60
63
  const errors = [];
61
64
  if ((0, isUndefinedOrNull_1.default)(value) && this.isRequired) {
@@ -34,6 +34,7 @@ export type FieldDefinition<Value = any, DefaultValue = Value, ArrayValue = Valu
34
34
  hint?: string;
35
35
  isRequired?: boolean;
36
36
  minArrayLength?: number;
37
+ maxArrayLength?: number;
37
38
  } & ({
38
39
  isArray: true;
39
40
  defaultValue?: DefaultArrayValue | null;
@@ -52,6 +53,7 @@ export interface Field<F extends FieldDefinitions> {
52
53
  readonly isPrivate: F['isPrivate'];
53
54
  readonly isArray: F['isArray'];
54
55
  readonly minArrayLength: F['minArrayLength'];
56
+ readonly maxArrayLength: F['maxArrayLength'];
55
57
  readonly label: F['label'];
56
58
  readonly hint: F['hint'];
57
59
  name: string;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "!build/__tests__",
9
9
  "esm"
10
10
  ],
11
- "version": "34.0.7",
11
+ "version": "34.1.0",
12
12
  "main": "./build/index.js",
13
13
  "types": "./build/index.d.ts",
14
14
  "module": "./build/esm/index.js",