@simtlix/simfinity-js 1.9.0 → 1.9.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/README.md +5 -5
- package/package.json +2 -2
- package/src/index.js +2 -2
- package/tests/scalar-naming.test.js +125 -0
- package/tests/validated-scalar.test.js +1 -1
package/README.md
CHANGED
|
@@ -1072,13 +1072,13 @@ const OrderType = new GraphQLObjectType({
|
|
|
1072
1072
|
|
|
1073
1073
|
### Custom Validated Scalar Types
|
|
1074
1074
|
|
|
1075
|
-
Create custom scalar types with built-in validation
|
|
1075
|
+
Create custom scalar types with built-in validation. The generated type names follow the pattern `{name}_{baseScalarTypeName}`:
|
|
1076
1076
|
|
|
1077
1077
|
```javascript
|
|
1078
1078
|
const { GraphQLString, GraphQLInt } = require('graphql');
|
|
1079
1079
|
const { createValidatedScalar } = require('@simtlix/simfinity-js');
|
|
1080
1080
|
|
|
1081
|
-
// Email scalar with validation
|
|
1081
|
+
// Email scalar with validation (generates type name: Email_String)
|
|
1082
1082
|
const EmailScalar = createValidatedScalar(
|
|
1083
1083
|
'Email',
|
|
1084
1084
|
'A valid email address',
|
|
@@ -1091,7 +1091,7 @@ const EmailScalar = createValidatedScalar(
|
|
|
1091
1091
|
}
|
|
1092
1092
|
);
|
|
1093
1093
|
|
|
1094
|
-
// Positive integer scalar
|
|
1094
|
+
// Positive integer scalar (generates type name: PositiveInt_Int)
|
|
1095
1095
|
const PositiveIntScalar = createValidatedScalar(
|
|
1096
1096
|
'PositiveInt',
|
|
1097
1097
|
'A positive integer',
|
|
@@ -1108,8 +1108,8 @@ const UserType = new GraphQLObjectType({
|
|
|
1108
1108
|
name: 'User',
|
|
1109
1109
|
fields: () => ({
|
|
1110
1110
|
id: { type: GraphQLID },
|
|
1111
|
-
email: { type: EmailScalar },
|
|
1112
|
-
age: { type: PositiveIntScalar },
|
|
1111
|
+
email: { type: EmailScalar }, // Type name: Email_String
|
|
1112
|
+
age: { type: PositiveIntScalar }, // Type name: PositiveInt_Int
|
|
1113
1113
|
}),
|
|
1114
1114
|
});
|
|
1115
1115
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simtlix/simfinity-js",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"license": "Apache-2.0",
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "https://github.com/simtlix/simfinity.git"
|
|
21
|
+
"url": "git+https://github.com/simtlix/simfinity.git"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
24
|
"graphql": "^16.11.0",
|
package/src/index.js
CHANGED
|
@@ -202,7 +202,7 @@ function createValidatedScalar(name, description, baseScalarType, validate) {
|
|
|
202
202
|
const baseKind = kindMap[baseScalarType.name] || Kind.STRING;
|
|
203
203
|
|
|
204
204
|
const scalar = new GraphQLScalarType({
|
|
205
|
-
name
|
|
205
|
+
name: `${name}_${baseScalarType.name}`,
|
|
206
206
|
description,
|
|
207
207
|
serialize(value) {
|
|
208
208
|
validate(value);
|
|
@@ -214,7 +214,7 @@ function createValidatedScalar(name, description, baseScalarType, validate) {
|
|
|
214
214
|
},
|
|
215
215
|
parseLiteral(ast, variables) {
|
|
216
216
|
if (ast.kind !== baseKind) {
|
|
217
|
-
throw new Error(`${name} must be a ${baseScalarType.name}`);
|
|
217
|
+
throw new Error(`${name}_${baseScalarType.name} must be a ${baseScalarType.name}`);
|
|
218
218
|
}
|
|
219
219
|
const value = baseScalarType.parseLiteral(ast, variables);
|
|
220
220
|
validate(value);
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
describe, test, expect,
|
|
3
|
+
} from 'vitest';
|
|
4
|
+
import { GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID } from 'graphql';
|
|
5
|
+
import { createValidatedScalar } from '../src/index.js';
|
|
6
|
+
|
|
7
|
+
describe('Validated Scalar Naming Convention', () => {
|
|
8
|
+
test('should generate correct type names with base scalar type suffix', () => {
|
|
9
|
+
const EmailScalar = createValidatedScalar(
|
|
10
|
+
'Email',
|
|
11
|
+
'A valid email address',
|
|
12
|
+
GraphQLString,
|
|
13
|
+
(value) => {
|
|
14
|
+
if (!value.includes('@')) {
|
|
15
|
+
throw new Error('Invalid email format');
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const EpisodeNumberScalar = createValidatedScalar(
|
|
21
|
+
'EpisodeNumber',
|
|
22
|
+
'A valid episode number',
|
|
23
|
+
GraphQLInt,
|
|
24
|
+
(value) => {
|
|
25
|
+
if (value <= 0) {
|
|
26
|
+
throw new Error('Episode number must be positive');
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const RatingScalar = createValidatedScalar(
|
|
32
|
+
'Rating',
|
|
33
|
+
'A valid rating between 0 and 10',
|
|
34
|
+
GraphQLFloat,
|
|
35
|
+
(value) => {
|
|
36
|
+
if (value < 0 || value > 10) {
|
|
37
|
+
throw new Error('Rating must be between 0 and 10');
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const IsActiveScalar = createValidatedScalar(
|
|
43
|
+
'IsActive',
|
|
44
|
+
'A boolean indicating if something is active',
|
|
45
|
+
GraphQLBoolean,
|
|
46
|
+
(value) => {
|
|
47
|
+
// Boolean validation is usually not needed, but this is for testing
|
|
48
|
+
if (typeof value !== 'boolean') {
|
|
49
|
+
throw new Error('Must be a boolean value');
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const CustomIdScalar = createValidatedScalar(
|
|
55
|
+
'CustomId',
|
|
56
|
+
'A custom ID with specific format',
|
|
57
|
+
GraphQLID,
|
|
58
|
+
(value) => {
|
|
59
|
+
if (!value.startsWith('CUST_')) {
|
|
60
|
+
throw new Error('Custom ID must start with CUST_');
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// Test the naming convention
|
|
66
|
+
expect(EmailScalar.name).toBe('Email_String');
|
|
67
|
+
expect(EpisodeNumberScalar.name).toBe('EpisodeNumber_Int');
|
|
68
|
+
expect(RatingScalar.name).toBe('Rating_Float');
|
|
69
|
+
expect(IsActiveScalar.name).toBe('IsActive_Boolean');
|
|
70
|
+
expect(CustomIdScalar.name).toBe('CustomId_ID');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('should maintain baseScalarType property', () => {
|
|
74
|
+
const EmailScalar = createValidatedScalar(
|
|
75
|
+
'Email',
|
|
76
|
+
'A valid email address',
|
|
77
|
+
GraphQLString,
|
|
78
|
+
(value) => {
|
|
79
|
+
if (!value.includes('@')) {
|
|
80
|
+
throw new Error('Invalid email format');
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
expect(EmailScalar.baseScalarType).toBe(GraphQLString);
|
|
86
|
+
expect(EmailScalar.name).toBe('Email_String');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('should work with validation functions', () => {
|
|
90
|
+
const EpisodeNumberScalar = createValidatedScalar(
|
|
91
|
+
'EpisodeNumber',
|
|
92
|
+
'A valid episode number',
|
|
93
|
+
GraphQLInt,
|
|
94
|
+
(value) => {
|
|
95
|
+
if (value <= 0) {
|
|
96
|
+
throw new Error('Episode number must be positive');
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
// Test valid value
|
|
102
|
+
expect(() => EpisodeNumberScalar.serialize(5)).not.toThrow();
|
|
103
|
+
expect(EpisodeNumberScalar.serialize(5)).toBe(5);
|
|
104
|
+
|
|
105
|
+
// Test invalid value
|
|
106
|
+
expect(() => EpisodeNumberScalar.serialize(0)).toThrow('Episode number must be positive');
|
|
107
|
+
expect(() => EpisodeNumberScalar.serialize(-1)).toThrow('Episode number must be positive');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test('should generate error messages with correct type names', () => {
|
|
111
|
+
const EpisodeNumberScalar = createValidatedScalar(
|
|
112
|
+
'EpisodeNumber',
|
|
113
|
+
'A valid episode number',
|
|
114
|
+
GraphQLInt,
|
|
115
|
+
(value) => {
|
|
116
|
+
if (value <= 0) {
|
|
117
|
+
throw new Error('Episode number must be positive');
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
// The error message should include the full type name
|
|
123
|
+
expect(() => EpisodeNumberScalar.serialize(0)).toThrow('Episode number must be positive');
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -70,7 +70,7 @@ describe('Custom Validated Scalar Types', () => {
|
|
|
70
70
|
describe('createValidatedScalar function', () => {
|
|
71
71
|
test('should create a valid scalar type with baseScalarType property', () => {
|
|
72
72
|
expect(EmailScalar).toBeDefined();
|
|
73
|
-
expect(EmailScalar.name).toBe('
|
|
73
|
+
expect(EmailScalar.name).toBe('Email_String');
|
|
74
74
|
expect(EmailScalar.baseScalarType).toBe(GraphQLString);
|
|
75
75
|
expect(EmailScalar.serialize).toBeDefined();
|
|
76
76
|
expect(EmailScalar.parseValue).toBeDefined();
|