19
19
20
20
import { Dictionary , DictionaryDocument } from '../models/Dictionary' ;
21
21
import { ConflictError , BadRequestError , NotFoundError } from '../utils/errors' ;
22
- import { validate } from '../services/schemaService' ;
22
+ import { normalizeSchema , validate } from '../services/schemaService' ;
23
23
import { incrementMajor , incrementMinor , isValidVersion , isGreater } from '../utils/version' ;
24
24
import logger from '../config/logger' ;
25
25
@@ -103,7 +103,9 @@ export const create = async (newDict: {
103
103
104
104
if ( ! isGreater ( newDict . version , latest ) ) {
105
105
logger . warn (
106
- `Rejected ${ newDict . name } due to version ${ newDict . version } being lower than latest: ${ latest } ` ,
106
+ `Rejected ${ newDict . name } due to version ${
107
+ newDict . version
108
+ } being lower than latest: ${ latest } `,
107
109
) ;
108
110
throw new BadRequestError (
109
111
`New version for ${ newDict . name } is not greater than latest existing version` ,
@@ -116,11 +118,13 @@ export const create = async (newDict: {
116
118
if ( ! result . valid ) throw new BadRequestError ( JSON . stringify ( result . errors ) ) ;
117
119
} ) ;
118
120
121
+ const normalizedSchemas = newDict . schemas . map ( schema => normalizeSchema ( schema ) ) ;
122
+
119
123
// Save new dictionary version
120
124
const dict = new Dictionary ( {
121
125
name : newDict . name ,
122
126
version : newDict . version ,
123
- schemas : newDict . schemas ,
127
+ schemas : normalizedSchemas ,
124
128
references : newDict . references || { } ,
125
129
} ) ;
126
130
const saved = await dict . save ( ) ;
@@ -149,8 +153,10 @@ export const addSchema = async (id: string, schema: any): Promise<DictionaryDocu
149
153
const entities = doc . schemas . map ( s => s [ 'name' ] ) ;
150
154
if ( entities . includes ( schema [ 'name' ] ) ) throw new ConflictError ( 'This schema already exists.' ) ;
151
155
156
+ const normalizedSchema = normalizeSchema ( schema ) ;
157
+
152
158
const schemas = doc . schemas ;
153
- schemas . push ( schema ) ;
159
+ schemas . push ( normalizedSchema ) ;
154
160
// Save new dictionary version
155
161
const dict = new Dictionary ( {
156
162
name : doc . name ,
@@ -193,7 +199,10 @@ export const updateSchema = async (
193
199
194
200
// Filter out one to update
195
201
const schemas = doc . schemas . filter ( s => ! ( s [ 'name' ] === schema [ 'name' ] ) ) ;
196
- schemas . push ( schema ) ;
202
+
203
+ const normalizedSchema = normalizeSchema ( schema ) ;
204
+
205
+ schemas . push ( normalizedSchema ) ;
197
206
198
207
// Increment Version
199
208
const nextVersion = major ? incrementMajor ( doc . version ) : incrementMinor ( doc . version ) ;
0 commit comments