@strapi/typescript-utils 4.19.1 → 4.20.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.
@@ -671,6 +671,38 @@ describe('Attributes', () => {
671
671
  // Check for string keyword on the second typeArgument
672
672
  expect(typeofMinMax.kind).toBe(ts.SyntaxKind.StringKeyword);
673
673
  });
674
+
675
+ test('Min: 0', () => {
676
+ const attribute = { min: 0 };
677
+ const modifiers = getAttributeModifiers(attribute);
678
+
679
+ expect(modifiers).toHaveLength(1);
680
+
681
+ expect(modifiers[0].kind).toBe(ts.SyntaxKind.TypeReference);
682
+ expect(modifiers[0].typeName.escapedText).toBe('Attribute.SetMinMax');
683
+
684
+ const [setMinMax] = modifiers;
685
+ const { typeArguments } = setMinMax;
686
+
687
+ expect(typeArguments).toBeDefined();
688
+ expect(typeArguments).toHaveLength(2);
689
+
690
+ const [definition, typeofMinMax] = typeArguments;
691
+
692
+ // Min/Max
693
+ expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
694
+ expect(definition.members).toHaveLength(1);
695
+
696
+ const [min] = definition.members;
697
+
698
+ expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
699
+ expect(min.name.escapedText).toBe('min');
700
+ expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
701
+ expect(min.type.text).toBe('0');
702
+
703
+ // Check for string keyword on the second typeArgument
704
+ expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
705
+ });
674
706
  });
675
707
 
676
708
  describe('MinLength / MaxLength', () => {
@@ -114,21 +114,21 @@ const getAttributeModifiers = (attribute) => {
114
114
  throw new Error('typeof min/max values mismatch');
115
115
  }
116
116
 
117
- const typeofMinMax = (max && typeofMax) ?? (min && typeofMin);
118
117
  let typeKeyword;
119
118
 
120
- // Determines type keyword (string/number) based on min/max options, throws error for invalid types
121
- switch (typeofMinMax) {
122
- case 'string':
123
- typeKeyword = ts.SyntaxKind.StringKeyword;
124
- break;
125
- case 'number':
126
- typeKeyword = ts.SyntaxKind.NumberKeyword;
127
- break;
128
- default:
129
- throw new Error(
130
- `Invalid data type for min/max options. Must be string or number, but found ${typeofMinMax}`
131
- );
119
+ // use 'string'
120
+ if (typeofMin === 'string' || typeofMax === 'string') {
121
+ typeKeyword = ts.SyntaxKind.StringKeyword;
122
+ }
123
+ // use 'number'
124
+ else if (typeofMin === 'number' || typeofMax === 'number') {
125
+ typeKeyword = ts.SyntaxKind.NumberKeyword;
126
+ }
127
+ // invalid type
128
+ else {
129
+ throw new Error(
130
+ `Invalid data type for min/max options. Must be string, number or undefined, but found { min: ${min} (${typeofMin}), max: ${max} (${typeofMax}) }`
131
+ );
132
132
  }
133
133
 
134
134
  modifiers.push(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/typescript-utils",
3
- "version": "4.19.1",
3
+ "version": "4.20.1",
4
4
  "description": "Typescript support for Strapi",
5
5
  "keywords": [
6
6
  "strapi",
@@ -46,5 +46,5 @@
46
46
  "node": ">=18.0.0 <=20.x.x",
47
47
  "npm": ">=6.0.0"
48
48
  },
49
- "gitHead": "34f7e875cdbeee1f75a46ab733b7943eb518f47e"
49
+ "gitHead": "9aa6e94b2fb58dd50a098d8736f995e5982de730"
50
50
  }