@api-client/core 0.12.1 → 0.12.2
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/data/models/example-generator-api.json +6 -6
- 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 +140 -0
- package/tests/unit/modeling/validation/entity_validation.spec.ts +192 -0
- package/tests/unit/modeling/validation/property_validation.spec.ts +125 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { DomainAssociationKind } from '../../models/kinds.js'
|
|
2
|
+
import type { DataDomain } from '../DataDomain.js'
|
|
3
|
+
import type { DomainAssociation } from '../DomainAssociation.js'
|
|
4
|
+
import type { DomainEntity } from '../DomainEntity.js'
|
|
5
|
+
import { type DomainValidation, validatePropertyName } from './rules.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* AssociationValidation is a class that performs validation on associations in a data domain.
|
|
9
|
+
* Note that an association in most cases is a property of an entity.
|
|
10
|
+
*/
|
|
11
|
+
export class AssociationValidation {
|
|
12
|
+
constructor(protected domain: DataDomain) {}
|
|
13
|
+
/**
|
|
14
|
+
* Performs all the validation rules on the association.
|
|
15
|
+
* If you are interested in a specific rule, use the specific method.
|
|
16
|
+
* @param target The target association to validate. Can be a string with
|
|
17
|
+
* the association key or a DomainAssociation object.
|
|
18
|
+
*/
|
|
19
|
+
validate(target: string | DomainAssociation): DomainValidation[] {
|
|
20
|
+
const results: DomainValidation[] = []
|
|
21
|
+
let association: DomainAssociation | undefined
|
|
22
|
+
if (typeof target === 'string') {
|
|
23
|
+
association = this.domain.findAssociation(target)
|
|
24
|
+
} else {
|
|
25
|
+
association = target
|
|
26
|
+
}
|
|
27
|
+
if (!association) {
|
|
28
|
+
const message = `The "${target}" association does not exist.`
|
|
29
|
+
const help = `The association must be defined in the domain.`
|
|
30
|
+
results.push({
|
|
31
|
+
field: '*',
|
|
32
|
+
rule: 'exists',
|
|
33
|
+
message,
|
|
34
|
+
help,
|
|
35
|
+
key: target as string,
|
|
36
|
+
kind: DomainAssociationKind,
|
|
37
|
+
severity: 'error',
|
|
38
|
+
})
|
|
39
|
+
return results
|
|
40
|
+
}
|
|
41
|
+
const name = this.validateName(association)
|
|
42
|
+
results.push(...name)
|
|
43
|
+
const targets = this.validateTargets(association)
|
|
44
|
+
results.push(...targets)
|
|
45
|
+
return results
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Validates the association name.
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* - A association must have a name defined.
|
|
53
|
+
* - The name has to follow the same rules as the names in a PostgreSQL database.
|
|
54
|
+
* - Column names can only contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
|
|
55
|
+
* - The name must start with a letter (a-z, A-Z) or an underscore (_).
|
|
56
|
+
* - PostgreSQL limits column names to a maximum of 59 characters.
|
|
57
|
+
* - (our rule) Column names are case insensitive.
|
|
58
|
+
* - (recommendation) Column names should be in lower case.
|
|
59
|
+
* @param association The association to validate
|
|
60
|
+
*/
|
|
61
|
+
validateName(association: DomainAssociation): DomainValidation[] {
|
|
62
|
+
return validatePropertyName(association)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Validates the association targets.
|
|
67
|
+
* @param association The association to validate
|
|
68
|
+
*/
|
|
69
|
+
validateTargets(association: DomainAssociation): DomainValidation[] {
|
|
70
|
+
const results: DomainValidation[] = []
|
|
71
|
+
const label = association.info.getLabel()
|
|
72
|
+
const parentEntity = association.getParentInstance() as DomainEntity
|
|
73
|
+
if (!association.targets.length) {
|
|
74
|
+
const message = `The "${label}" association has no target.`
|
|
75
|
+
const help = `An association must have at least one target.`
|
|
76
|
+
results.push({
|
|
77
|
+
field: 'targets',
|
|
78
|
+
rule: 'required',
|
|
79
|
+
message,
|
|
80
|
+
help,
|
|
81
|
+
severity: 'error',
|
|
82
|
+
key: association.key,
|
|
83
|
+
kind: association.kind,
|
|
84
|
+
parent: parentEntity.key,
|
|
85
|
+
})
|
|
86
|
+
return results
|
|
87
|
+
}
|
|
88
|
+
for (const target of association.targets) {
|
|
89
|
+
const entity = target.domain
|
|
90
|
+
? this.domain.findForeignEntity(target.key, target.domain)
|
|
91
|
+
: this.domain.findEntity(target.key)
|
|
92
|
+
if (!entity) {
|
|
93
|
+
const message = `The "${label}" association has an invalid target "${target.key}".`
|
|
94
|
+
const help = `The target must be defined in the domain.`
|
|
95
|
+
results.push({
|
|
96
|
+
field: 'targets',
|
|
97
|
+
rule: 'exists',
|
|
98
|
+
message,
|
|
99
|
+
help,
|
|
100
|
+
severity: 'error',
|
|
101
|
+
key: association.key,
|
|
102
|
+
kind: association.kind,
|
|
103
|
+
parent: parentEntity.key,
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return results
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -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 < 3) {
|
|
135
|
+
const message = `The "${label}" entity name is too short.`
|
|
136
|
+
const help = `The name must be at least 3 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 < 3) {
|
|
82
|
+
const message = `The "${label}" ${type} name is too short.`
|
|
83
|
+
const help = `The name must be at least 3 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
|
|