Skip to content

Commit 7ccc58e

Browse files
authored
[DAPS-1896] - feature: foxx, schema format and type added to schema create API (#1897)
1 parent b995424 commit 7ccc58e

2 files changed

Lines changed: 382 additions & 15 deletions

File tree

core/database/foxx/api/schema_router.js

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,23 @@ router
153153
action: function () {
154154
const client = g_lib.getUserFromClientID(req.queryParams.client);
155155

156-
// Schema validator has already been run at this point; however, DataFed further restricts
157-
// the allowed character set for keys and this must be applied at this point.
158-
validateProperties(req.body.def.properties);
159-
160156
var obj = {
161157
cnt: 0,
162158
ver: 0,
163159
pub: req.body.pub,
164-
def: req.body.def,
160+
format: req.body.format,
161+
type: req.body.type,
165162
};
166163

164+
if (req.body.type === "json-schema") {
165+
// Schema validator has already been run at this point; however, DataFed further restricts
166+
// the allowed character set for keys and this must be applied at this point.
167+
validateProperties(req.body.def.properties);
168+
obj.def = req.body.def;
169+
} else {
170+
obj.def = {};
171+
}
172+
167173
if (req.body.sys) {
168174
if (!client.is_admin)
169175
throw [
@@ -219,6 +225,8 @@ router
219225
own_id: sch?.own_id,
220226
pub: req.body.pub,
221227
sys: req.body.sys,
228+
format: sch?.format,
229+
type: sch?.type,
222230
},
223231
});
224232
} catch (e) {
@@ -234,6 +242,8 @@ router
234242
own_id: sch?.own_id,
235243
pub: req.body.pub,
236244
sys: req.body.sys,
245+
format: sch?.format,
246+
type: sch?.type,
237247
},
238248
error: e,
239249
});
@@ -249,6 +259,8 @@ router
249259
def: joi.object().required(),
250260
pub: joi.boolean().optional().default(true),
251261
sys: joi.boolean().optional().default(false),
262+
format: joi.string().default("json").valid("json", "xml", "yaml"),
263+
type: joi.string().default("json-schema").valid("json-schema", "linkml"),
252264
})
253265
.required(),
254266
"Schema fields",
@@ -353,8 +365,12 @@ router
353365
g_lib.procInputParam(req.body, "desc", true, obj);
354366

355367
if (req.body.def) {
356-
validateProperties(req.body.def.properties);
357-
obj.def = req.body.def;
368+
if (sch_old.type === "json-schema") {
369+
validateProperties(req.body.def.properties);
370+
obj.def = req.body.def;
371+
} else {
372+
obj.def = {};
373+
}
358374
}
359375

360376
sch_new = g_db.sch.update(sch_old._id, obj, {
@@ -505,8 +521,12 @@ router
505521
g_lib.procInputParam(req.body, "desc", true, sch);
506522

507523
if (req.body.def != undefined) {
508-
validateProperties(req.body.def.properties);
509-
sch.def = req.body.def;
524+
if (sch.type === "json-schema") {
525+
validateProperties(req.body.def.properties);
526+
sch.def = req.body.def;
527+
} else {
528+
sch.def = {};
529+
}
510530
}
511531

512532
var old_id = sch._id;
@@ -548,6 +568,8 @@ router
548568
id: sch_new.id,
549569
pub: req.body.pub,
550570
sys: req.body.sys,
571+
type: sch_new?.type,
572+
format: sch_new?.format,
551573
},
552574
});
553575
} catch (e) {
@@ -564,6 +586,8 @@ router
564586
id: sch_new?.id,
565587
pub: req.body?.pub,
566588
sys: req.body?.sys,
589+
type: sch_new?.type,
590+
format: sch_new?.format,
567591
},
568592
error: e,
569593
});
@@ -745,6 +769,15 @@ router
745769
fixSchOwnNm(sch);
746770

747771
sch.id = parsed.id + ":" + parsed.ver;
772+
773+
// If schema is missing sch_format and sch_type default to json
774+
if (!Object.hasOwn(sch, "format")) {
775+
sch.format = "json";
776+
}
777+
if (!Object.hasOwn(sch, "type")) {
778+
sch.type = "json-schema";
779+
}
780+
748781
res.send([sch]);
749782
logger.logRequestSuccess({
750783
client: req.queryParams?.client,
@@ -759,6 +792,8 @@ router
759792
id: sch.id,
760793
pub: sch.pub,
761794
sys: sch.sys,
795+
sch_format: sch.format,
796+
sch_type: sch.type,
762797
},
763798
});
764799
} catch (e) {
@@ -851,18 +886,16 @@ router
851886
} else {
852887
if (req.queryParams.sort_rev) qry += " sort i.id desc, i.ver";
853888
else qry += " sort i.id,i.ver";
854-
855-
//qry += (req.queryParams.sort_rev?" desc":"");
856889
}
857890

858891
qry +=
859892
" limit " +
860893
off +
861894
"," +
862895
cnt +
863-
" return {_id:i._id,id:i.id,ver:i.ver,cnt:i.cnt,pub:i.pub,own_nm:i.own_nm,own_id:i.own_id}";
864-
865-
//qry += " filter (i.pub == true || i.own_id == @uid) sort i.id limit " + off + "," + cnt + " return {id:i.id,ver:i.ver,cnt:i.cnt,pub:i.pub,own_nm:i.own_nm,own_id:i.own_id}";
896+
" return {_id:i._id,id:i.id,ver:i.ver,cnt:i.cnt,pub:i.pub,own_nm:i.own_nm,own_id:i.own_id," +
897+
"type: NOT_NULL(i.type, 'json-schema')," +
898+
"format: NOT_NULL(i.format, 'json')}";
866899

867900
result = g_db._query(
868901
qry,
@@ -1016,7 +1049,9 @@ function updateSchemaRefs(a_sch) {
10161049
r,
10171050
refs = new Set();
10181051

1019-
gatherRefs(a_sch.def.properties, refs);
1052+
if (a_sch.def && typeof a_sch.def === "object") {
1053+
gatherRefs(a_sch.def.properties, refs);
1054+
}
10201055

10211056
refs.forEach(function (v) {
10221057
idx = v.indexOf(":");

0 commit comments

Comments
 (0)