• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mia-platform / crud-service / 6650818505

26 Oct 2023 07:24AM UTC coverage: 96.95% (+2.6%) from 94.301%
6650818505

push

github

web-flow
fix: review of ci to update to NodeJS v20 (#211)

* ci: update setup-node action to v4

* ci: separate tests of previous MongoDB versions from v7

* ci: update setup-node action to v4

* ci: set a fixed version for coverall github action

* ci: fix coverallapps version

1645 of 1787 branches covered (0.0%)

Branch coverage included in aggregate %.

8494 of 8671 relevant lines covered (97.96%)

6911.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.35
/lib/AdditionalCaster.utils.js
1
/*
94✔
2
 * Copyright 2023 Mia s.r.l.
94✔
3
 *
94✔
4
 * Licensed under the Apache License, Version 2.0 (the "License");
94✔
5
 * you may not use this file except in compliance with the License.
94✔
6
 * You may obtain a copy of the License at
94✔
7
 *
94✔
8
 *     http://www.apache.org/licenses/LICENSE-2.0
94✔
9
 *
94✔
10
 * Unless required by applicable law or agreed to in writing, software
94✔
11
 * distributed under the License is distributed on an "AS IS" BASIS,
94✔
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
94✔
13
 * See the License for the specific language governing permissions and
94✔
14
 * limitations under the License.
94✔
15
 */
94✔
16

94✔
17
'use strict'
94✔
18

94✔
19

94✔
20
const miaConfigurationKeys = [
94✔
21
  'sensitivityDescription',
94✔
22
  'sensitivityValue',
94✔
23
  'castFunction',
94✔
24
  'primaryKey',
94✔
25
  'custom',
94✔
26
]
94✔
27

94✔
28
const oneToOnePropertyKeys = [
94✔
29
  'description',
94✔
30
  'nullable',
94✔
31
]
94✔
32

94✔
33
const { mandatoryFields } = require('./schemaGetters')
94✔
34
const { STATES } = require('./consts')
94✔
35
const { getIdType } = require('./mongo/mongo-plugin')
94✔
36

94✔
37
function startingSchema(idType) {
3,650✔
38
  return {
3,650✔
39
    _id: idType === 'ObjectId' ? {
3,650✔
40
      type: 'string',
3,044✔
41
      pattern: '^[a-fA-F0-9]{24}$',
3,044✔
42
      __mia_configuration: {
3,044✔
43
        type: 'ObjectId',
3,044✔
44
      },
3,044✔
45
      description: 'Hexadecimal identifier of the document in the collection',
3,044✔
46
    } : {
3,650✔
47
      _id: {
606✔
48
        type: 'string',
606✔
49
        pattern: '^(?!\\s*$).+',
606✔
50
        description: 'String identifier of the document in the collection',
606✔
51
        example: '00000000-0000-4000-0000-000000000000',
606✔
52
      },
606✔
53
    },
3,650✔
54
    __STATE__: {
3,650✔
55
      type: 'string',
3,650✔
56
      enum: Object.values(STATES),
3,650✔
57
      description: 'The state of the document',
3,650✔
58
    },
3,650✔
59
    creatorId: {
3,650✔
60
      type: 'string',
3,650✔
61
      description: 'User id that has created this object',
3,650✔
62
    },
3,650✔
63
    createdAt: {
3,650✔
64
      type: 'string',
3,650✔
65
      format: 'date-time',
3,650✔
66
      description: 'Date of the request that has performed the object creation',
3,650✔
67
    },
3,650✔
68
    updaterId: {
3,650✔
69
      type: 'string',
3,650✔
70
      description: 'User id that has requested the last change successfully',
3,650✔
71
    },
3,650✔
72
    updatedAt: {
3,650✔
73
      type: 'string',
3,650✔
74
      format: 'date-time',
3,650✔
75
      description: 'Date of the request that has performed the last change',
3,650✔
76
    },
3,650✔
77
  }
3,650✔
78
}
3,650✔
79

94✔
80
function fieldsToSchema(fields) {
3,650✔
81
  const idType = getIdType({ fields })
3,650✔
82
  const schema = {
3,650✔
83
    type: 'object',
3,650✔
84
    required: fields.filter(field => field.required).map(field => field.name),
3,650✔
85
    properties: fields.reduce((acc, field) => {
3,650✔
86
      if (mandatoryFields.has(field.name)) { return acc }
39,580✔
87
      const jsonSchemaType = getJsonSchemaTypeFromCustomType(field.type)
17,680✔
88
      return {
17,680✔
89
        ...acc,
17,680✔
90
        [field.name]: {
17,680✔
91
          type: jsonSchemaType,
17,680✔
92
          ...getOneToOneProperties(field),
17,680✔
93
          ...getMiaConfiguration(field),
17,680✔
94
          ...(field.type === 'Date' ? { format: 'date-time' } : {}),
39,580✔
95
          ...(jsonSchemaType === 'object' ? getObjectSchema(field) : {}),
39,580✔
96
          ...(jsonSchemaType === 'array' ? getArraySchema(field) : {}),
39,580✔
97
          ...(field.pattern ? { pattern: field.pattern } : {}),
39,580!
98
          ...(field.enum ? { enum: field.enum } : {}),
39,580!
99
          ...(field.description ? { description: field.description } : {}),
39,580✔
100
        },
39,580✔
101
      }
39,580✔
102
    }, startingSchema(idType)),
3,650✔
103
  }
3,650✔
104

3,650✔
105
  return schema
3,650✔
106
}
3,650✔
107

94✔
108
function getOneToOneProperties(field) {
17,680✔
109
  return Object.fromEntries(
17,680✔
110
    Object.entries(field).filter(([key]) =>
17,680✔
111
      oneToOnePropertyKeys.includes(key)
77,254✔
112
    )
17,680✔
113
  )
17,680✔
114
}
17,680✔
115

94✔
116
function getMiaConfiguration(field) {
21,760✔
117
  const miaConfigurationPropertiesInField = miaConfigurationKeys.reduce((acc, keyword) => {
21,760✔
118
    return keyword in field ? {
108,800!
119
      ...acc,
×
120
      [keyword]: field[keyword],
×
121
    } : acc
108,800✔
122
  }, {})
21,760✔
123

21,760✔
124
  const customType = doesTypeNeedToBeInMiaConfiguration(field.type) ? {
21,760✔
125
    type: field.type,
2,648✔
126
  } : {}
21,760✔
127

21,760✔
128
  const miaConfiguration = {
21,760✔
129
    ...miaConfigurationPropertiesInField,
21,760✔
130
    ...customType,
21,760✔
131
  }
21,760✔
132

21,760✔
133
  return Object.keys(miaConfiguration).length > 0 ? {
21,760✔
134
    __mia_configuration: miaConfiguration,
2,648✔
135
  } : {}
21,760✔
136
}
21,760✔
137

94✔
138
function getArraySchema(field) {
3,466✔
139
  const type = getJsonSchemaTypeFromCustomType(field.items.type)
3,466✔
140

3,466✔
141
  if (type === 'object') {
3,466✔
142
    return {
1,426✔
143
      items: {
1,426✔
144
        ...getObjectSchema(field.items, true),
1,426✔
145
      },
1,426✔
146
    }
1,426✔
147
  }
1,426✔
148

2,040✔
149
  return {
2,040✔
150
    items: {
2,040✔
151
      type,
2,040✔
152
    },
2,040✔
153
  }
2,040✔
154
}
3,466✔
155

94✔
156
function getObjectSchema(field, isArray) {
4,080✔
157
  if ('schema' in field && field.schema) {
4,080✔
158
    return {
1,840✔
159
      ...field.schema,
1,840✔
160
      ...getMiaConfiguration(field),
1,840✔
161
      type: 'object',
1,840✔
162
    }
1,840✔
163
  }
1,840✔
164

2,240✔
165
  return {
2,240✔
166
    ...getMiaConfiguration(field),
2,240✔
167
    type: 'object',
2,240✔
168
    ...isArray ? { additionalProperties: true } : {},
4,080✔
169
  }
4,080✔
170
}
4,080✔
171

94✔
172

94✔
173
function getJsonSchemaTypeFromCustomType(customType) {
21,146✔
174
  switch (customType) {
21,146✔
175
  case 'RawObject':
21,146✔
176
  case 'GeoPoint':
21,146✔
177
    return 'object'
4,080✔
178
  case 'ObjectId':
21,146✔
179
    return 'string'
1,222✔
180
  case 'string':
21,146✔
181
  case 'Date':
21,146✔
182
    return 'string'
8,724✔
183
  case 'number':
21,146✔
184
    return 'number'
2,840✔
185
  case 'boolean':
21,146✔
186
    return 'boolean'
814✔
187
  case 'Array':
21,146✔
188
    return 'array'
3,466✔
189

21,146✔
190
  default:
21,146!
191
    throw new Error('Unrecognized field type')
×
192
  }
21,146✔
193
}
21,146✔
194

94✔
195
function doesTypeNeedToBeInMiaConfiguration(customType) {
21,760✔
196
  switch (customType) {
21,760✔
197
  case 'GeoPoint':
21,760✔
198
  case 'ObjectId':
21,760✔
199
    return true
2,648✔
200

21,760✔
201
  default:
21,760✔
202
    return false
19,112✔
203
  }
21,760✔
204
}
21,760✔
205

94✔
206
module.exports = {
94✔
207
  fieldsToSchema,
94✔
208
}
94✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc