-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDatasetResourceValidator.test.ts
More file actions
238 lines (218 loc) · 9.5 KB
/
DatasetResourceValidator.test.ts
File metadata and controls
238 lines (218 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { DatasetResourceValidator } from '../../../src/datasets/domain/useCases/validators/DatasetResourceValidator'
import {
createDatasetDTO,
createDatasetMetadataBlockModel,
createDatasetDTOWithoutFirstLevelRequiredField
} from '../../testHelpers/datasets/datasetHelper'
import { FieldValidationError } from '../../../src/datasets/domain/useCases/validators/errors/FieldValidationError'
import {
DatasetDTO,
DatasetMetadataChildFieldValueDTO
} from '../../../src/datasets/domain/dtos/DatasetDTO'
import { SingleMetadataFieldValidator } from '../../../src/datasets/domain/useCases/validators/SingleMetadataFieldValidator'
import { MetadataFieldValidator } from '../../../src/datasets/domain/useCases/validators/MetadataFieldValidator'
import { MultipleMetadataFieldValidator } from '../../../src/datasets/domain/useCases/validators/MultipleMetadataFieldValidator'
describe('validate', () => {
const testMetadataBlocks = [createDatasetMetadataBlockModel()]
const singleMetadataFieldValidator = new SingleMetadataFieldValidator()
const metadataFieldValidator = new MetadataFieldValidator(
new SingleMetadataFieldValidator(),
new MultipleMetadataFieldValidator(singleMetadataFieldValidator)
)
const sut = new DatasetResourceValidator(metadataFieldValidator)
const runValidateExpectingFieldValidationError = (
dataset: DatasetDTO,
expectedMetadataFieldName: string,
expectedErrorMessage: string,
expectedParentMetadataFieldName?: string,
expectedPosition?: number
) => {
try {
sut.validate(dataset, testMetadataBlocks)
throw new Error('Validation should fail')
} catch (error) {
expect(error).toBeInstanceOf(FieldValidationError)
expect((error as FieldValidationError)?.citationBlockName).toEqual('citation')
expect((error as FieldValidationError)?.metadataFieldName).toEqual(expectedMetadataFieldName)
expect((error as FieldValidationError)?.parentMetadataFieldName).toEqual(
expectedParentMetadataFieldName
)
expect((error as FieldValidationError)?.fieldPosition).toEqual(expectedPosition)
expect((error as FieldValidationError)?.message).toEqual(expectedErrorMessage)
}
}
test('should not raise a validation error when a new dataset with only the required fields is valid', () => {
const testDataset = createDatasetDTO()
expect(() => sut.validate(testDataset, testMetadataBlocks)).not.toThrow()
})
test('should raise an empty field error when a first level required string field is missing', () => {
expect.assertions(6)
runValidateExpectingFieldValidationError(
createDatasetDTOWithoutFirstLevelRequiredField(),
'author',
'There was an error when validating the field author from metadata block citation. Reason was: The field should not be empty.'
)
})
test('should raise an empty field error when a first level required array field is empty', () => {
expect.assertions(6)
const testDataset = createDatasetDTO(undefined, [], undefined)
runValidateExpectingFieldValidationError(
testDataset,
'author',
'There was an error when validating the field author from metadata block citation. Reason was: The field should not be empty.'
)
})
test('should raise an error when the provided field value for a unique field is an array', () => {
expect.assertions(6)
const testDataset = createDatasetDTO(['title1', 'title2'], undefined, undefined)
runValidateExpectingFieldValidationError(
testDataset,
'title',
'There was an error when validating the field title from metadata block citation. Reason was: Expecting a single field, not an array.'
)
})
test('should raise an error when the provided field value is an object and the field expects a string', () => {
const testDataset = createDatasetDTO(
{ invalidChildField1: 'invalid value 1', invalidChildField2: 'invalid value 2' },
undefined,
undefined
)
expect.assertions(6)
runValidateExpectingFieldValidationError(
testDataset,
'title',
'There was an error when validating the field title from metadata block citation. Reason was: Expecting a string, not child fields.'
)
})
test('should raise an error when the provided field value for a multiple field is a string', () => {
const testDataset = createDatasetDTO(undefined, 'invalidValue', undefined)
expect.assertions(6)
runValidateExpectingFieldValidationError(
testDataset,
'author',
'There was an error when validating the field author from metadata block citation. Reason was: Expecting an array of values.'
)
})
test('should raise an error when the provided field value is an array of strings and the field expects an array of objects', () => {
const invalidAuthorFieldValue = ['invalidValue1', 'invalidValue2']
const testDataset = createDatasetDTO(undefined, invalidAuthorFieldValue, undefined)
expect.assertions(6)
runValidateExpectingFieldValidationError(
testDataset,
'author',
'There was an error when validating the field author from metadata block citation. Reason was: Expecting an array of child fields, not strings.'
)
})
test('should raise an error when the provided field value is an array of objects and the field expects an array of strings', () => {
const invalidAlternativeTitleFieldValue = [
{ invalidChildField1: 'invalid value 1', invalidChildField2: 'invalid value 2' },
{ invalidChildField1: 'invalid value 1', invalidChildField2: 'invalid value 2' }
]
const testDataset = createDatasetDTO(undefined, undefined, invalidAlternativeTitleFieldValue)
expect.assertions(6)
runValidateExpectingFieldValidationError(
testDataset,
'alternativeRequiredTitle',
'There was an error when validating the field alternativeRequiredTitle from metadata block citation. Reason was: Expecting an array of strings, not child fields.'
)
})
test('should raise an empty field error when a required child field is missing', () => {
const invalidAuthorFieldValue: DatasetMetadataChildFieldValueDTO[] = [
{ authorName: 'Admin, Dataverse', authorAffiliation: 'Dataverse.org' },
{ authorAffiliation: 'Dataverse.org' }
]
const testDataset = createDatasetDTO(undefined, invalidAuthorFieldValue, undefined)
expect.assertions(6)
runValidateExpectingFieldValidationError(
testDataset,
'authorName',
'There was an error when validating the field authorName from metadata block citation with parent field author in position 1. Reason was: The field should not be empty.',
'author',
1
)
})
test('should not raise an empty field error when a not required child field is missing', () => {
const authorFieldValue: DatasetMetadataChildFieldValueDTO[] = [
{ authorName: 'Admin, Dataverse', authorAffiliation: 'Dataverse.org' },
{ authorName: 'John, Doe' }
]
const testDataset = createDatasetDTO(undefined, authorFieldValue, undefined)
expect(() => sut.validate(testDataset, testMetadataBlocks)).not.toThrow()
})
test('should raise a controlled vocabulary error when a controlled vocabulary field has an invalid format', () => {
const testDataset = createDatasetDTO(
undefined,
undefined,
undefined,
undefined,
'Wrong Value',
undefined,
undefined,
'Some Value'
)
expect.assertions(6)
runValidateExpectingFieldValidationError(
testDataset,
'contributorType',
'There was an error when validating the field contributorType from metadata block citation with parent field contributor in position 0. Reason was: The field does not have a valid controlled vocabulary value.',
'contributor',
0
)
})
test('should not raise a controlled vocabulary error when the value for a controlled vocabulary field is correct', () => {
const testDataset = createDatasetDTO(
undefined,
undefined,
undefined,
undefined,
'Project Member'
)
expect(() => sut.validate(testDataset, testMetadataBlocks)).not.toThrow()
})
test("should not raise an error when there is a compound field not required with a required childfield that has no value but siblings don't have value either", () => {
const testDataset = createDatasetDTO(
undefined,
undefined,
undefined,
undefined,
'',
undefined,
undefined,
''
)
expect(() => sut.validate(testDataset, testMetadataBlocks)).not.toThrow()
})
test('should not raise an error when there is a compound field not required with a required childfield that has value and some of the siblings has value', () => {
const testDataset = createDatasetDTO(
undefined,
undefined,
undefined,
undefined,
'Project Member',
undefined,
undefined,
'Foo'
)
expect(() => sut.validate(testDataset, testMetadataBlocks)).not.toThrow()
})
test('should raise an error when there is a compound field not required with a required childfield that has no value but some of the siblings have', () => {
const testDataset = createDatasetDTO(
undefined,
undefined,
undefined,
undefined,
'Project Member',
undefined,
undefined,
''
)
expect.assertions(6)
runValidateExpectingFieldValidationError(
testDataset,
'contributorName',
'There was an error when validating the field contributorName from metadata block citation with parent field contributor in position 0. Reason was: The field should not be empty.',
'contributor',
0
)
})
})