@api-client/core 0.12.1 → 0.12.3
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/bin/plugins/sinon/assert.ts +29 -0
- package/bin/test.ts +2 -0
- package/build/src/browser.d.ts +4 -0
- package/build/src/browser.d.ts.map +1 -1
- package/build/src/browser.js +3 -0
- package/build/src/browser.js.map +1 -1
- package/build/src/index.d.ts +4 -0
- package/build/src/index.d.ts.map +1 -1
- package/build/src/index.js +3 -0
- package/build/src/index.js.map +1 -1
- package/build/src/modeling/DataDomain.d.ts +4 -0
- package/build/src/modeling/DataDomain.d.ts.map +1 -1
- package/build/src/modeling/DataDomain.js +11 -0
- package/build/src/modeling/DataDomain.js.map +1 -1
- package/build/src/modeling/DomainAssociation.js +1 -1
- package/build/src/modeling/DomainAssociation.js.map +1 -1
- package/build/src/modeling/DomainEntity.d.ts +46 -0
- package/build/src/modeling/DomainEntity.d.ts.map +1 -1
- package/build/src/modeling/DomainEntity.js +71 -0
- package/build/src/modeling/DomainEntity.js.map +1 -1
- package/build/src/modeling/DomainImpactAnalysis.d.ts +31 -8
- package/build/src/modeling/DomainImpactAnalysis.d.ts.map +1 -1
- package/build/src/modeling/DomainImpactAnalysis.js +118 -46
- package/build/src/modeling/DomainImpactAnalysis.js.map +1 -1
- package/build/src/modeling/DomainProperty.js +1 -1
- package/build/src/modeling/DomainProperty.js.map +1 -1
- package/build/src/modeling/validation/association_validation.d.ts +38 -0
- package/build/src/modeling/validation/association_validation.d.ts.map +1 -0
- package/build/src/modeling/validation/association_validation.js +108 -0
- package/build/src/modeling/validation/association_validation.js.map +1 -0
- package/build/src/modeling/validation/entity_validation.d.ts +52 -0
- package/build/src/modeling/validation/entity_validation.d.ts.map +1 -0
- package/build/src/modeling/validation/entity_validation.js +241 -0
- package/build/src/modeling/validation/entity_validation.js.map +1 -0
- package/build/src/modeling/validation/postgresql.d.ts +2 -0
- package/build/src/modeling/validation/postgresql.d.ts.map +1 -0
- package/build/src/modeling/validation/postgresql.js +58 -0
- package/build/src/modeling/validation/postgresql.js.map +1 -0
- package/build/src/modeling/validation/property_validation.d.ts +29 -0
- package/build/src/modeling/validation/property_validation.d.ts.map +1 -0
- package/build/src/modeling/validation/property_validation.js +58 -0
- package/build/src/modeling/validation/property_validation.js.map +1 -0
- package/build/src/modeling/validation/rules.d.ts +55 -0
- package/build/src/modeling/validation/rules.d.ts.map +1 -0
- package/build/src/modeling/validation/rules.js +110 -0
- package/build/src/modeling/validation/rules.js.map +1 -0
- package/package.json +1 -1
- package/src/modeling/DataDomain.ts +12 -0
- package/src/modeling/DomainAssociation.ts +1 -1
- package/src/modeling/DomainEntity.ts +75 -0
- package/src/modeling/DomainImpactAnalysis.ts +144 -54
- package/src/modeling/DomainProperty.ts +1 -1
- package/src/modeling/validation/association_validation.ts +109 -0
- package/src/modeling/validation/entity_validation.ts +246 -0
- package/src/modeling/validation/postgresql.ts +57 -0
- package/src/modeling/validation/property_validation.ts +58 -0
- package/src/modeling/validation/rules.ts +152 -0
- package/tests/unit/modeling/data_domain_associations.spec.ts +1 -1
- package/tests/unit/modeling/data_domain_property.spec.ts +1 -1
- package/tests/unit/modeling/domain.property.spec.ts +7 -7
- package/tests/unit/modeling/domain_asociation.spec.ts +3 -3
- package/tests/unit/modeling/domain_entity_associations.spec.ts +1 -1
- package/tests/unit/modeling/domain_entity_properties.spec.ts +2 -2
- package/tests/unit/modeling/domain_impact_analysis.spec.ts +138 -29
- package/tests/unit/modeling/validation/association_validation.spec.ts +157 -0
- package/tests/unit/modeling/validation/entity_validation.spec.ts +192 -0
- package/tests/unit/modeling/validation/property_validation.spec.ts +135 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { DomainEntityKind } from '../../models/kinds.js'
|
|
2
|
+
import type { DataDomain } from '../DataDomain.js'
|
|
3
|
+
import type { DomainEntity } from '../DomainEntity.js'
|
|
4
|
+
import { ReservedKeywords } from './postgresql.js'
|
|
5
|
+
import type { DomainValidation } from './rules.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* EntityValidation is a class that performs validation on entities in a data domain.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* - We do not need to check for parent uniqueness here, because in the graph there can only be one edge
|
|
12
|
+
* between two nodes. Parent relationships are described by an edge in the graph.
|
|
13
|
+
*/
|
|
14
|
+
export class EntityValidation {
|
|
15
|
+
constructor(protected domain: DataDomain) {}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Performs all the validation rules on the entity.
|
|
19
|
+
* If you are interested in a specific rule, use the specific method.
|
|
20
|
+
* @param target The target entity to validate. Can be a string with the entity key or a DomainEntity object.
|
|
21
|
+
*/
|
|
22
|
+
validate(target: string | DomainEntity): DomainValidation[] {
|
|
23
|
+
const results: DomainValidation[] = []
|
|
24
|
+
let entity: DomainEntity | undefined
|
|
25
|
+
if (typeof target === 'string') {
|
|
26
|
+
entity = this.domain.findEntity(target)
|
|
27
|
+
} else {
|
|
28
|
+
entity = target
|
|
29
|
+
}
|
|
30
|
+
if (!entity) {
|
|
31
|
+
const message = `The "${target}" entity does not exist.`
|
|
32
|
+
const help = `The entity must be defined in the domain.`
|
|
33
|
+
results.push({
|
|
34
|
+
field: '*',
|
|
35
|
+
rule: 'exists',
|
|
36
|
+
message,
|
|
37
|
+
help,
|
|
38
|
+
key: target as string,
|
|
39
|
+
kind: DomainEntityKind,
|
|
40
|
+
severity: 'error',
|
|
41
|
+
})
|
|
42
|
+
return results
|
|
43
|
+
}
|
|
44
|
+
const primary = this.validatePrimaryKey(entity)
|
|
45
|
+
results.push(...primary)
|
|
46
|
+
const minimum = this.minimumRequiredProperties(entity)
|
|
47
|
+
results.push(...minimum)
|
|
48
|
+
const name = this.validateName(entity)
|
|
49
|
+
results.push(...name)
|
|
50
|
+
const uniqueName = this.uniqueName(entity)
|
|
51
|
+
results.push(...uniqueName)
|
|
52
|
+
return results
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Validates the entity against the primary key validation rules.
|
|
57
|
+
* @param entity The entity to validate
|
|
58
|
+
* @returns The list of validation messages.
|
|
59
|
+
*/
|
|
60
|
+
validatePrimaryKey(entity: DomainEntity): DomainValidation[] {
|
|
61
|
+
const results: DomainValidation[] = []
|
|
62
|
+
const primary = entity.primaryKey()
|
|
63
|
+
if (!primary) {
|
|
64
|
+
const message = `The "${entity.info.getLabel()}" entity has no identifier.`
|
|
65
|
+
const help = `An entity that can exists by itself must have identifier defined.`
|
|
66
|
+
results.push({
|
|
67
|
+
field: 'properties',
|
|
68
|
+
rule: 'primary_key',
|
|
69
|
+
message,
|
|
70
|
+
help,
|
|
71
|
+
severity: 'error',
|
|
72
|
+
key: entity.key,
|
|
73
|
+
kind: entity.kind,
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
return results
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Checks if the entity has the minimum required properties.
|
|
81
|
+
* @param entity The entity to validate
|
|
82
|
+
*/
|
|
83
|
+
minimumRequiredProperties(entity: DomainEntity): DomainValidation[] {
|
|
84
|
+
const results: DomainValidation[] = []
|
|
85
|
+
if (!entity.hasProperties() && !entity.hasAssociations()) {
|
|
86
|
+
const message = `The "${entity.info.getLabel()}" entity has no properties. It will be ignored.`
|
|
87
|
+
const help = `Entities that have no properties are ignored in the data domain. No schema will be generated for it.`
|
|
88
|
+
results.push({
|
|
89
|
+
field: 'properties',
|
|
90
|
+
rule: 'required',
|
|
91
|
+
message,
|
|
92
|
+
help,
|
|
93
|
+
severity: 'warning',
|
|
94
|
+
key: entity.key,
|
|
95
|
+
kind: entity.kind,
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
return results
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Validates the entity name.
|
|
103
|
+
*
|
|
104
|
+
* @remarks
|
|
105
|
+
* - An entity must have a name defined.
|
|
106
|
+
* - The name has to follow the same rules as the names in a PostgreSQL database.
|
|
107
|
+
* - Table names must start with a letter (a-z) or underscore (_).
|
|
108
|
+
* - Subsequent characters can be letters, digits (0-9), or underscores (_).
|
|
109
|
+
* - Names are case-insensitive.
|
|
110
|
+
* - The maximum length of a table name is 31 characters
|
|
111
|
+
* - Should be snake case (it's a convention, not an error).
|
|
112
|
+
* - Should not be a reserved word (for example: "IN", "SELECT", "FROM", etc.).
|
|
113
|
+
* @param entity The entity to validate
|
|
114
|
+
*/
|
|
115
|
+
validateName(entity: DomainEntity): DomainValidation[] {
|
|
116
|
+
const results: DomainValidation[] = []
|
|
117
|
+
const label = entity.info.getLabel()
|
|
118
|
+
if (!entity.info.name) {
|
|
119
|
+
const message = `The "${label}" entity has no name.`
|
|
120
|
+
const help = `An entity must have a name.`
|
|
121
|
+
results.push({
|
|
122
|
+
field: 'name',
|
|
123
|
+
rule: 'required',
|
|
124
|
+
message,
|
|
125
|
+
help,
|
|
126
|
+
severity: 'error',
|
|
127
|
+
key: entity.key,
|
|
128
|
+
kind: entity.kind,
|
|
129
|
+
})
|
|
130
|
+
return results
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const name = entity.info.name
|
|
134
|
+
if (name.length < 2) {
|
|
135
|
+
const message = `The "${label}" entity name is too short.`
|
|
136
|
+
const help = `The name must be at least 2 characters long.`
|
|
137
|
+
results.push({
|
|
138
|
+
field: 'name',
|
|
139
|
+
rule: 'length',
|
|
140
|
+
message,
|
|
141
|
+
help,
|
|
142
|
+
severity: 'error',
|
|
143
|
+
key: entity.key,
|
|
144
|
+
kind: entity.kind,
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
if (name.length > 31) {
|
|
148
|
+
const message = `The "${label}" entity name is too long.`
|
|
149
|
+
const help = `The name must be at most 31 characters long.`
|
|
150
|
+
results.push({
|
|
151
|
+
field: 'name',
|
|
152
|
+
rule: 'length',
|
|
153
|
+
message,
|
|
154
|
+
help,
|
|
155
|
+
severity: 'error',
|
|
156
|
+
key: entity.key,
|
|
157
|
+
kind: entity.kind,
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)) {
|
|
161
|
+
const message = `The "${label}" entity name is invalid.`
|
|
162
|
+
const help = `The name must start with a letter (a-z) or underscore (_). Subsequent characters can be letters, digits (0-9), or underscores (_).`
|
|
163
|
+
results.push({
|
|
164
|
+
field: 'name',
|
|
165
|
+
rule: 'format',
|
|
166
|
+
message,
|
|
167
|
+
help,
|
|
168
|
+
severity: 'error',
|
|
169
|
+
key: entity.key,
|
|
170
|
+
kind: entity.kind,
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
if (/^[A-Z]/.test(name)) {
|
|
174
|
+
const message = `The "${label}" entity name is not in snake case.`
|
|
175
|
+
const help = `The name should be in snake case (lowercase letters and underscores).`
|
|
176
|
+
results.push({
|
|
177
|
+
field: 'name',
|
|
178
|
+
rule: 'snake_case',
|
|
179
|
+
message,
|
|
180
|
+
help,
|
|
181
|
+
severity: 'info',
|
|
182
|
+
key: entity.key,
|
|
183
|
+
kind: entity.kind,
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
if (ReservedKeywords.has(name.toUpperCase())) {
|
|
187
|
+
const message = `The "${label}" entity name is a reserved keyword.`
|
|
188
|
+
const help = `The name should not be a reserved keyword.`
|
|
189
|
+
results.push({
|
|
190
|
+
field: 'name',
|
|
191
|
+
rule: 'reserved',
|
|
192
|
+
message,
|
|
193
|
+
help,
|
|
194
|
+
severity: 'error',
|
|
195
|
+
key: entity.key,
|
|
196
|
+
kind: entity.kind,
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
return results
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Checks if the entity name is unique in the data domain.
|
|
204
|
+
* @param entity The entity to validate
|
|
205
|
+
*/
|
|
206
|
+
uniqueName(entity: DomainEntity): DomainValidation[] {
|
|
207
|
+
const results: DomainValidation[] = []
|
|
208
|
+
const name = entity.info.name?.toLowerCase()
|
|
209
|
+
if (!name) {
|
|
210
|
+
return results
|
|
211
|
+
}
|
|
212
|
+
// We need to check the unique names in all entities, including foreign ones.
|
|
213
|
+
for (const other of this.domain.listEntities()) {
|
|
214
|
+
if (other.info.name?.toLowerCase() === name && other.key !== entity.key) {
|
|
215
|
+
const message = `The "${name}" entity name is already used in the data domain.`
|
|
216
|
+
const help = `The name must be unique. This includes references to other data domains.`
|
|
217
|
+
results.push({
|
|
218
|
+
field: 'name',
|
|
219
|
+
rule: 'unique',
|
|
220
|
+
message,
|
|
221
|
+
help,
|
|
222
|
+
severity: 'error',
|
|
223
|
+
key: other.key,
|
|
224
|
+
kind: other.kind,
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
for (const other of this.domain.listForeignEntities()) {
|
|
229
|
+
if (other.info.name?.toLowerCase() === name && other.key !== entity.key) {
|
|
230
|
+
const message = `The "${name}" entity name is already used in the foreign data domain.`
|
|
231
|
+
const help = `The name must be unique. This includes references to other data domains.`
|
|
232
|
+
results.push({
|
|
233
|
+
field: 'name',
|
|
234
|
+
rule: 'unique',
|
|
235
|
+
message,
|
|
236
|
+
help,
|
|
237
|
+
severity: 'error',
|
|
238
|
+
key: other.key,
|
|
239
|
+
kind: other.kind,
|
|
240
|
+
parent: other.domain.key,
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return results
|
|
245
|
+
}
|
|
246
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export const ReservedKeywords = new Set([
|
|
2
|
+
'IN',
|
|
3
|
+
'EXISTS',
|
|
4
|
+
'SELECT',
|
|
5
|
+
'FROM',
|
|
6
|
+
'WHERE',
|
|
7
|
+
'AND',
|
|
8
|
+
'OR',
|
|
9
|
+
'NOT',
|
|
10
|
+
'NULL',
|
|
11
|
+
'TRUE',
|
|
12
|
+
'FALSE',
|
|
13
|
+
'BETWEEN',
|
|
14
|
+
'LIKE',
|
|
15
|
+
'INTO',
|
|
16
|
+
'JOIN',
|
|
17
|
+
'INNER JOIN',
|
|
18
|
+
'LEFT JOIN',
|
|
19
|
+
'RIGHT JOIN',
|
|
20
|
+
'FULL JOIN',
|
|
21
|
+
'CROSS JOIN',
|
|
22
|
+
'UNION',
|
|
23
|
+
'INTERSECT',
|
|
24
|
+
'EXCEPT',
|
|
25
|
+
'ORDER BY',
|
|
26
|
+
'GROUP BY',
|
|
27
|
+
'HAVING',
|
|
28
|
+
'DISTINCT',
|
|
29
|
+
'LIMIT',
|
|
30
|
+
'OFFSET',
|
|
31
|
+
'INSERT',
|
|
32
|
+
'UPDATE',
|
|
33
|
+
'DELETE',
|
|
34
|
+
'CREATE',
|
|
35
|
+
'ALTER',
|
|
36
|
+
'DROP',
|
|
37
|
+
'TRUNCATE',
|
|
38
|
+
'RENAME',
|
|
39
|
+
'GRANT',
|
|
40
|
+
'REVOKE',
|
|
41
|
+
'COMMIT',
|
|
42
|
+
'ROLLBACK',
|
|
43
|
+
'SAVEPOINT',
|
|
44
|
+
'TRANSACTION',
|
|
45
|
+
'BEGIN',
|
|
46
|
+
'END',
|
|
47
|
+
'CASE',
|
|
48
|
+
'WHEN',
|
|
49
|
+
'THEN',
|
|
50
|
+
'ELSE',
|
|
51
|
+
'END CASE',
|
|
52
|
+
'EXCEPTION',
|
|
53
|
+
'RAISE',
|
|
54
|
+
'RETURN',
|
|
55
|
+
'FUNCTION',
|
|
56
|
+
'PROCEDURE',
|
|
57
|
+
])
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { DomainPropertyKind } from '../../models/kinds.js'
|
|
2
|
+
import type { DataDomain } from '../DataDomain.js'
|
|
3
|
+
import type { DomainProperty } from '../DomainProperty.js'
|
|
4
|
+
import { type DomainValidation, validatePropertyName } from './rules.js'
|
|
5
|
+
|
|
6
|
+
export class PropertyValidation {
|
|
7
|
+
constructor(protected domain: DataDomain) {}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Performs all the validation rules on the property.
|
|
11
|
+
* If you are interested in a specific rule, use the specific method.
|
|
12
|
+
* @param target The target property to validate. Can be a string with
|
|
13
|
+
* the property key or a DomainProperty object.
|
|
14
|
+
*/
|
|
15
|
+
validate(target: string | DomainProperty): DomainValidation[] {
|
|
16
|
+
const results: DomainValidation[] = []
|
|
17
|
+
let property: DomainProperty | undefined
|
|
18
|
+
if (typeof target === 'string') {
|
|
19
|
+
property = this.domain.findProperty(target)
|
|
20
|
+
} else {
|
|
21
|
+
property = target
|
|
22
|
+
}
|
|
23
|
+
if (!property) {
|
|
24
|
+
const message = `The "${target}" property does not exist.`
|
|
25
|
+
const help = `The property must be defined in the domain.`
|
|
26
|
+
results.push({
|
|
27
|
+
field: '*',
|
|
28
|
+
rule: 'exists',
|
|
29
|
+
message,
|
|
30
|
+
help,
|
|
31
|
+
key: target as string,
|
|
32
|
+
kind: DomainPropertyKind,
|
|
33
|
+
severity: 'error',
|
|
34
|
+
})
|
|
35
|
+
return results
|
|
36
|
+
}
|
|
37
|
+
const name = this.validateName(property)
|
|
38
|
+
results.push(...name)
|
|
39
|
+
return results
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Validates the property name.
|
|
44
|
+
*
|
|
45
|
+
* @remarks
|
|
46
|
+
* - A property must have a name defined.
|
|
47
|
+
* - The name has to follow the same rules as the names in a PostgreSQL database.
|
|
48
|
+
* - Column names can only contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
|
|
49
|
+
* - The name must start with a letter (a-z, A-Z) or an underscore (_).
|
|
50
|
+
* - PostgreSQL limits column names to a maximum of 59 characters.
|
|
51
|
+
* - (our rule) Column names are case insensitive.
|
|
52
|
+
* - (recommendation) Column names should be in lower case.
|
|
53
|
+
* @param property The property to validate
|
|
54
|
+
*/
|
|
55
|
+
validateName(property: DomainProperty): DomainValidation[] {
|
|
56
|
+
return validatePropertyName(property)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { ReservedKeywords } from './postgresql.js'
|
|
2
|
+
import type { DomainProperty } from '../DomainProperty.js'
|
|
3
|
+
import type { DomainAssociation } from '../DomainAssociation.js'
|
|
4
|
+
import type { DomainImpactItem } from '../DomainImpactAnalysis.js'
|
|
5
|
+
import type { DomainEntity } from '../DomainEntity.js'
|
|
6
|
+
import { DomainPropertyKind } from '../../models/kinds.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* `DomainImpactItem` mapping:
|
|
10
|
+
* - `impact` -> `message`
|
|
11
|
+
* - `resolution` -> `help`
|
|
12
|
+
* - `type` -> unused
|
|
13
|
+
* - `blocking` -> unused (deprecated)
|
|
14
|
+
* - `relationship` -> unused
|
|
15
|
+
*/
|
|
16
|
+
export interface DomainValidation
|
|
17
|
+
extends Omit<DomainImpactItem, 'type' | 'impact' | 'blocking' | 'relationship' | 'resolution'> {
|
|
18
|
+
/**
|
|
19
|
+
* The field that did not pass validation.
|
|
20
|
+
*/
|
|
21
|
+
field: string
|
|
22
|
+
/**
|
|
23
|
+
* The name of the rule that was violated.
|
|
24
|
+
*/
|
|
25
|
+
rule: string
|
|
26
|
+
/**
|
|
27
|
+
* Optional help message that can be used to provide
|
|
28
|
+
* more information about the error.
|
|
29
|
+
*/
|
|
30
|
+
help?: string
|
|
31
|
+
/**
|
|
32
|
+
* Optional URL that can be used to provide more information
|
|
33
|
+
* about the error.
|
|
34
|
+
*/
|
|
35
|
+
url?: string
|
|
36
|
+
/**
|
|
37
|
+
* The validation error message.
|
|
38
|
+
* This message should be user-friendly and should not
|
|
39
|
+
* contain any technical details.
|
|
40
|
+
* It should be used to display the error to the user.
|
|
41
|
+
* It should be short and concise.
|
|
42
|
+
*/
|
|
43
|
+
message: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Validates the property name. This includes associations.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* - A property must have a name defined.
|
|
51
|
+
* - The name has to follow the same rules as the names in a PostgreSQL database.
|
|
52
|
+
* - Column names can only contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
|
|
53
|
+
* - The name must start with a letter (a-z, A-Z) or an underscore (_).
|
|
54
|
+
* - PostgreSQL limits column names to a maximum of 59 characters.
|
|
55
|
+
* - (our rule) Column names are case insensitive.
|
|
56
|
+
* - (recommendation) Column names should be in lower case.
|
|
57
|
+
*
|
|
58
|
+
* @param property The property to validate
|
|
59
|
+
*/
|
|
60
|
+
export function validatePropertyName(property: DomainProperty | DomainAssociation): DomainValidation[] {
|
|
61
|
+
const results: DomainValidation[] = []
|
|
62
|
+
const label = property.info.getLabel()
|
|
63
|
+
const parentEntity = property.getParentInstance() as DomainEntity
|
|
64
|
+
const type = property.kind === DomainPropertyKind ? 'property' : 'association'
|
|
65
|
+
if (!property.info.name) {
|
|
66
|
+
const message = `The "${label}" ${type} has no name.`
|
|
67
|
+
const help = `The ${type} must have a name.`
|
|
68
|
+
results.push({
|
|
69
|
+
field: 'name',
|
|
70
|
+
rule: 'required',
|
|
71
|
+
message,
|
|
72
|
+
help,
|
|
73
|
+
severity: 'error',
|
|
74
|
+
key: property.key,
|
|
75
|
+
kind: property.kind,
|
|
76
|
+
parent: parentEntity.key,
|
|
77
|
+
})
|
|
78
|
+
return results
|
|
79
|
+
}
|
|
80
|
+
const name = property.info.name
|
|
81
|
+
if (name.length < 2) {
|
|
82
|
+
const message = `The "${label}" ${type} name is too short.`
|
|
83
|
+
const help = `The name must be at least 2 characters long.`
|
|
84
|
+
results.push({
|
|
85
|
+
field: 'name',
|
|
86
|
+
rule: 'length',
|
|
87
|
+
message,
|
|
88
|
+
help,
|
|
89
|
+
severity: 'error',
|
|
90
|
+
key: property.key,
|
|
91
|
+
kind: property.kind,
|
|
92
|
+
parent: parentEntity.key,
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
if (name.length > 59) {
|
|
96
|
+
const message = `The "${label}" ${type} name is too long.`
|
|
97
|
+
const help = `The name must be at most 59 characters long.`
|
|
98
|
+
results.push({
|
|
99
|
+
field: 'name',
|
|
100
|
+
rule: 'length',
|
|
101
|
+
message,
|
|
102
|
+
help,
|
|
103
|
+
severity: 'error',
|
|
104
|
+
key: property.key,
|
|
105
|
+
kind: property.kind,
|
|
106
|
+
parent: parentEntity.key,
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)) {
|
|
110
|
+
const message = `The "${label}" ${type} name is invalid.`
|
|
111
|
+
const help = `The name must start with a letter (a-z) or underscore (_). Subsequent characters can be letters, digits (0-9), or underscores (_).`
|
|
112
|
+
results.push({
|
|
113
|
+
field: 'name',
|
|
114
|
+
rule: 'format',
|
|
115
|
+
message,
|
|
116
|
+
help,
|
|
117
|
+
severity: 'error',
|
|
118
|
+
key: property.key,
|
|
119
|
+
kind: property.kind,
|
|
120
|
+
parent: parentEntity.key,
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
if (/^[A-Z]/.test(name)) {
|
|
124
|
+
const message = `The "${label}" ${type} name is not in snake case.`
|
|
125
|
+
const help = `The name should be in snake case (lowercase letters and underscores).`
|
|
126
|
+
results.push({
|
|
127
|
+
field: 'name',
|
|
128
|
+
rule: 'snake_case',
|
|
129
|
+
message,
|
|
130
|
+
help,
|
|
131
|
+
severity: 'info',
|
|
132
|
+
key: property.key,
|
|
133
|
+
kind: property.kind,
|
|
134
|
+
parent: parentEntity.key,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
if (ReservedKeywords.has(name.toUpperCase())) {
|
|
138
|
+
const message = `The "${label}" ${type} name is a reserved keyword.`
|
|
139
|
+
const help = `The name should not be a reserved keyword.`
|
|
140
|
+
results.push({
|
|
141
|
+
field: 'name',
|
|
142
|
+
rule: 'reserved',
|
|
143
|
+
message,
|
|
144
|
+
help,
|
|
145
|
+
severity: 'error',
|
|
146
|
+
key: property.key,
|
|
147
|
+
kind: property.kind,
|
|
148
|
+
parent: parentEntity.key,
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
return results
|
|
152
|
+
}
|
|
@@ -37,7 +37,7 @@ test.group('DataDomain.addAssociation()', () => {
|
|
|
37
37
|
assert.instanceOf(association, DomainAssociation)
|
|
38
38
|
assert.isTrue(dataDomain.graph.hasNode(association.key))
|
|
39
39
|
assert.typeOf(association.key, 'string')
|
|
40
|
-
assert.equal(association.info.name, '
|
|
40
|
+
assert.equal(association.info.name, 'new_association')
|
|
41
41
|
})
|
|
42
42
|
|
|
43
43
|
test('addAssociation notifies change', async ({ assert }) => {
|
|
@@ -19,7 +19,7 @@ test.group('DataDomain.addProperty()', () => {
|
|
|
19
19
|
const property = dataDomain.addProperty(entity.key)
|
|
20
20
|
assert.instanceOf(property, DomainProperty)
|
|
21
21
|
assert.isTrue(dataDomain.graph.hasNode(property.key))
|
|
22
|
-
assert.equal(property.info.name, '
|
|
22
|
+
assert.equal(property.info.name, 'new_property')
|
|
23
23
|
assert.equal(property.type, 'string')
|
|
24
24
|
})
|
|
25
25
|
|
|
@@ -7,7 +7,7 @@ test.group('DomainProperty.createSchema()', () => {
|
|
|
7
7
|
assert.equal(schema.kind, DomainPropertyKind)
|
|
8
8
|
assert.typeOf(schema.key, 'string')
|
|
9
9
|
assert.isNotEmpty(schema.key)
|
|
10
|
-
assert.deepInclude(schema.info, { name: '
|
|
10
|
+
assert.deepInclude(schema.info, { name: 'new_property' })
|
|
11
11
|
assert.equal(schema.type, 'string')
|
|
12
12
|
assert.isUndefined(schema.multiple)
|
|
13
13
|
assert.isUndefined(schema.required)
|
|
@@ -94,7 +94,7 @@ test.group('DomainProperty.createSchema()', () => {
|
|
|
94
94
|
assert.equal(schema.kind, DomainPropertyKind)
|
|
95
95
|
assert.typeOf(schema.key, 'string')
|
|
96
96
|
assert.isNotEmpty(schema.key)
|
|
97
|
-
assert.deepInclude(schema.info, { name: '
|
|
97
|
+
assert.deepInclude(schema.info, { name: 'new_property' })
|
|
98
98
|
assert.equal(schema.type, 'string')
|
|
99
99
|
assert.isUndefined(schema.multiple)
|
|
100
100
|
assert.isUndefined(schema.required)
|
|
@@ -193,7 +193,7 @@ test.group('DomainProperty.createSchema()', () => {
|
|
|
193
193
|
info: {},
|
|
194
194
|
schema: {},
|
|
195
195
|
})
|
|
196
|
-
assert.deepInclude(schema.info, { name: '
|
|
196
|
+
assert.deepInclude(schema.info, { name: 'new_property' })
|
|
197
197
|
assert.deepEqual(schema.schema, {})
|
|
198
198
|
})
|
|
199
199
|
|
|
@@ -218,7 +218,7 @@ test.group('DomainProperty.createSchema()', () => {
|
|
|
218
218
|
// @ts-expect-error Testing null values
|
|
219
219
|
schema: null,
|
|
220
220
|
})
|
|
221
|
-
assert.deepInclude(schema.info, { name: '
|
|
221
|
+
assert.deepInclude(schema.info, { name: 'new_property' })
|
|
222
222
|
assert.isUndefined(schema.schema)
|
|
223
223
|
})
|
|
224
224
|
})
|
|
@@ -232,7 +232,7 @@ test.group('DomainProperty.constructor()', () => {
|
|
|
232
232
|
assert.typeOf(property.key, 'string')
|
|
233
233
|
assert.isNotEmpty(property.key)
|
|
234
234
|
assert.instanceOf(property.info, Thing)
|
|
235
|
-
assert.deepInclude(property.info.toJSON(), { name: '
|
|
235
|
+
assert.deepInclude(property.info.toJSON(), { name: 'new_property' })
|
|
236
236
|
assert.equal(property.type, 'string')
|
|
237
237
|
assert.isUndefined(property.multiple)
|
|
238
238
|
assert.isUndefined(property.required)
|
|
@@ -311,7 +311,7 @@ test.group('DomainProperty.constructor()', () => {
|
|
|
311
311
|
assert.typeOf(property.key, 'string')
|
|
312
312
|
assert.isNotEmpty(property.key)
|
|
313
313
|
assert.instanceOf(property.info, Thing)
|
|
314
|
-
assert.deepInclude(property.info.toJSON(), { name: '
|
|
314
|
+
assert.deepInclude(property.info.toJSON(), { name: 'new_property' })
|
|
315
315
|
assert.equal(property.type, 'boolean')
|
|
316
316
|
})
|
|
317
317
|
|
|
@@ -394,7 +394,7 @@ test.group('DomainProperty.toJSON()', () => {
|
|
|
394
394
|
assert.equal(json.kind, DomainPropertyKind)
|
|
395
395
|
assert.typeOf(json.key, 'string')
|
|
396
396
|
assert.isNotEmpty(json.key)
|
|
397
|
-
assert.deepInclude(json.info, { name: '
|
|
397
|
+
assert.deepInclude(json.info, { name: 'new_property' })
|
|
398
398
|
assert.equal(json.type, 'string')
|
|
399
399
|
assert.isUndefined(json.multiple)
|
|
400
400
|
assert.isUndefined(json.required)
|
|
@@ -15,7 +15,7 @@ test.group('DomainAssociation.createSchema()', () => {
|
|
|
15
15
|
assert.equal(schema.kind, DomainAssociationKind)
|
|
16
16
|
assert.typeOf(schema.key, 'string')
|
|
17
17
|
assert.isDefined(schema.info)
|
|
18
|
-
assert.deepEqual(schema.info, Thing.fromJSON({ name: '
|
|
18
|
+
assert.deepEqual(schema.info, Thing.fromJSON({ name: 'new_association' }).toJSON())
|
|
19
19
|
assert.isUndefined(schema.schema)
|
|
20
20
|
assert.isUndefined(schema.multiple)
|
|
21
21
|
assert.isUndefined(schema.required)
|
|
@@ -120,7 +120,7 @@ test.group('DomainAssociation.constructor()', () => {
|
|
|
120
120
|
const association = new DomainAssociation(dataDomain, parentKey)
|
|
121
121
|
assert.equal(association.kind, DomainAssociationKind)
|
|
122
122
|
assert.typeOf(association.key, 'string')
|
|
123
|
-
assert.deepEqual(association.info.toJSON(), Thing.fromJSON({ name: '
|
|
123
|
+
assert.deepEqual(association.info.toJSON(), Thing.fromJSON({ name: 'new_association' }).toJSON())
|
|
124
124
|
assert.isUndefined(association.schema)
|
|
125
125
|
assert.isUndefined(association.multiple)
|
|
126
126
|
assert.isUndefined(association.required)
|
|
@@ -248,7 +248,7 @@ test.group('DomainAssociation.toJSON()', () => {
|
|
|
248
248
|
const json = association.toJSON()
|
|
249
249
|
assert.equal(json.kind, DomainAssociationKind)
|
|
250
250
|
assert.typeOf(json.key, 'string')
|
|
251
|
-
assert.deepEqual(json.info, Thing.fromJSON({ name: '
|
|
251
|
+
assert.deepEqual(json.info, Thing.fromJSON({ name: 'new_association' }).toJSON())
|
|
252
252
|
assert.isUndefined(json.schema)
|
|
253
253
|
assert.isUndefined(json.multiple)
|
|
254
254
|
assert.isUndefined(json.required)
|
|
@@ -38,7 +38,7 @@ test.group('DomainEntity.addAssociation()', () => {
|
|
|
38
38
|
const association = entity.addAssociation()
|
|
39
39
|
assert.instanceOf(association, DomainAssociation)
|
|
40
40
|
assert.isTrue(dataDomain.graph.hasNode(association.key))
|
|
41
|
-
assert.equal(association.info.name, '
|
|
41
|
+
assert.equal(association.info.name, 'new_association')
|
|
42
42
|
})
|
|
43
43
|
|
|
44
44
|
test('notifies change', async ({ assert }) => {
|
|
@@ -10,7 +10,7 @@ test.group('DomainEntity.addProperty()', () => {
|
|
|
10
10
|
assert.instanceOf(property, DomainProperty)
|
|
11
11
|
assert.equal(property.kind, DomainPropertyKind)
|
|
12
12
|
assert.equal(property.key, 'test-property')
|
|
13
|
-
assert.deepInclude(property.info, { name: '
|
|
13
|
+
assert.deepInclude(property.info, { name: 'new_property' })
|
|
14
14
|
assert.isTrue(dataDomain.graph.hasNode(property.key))
|
|
15
15
|
})
|
|
16
16
|
|
|
@@ -23,7 +23,7 @@ test.group('DomainEntity.addProperty()', () => {
|
|
|
23
23
|
assert.equal(property.kind, DomainPropertyKind)
|
|
24
24
|
assert.typeOf(property.key, 'string')
|
|
25
25
|
assert.isNotEmpty(property.key)
|
|
26
|
-
assert.deepInclude(property.info, { name: '
|
|
26
|
+
assert.deepInclude(property.info, { name: 'new_property' })
|
|
27
27
|
assert.isTrue(dataDomain.graph.hasNode(property.key))
|
|
28
28
|
})
|
|
29
29
|
|