@laboratoria/sdk-js 3.3.0 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/core.js +8 -2
- package/lib/model.js +79 -35
- package/package.json +5 -5
package/lib/core.js
CHANGED
@@ -39,6 +39,9 @@ const extended = {
|
|
39
39
|
email: {
|
40
40
|
inputType: 'email',
|
41
41
|
},
|
42
|
+
mobileNumber: {
|
43
|
+
inputType: 'phone',
|
44
|
+
},
|
42
45
|
bio: {
|
43
46
|
inputType: 'textarea',
|
44
47
|
},
|
@@ -190,9 +193,12 @@ const extended = {
|
|
190
193
|
'workIncome',
|
191
194
|
'isReady',
|
192
195
|
'bypassFilters',
|
193
|
-
// 'isReturningApplicant',
|
194
|
-
// 'legacyAdmissionCohortSlug',
|
195
196
|
],
|
197
|
+
properties: {
|
198
|
+
locality: {
|
199
|
+
required: true,
|
200
|
+
},
|
201
|
+
},
|
196
202
|
},
|
197
203
|
IscedLevel: {
|
198
204
|
parse: (props) => {
|
package/lib/model.js
CHANGED
@@ -39,43 +39,38 @@ const createValidator = (schema) => {
|
|
39
39
|
|
40
40
|
const validateAttr = (key, value) => {
|
41
41
|
const attrSchema = properties[key] || {};
|
42
|
-
const type = (
|
43
|
-
Array.isArray(attrSchema.type)
|
44
|
-
? attrSchema.type[0]
|
45
|
-
: attrSchema.type
|
46
|
-
);
|
47
|
-
const isRequired = (
|
48
|
-
!Array.isArray(attrSchema.type)
|
49
|
-
|| !attrSchema.type.includes('null')
|
50
|
-
);
|
51
42
|
|
52
43
|
if (attrSchema.enum) {
|
53
|
-
if (!
|
44
|
+
if (!attrSchema.required && !value) {
|
54
45
|
return;
|
55
46
|
}
|
56
47
|
if (!attrSchema.enum.includes(value)) {
|
57
|
-
return
|
48
|
+
return {
|
49
|
+
id: 'enum-validation-error',
|
50
|
+
values: { enum: attrSchema.enum, value },
|
51
|
+
};
|
58
52
|
}
|
59
53
|
}
|
60
54
|
|
61
|
-
switch (type) {
|
62
|
-
case 'string':
|
63
|
-
if (isRequired && !value) {
|
64
|
-
return 'Field is required';
|
65
|
-
}
|
66
|
-
break;
|
55
|
+
switch (attrSchema.$type) {
|
67
56
|
case 'integer':
|
68
57
|
const parsedValue = parseInt(value, 10);
|
69
|
-
if (
|
70
|
-
return '
|
58
|
+
if (attrSchema.required && Number.isNaN(parsedValue)) {
|
59
|
+
return { id: 'invalid-number-validation-error' };
|
71
60
|
}
|
72
61
|
break;
|
73
62
|
case 'date':
|
74
63
|
break;
|
75
64
|
case 'boolean':
|
76
65
|
break;
|
66
|
+
case 'json':
|
67
|
+
case 'string':
|
68
|
+
case 'ref':
|
77
69
|
default:
|
78
|
-
|
70
|
+
if (attrSchema.required && !value) {
|
71
|
+
return { id: 'required-validation-error' };
|
72
|
+
}
|
73
|
+
break;
|
79
74
|
}
|
80
75
|
};
|
81
76
|
|
@@ -138,6 +133,54 @@ const serializeData = (data, schema) => {
|
|
138
133
|
};
|
139
134
|
|
140
135
|
|
136
|
+
const parseSchema = ({ primaryKey, properties, ...rest }) => {
|
137
|
+
return {
|
138
|
+
primaryKey: primaryKey || 'id',
|
139
|
+
properties: Object.keys(properties || {}).reduce(
|
140
|
+
(memo, key) => {
|
141
|
+
const propSchema = properties[key] || {};
|
142
|
+
const $type = (
|
143
|
+
!propSchema.type
|
144
|
+
? propSchema.$ref
|
145
|
+
? 'ref'
|
146
|
+
: undefined
|
147
|
+
: !Array.isArray(propSchema.type)
|
148
|
+
? propSchema.type
|
149
|
+
: propSchema.type.length < 3
|
150
|
+
? propSchema.type[0]
|
151
|
+
: 'json'
|
152
|
+
);
|
153
|
+
const inputType = propSchema.inputType || (
|
154
|
+
$type === 'integer'
|
155
|
+
? 'number'
|
156
|
+
: $type === 'string' && propSchema.format === 'date-time'
|
157
|
+
? 'date'
|
158
|
+
: $type
|
159
|
+
);
|
160
|
+
return {
|
161
|
+
...memo,
|
162
|
+
[key]: {
|
163
|
+
...properties[key],
|
164
|
+
$type,
|
165
|
+
inputType,
|
166
|
+
required: (
|
167
|
+
typeof propSchema.required === 'boolean'
|
168
|
+
? propSchema.required
|
169
|
+
: (
|
170
|
+
!Array.isArray(propSchema.type)
|
171
|
+
|| !propSchema.type.includes('null')
|
172
|
+
)
|
173
|
+
),
|
174
|
+
},
|
175
|
+
};
|
176
|
+
},
|
177
|
+
{},
|
178
|
+
),
|
179
|
+
...rest,
|
180
|
+
};
|
181
|
+
};
|
182
|
+
|
183
|
+
|
141
184
|
export const createModel = (
|
142
185
|
baseUrl,
|
143
186
|
state,
|
@@ -145,19 +188,20 @@ export const createModel = (
|
|
145
188
|
schema = {},
|
146
189
|
models = {},
|
147
190
|
) => {
|
148
|
-
const
|
149
|
-
const
|
191
|
+
const parsedSchema = parseSchema(schema);
|
192
|
+
const { primaryKey, properties } = parsedSchema;
|
193
|
+
const validator = createValidator(parsedSchema);
|
150
194
|
const buildURL = createBuildURL(collectionName);
|
151
195
|
const req = (...args) => createClient(baseUrl, state.authUser)(...args);
|
152
196
|
const create = ({ data, ...q }) => req(buildURL(null, q), {
|
153
197
|
method: 'POST',
|
154
|
-
body: serializeData(data,
|
198
|
+
body: serializeData(data, parsedSchema),
|
155
199
|
});
|
156
200
|
const put = ({ data, ...q }) => {
|
157
201
|
const { where, ...rest } = q;
|
158
202
|
return req(buildURL(where[primaryKey], rest), {
|
159
203
|
method: 'PUT',
|
160
|
-
body: serializeData(data,
|
204
|
+
body: serializeData(data, parsedSchema),
|
161
205
|
});
|
162
206
|
};
|
163
207
|
|
@@ -168,9 +212,9 @@ export const createModel = (
|
|
168
212
|
|
169
213
|
const parsed = Object.keys(data).reduce(
|
170
214
|
(memo, key) => {
|
171
|
-
const propSchema =
|
215
|
+
const propSchema = properties[key] || {};
|
172
216
|
const { type, format, items } = propSchema;
|
173
|
-
if (items && isOneToManyRelation(
|
217
|
+
if (items && isOneToManyRelation(parsedSchema, key)) {
|
174
218
|
const relationModelName = lcFirst(items.$ref.split('/').pop());
|
175
219
|
return {
|
176
220
|
...memo,
|
@@ -182,9 +226,9 @@ export const createModel = (
|
|
182
226
|
};
|
183
227
|
}
|
184
228
|
const ref = (
|
185
|
-
isRequiredOneToOneRelation(
|
229
|
+
isRequiredOneToOneRelation(parsedSchema, key)
|
186
230
|
? propSchema.$ref
|
187
|
-
: isOptionalOneToOneRelation(
|
231
|
+
: isOptionalOneToOneRelation(parsedSchema, key)
|
188
232
|
? propSchema.anyOf[0]?.$ref
|
189
233
|
: null
|
190
234
|
);
|
@@ -208,27 +252,27 @@ export const createModel = (
|
|
208
252
|
{},
|
209
253
|
);
|
210
254
|
return (
|
211
|
-
typeof
|
212
|
-
?
|
255
|
+
typeof parsedSchema.parse === 'function'
|
256
|
+
? parsedSchema.parse(parsed)
|
213
257
|
: parsed
|
214
258
|
);
|
215
259
|
};
|
216
260
|
|
217
|
-
const relations = Object.keys(
|
261
|
+
const relations = Object.keys(properties || {}).reduce(
|
218
262
|
(memo, key) => (
|
219
|
-
isRequiredOneToOneRelation(
|
263
|
+
isRequiredOneToOneRelation(parsedSchema, key)
|
220
264
|
? Object.assign(memo, {
|
221
265
|
all: memo.all.concat(key),
|
222
266
|
oneToOne: memo.oneToOne.concat(key),
|
223
267
|
requiredOneToOne: memo.requiredOneToOne.concat(key),
|
224
268
|
})
|
225
|
-
: isOptionalOneToOneRelation(
|
269
|
+
: isOptionalOneToOneRelation(parsedSchema, key)
|
226
270
|
? Object.assign(memo, {
|
227
271
|
all: memo.all.concat(key),
|
228
272
|
oneToOne: memo.oneToOne.concat(key),
|
229
273
|
optionalOneToOne: memo.optionalOneToOne.concat(key),
|
230
274
|
})
|
231
|
-
: isOneToManyRelation(
|
275
|
+
: isOneToManyRelation(parsedSchema, key)
|
232
276
|
? Object.assign(memo, {
|
233
277
|
all: memo.all.concat(key),
|
234
278
|
oneToMany: memo.oneToMany.concat(key),
|
@@ -246,7 +290,7 @@ export const createModel = (
|
|
246
290
|
|
247
291
|
return {
|
248
292
|
get schema() {
|
249
|
-
return
|
293
|
+
return parsedSchema;
|
250
294
|
},
|
251
295
|
get relations() {
|
252
296
|
return relations;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@laboratoria/sdk-js",
|
3
|
-
"version": "3.
|
3
|
+
"version": "3.4.0",
|
4
4
|
"description": "Laboratoria JavaScript (browser) SDK",
|
5
5
|
"license": "MIT",
|
6
6
|
"scripts": {
|
@@ -11,14 +11,14 @@
|
|
11
11
|
},
|
12
12
|
"dependencies": {
|
13
13
|
"blueimp-md5": "^2.19.0",
|
14
|
-
"firebase": "^9.
|
14
|
+
"firebase": "^9.17.1"
|
15
15
|
},
|
16
16
|
"devDependencies": {
|
17
17
|
"@babel/core": "^7.20.12",
|
18
18
|
"@babel/plugin-transform-modules-commonjs": "^7.20.11",
|
19
|
-
"babel-jest": "^29.4.
|
20
|
-
"jest": "^29.4.
|
21
|
-
"jest-environment-jsdom": "^29.4.
|
19
|
+
"babel-jest": "^29.4.2",
|
20
|
+
"jest": "^29.4.2",
|
21
|
+
"jest-environment-jsdom": "^29.4.2",
|
22
22
|
"webpack": "^5.75.0",
|
23
23
|
"webpack-cli": "^5.0.1"
|
24
24
|
},
|