journal/tool/lib/journalTypes.jq
2024-11-09 13:13:47 +00:00

117 lines
4 KiB
Plaintext

include "dropins";
import "typeLib" as typeLib;
def ensureAdministrationRoute:
. as $administrationRoute |
if typeLib::typecheckingEnabled then
[
"ORAL",
"SUBLINGUAL",
"BUCCAL",
"INSUFFLATED",
"RECTAL",
"TRANSDERMAL",
"SUBCUTANEOUS",
"INTRAMUSCULAR",
"INTRAVENOUS",
"SMOKED",
"INHALED"
] | any(index($administrationRoute))
end;
def ensureIngestion:
. as $ingestion |
if typeLib::typecheckingEnabled then
typeLib::ensureObject |
$ingestion | typeLib::ensureKey("ingestion"; "substanceName") |
.substanceName | typeLib::ensureWrapError("experience:substanceName"; typeLib::ensureString) |
$ingestion | typeLib::ensureKey("ingestion"; "time") |
.time | typeLib::ensureWrapError("experience:time"; typeLib::ensureNumber) |
$ingestion | typeLib::ensureKey("ingestion"; "creationDate") |
.creationDate | typeLib::ensureWrapError("experience:creationDate"; typeLib::ensureNumber) |
# TODO: check routes
$ingestion |
typeLib::ensureKey("ingestion"; "administrationRoute") |
$ingestion.administrationRoute |
typeLib::ensureWrapError("experience:administrationRoute"; typeLib::ensureString) |
$ingestion.administrationRoute |
ensureAdministrationRoute |
typeLib::ensureWrapError(
"experience:dose";
$ingestion | typeLib::ensureIfKey("dose"; typeLib::ensureNullOr(typeLib::ensureNumber))
) |
$ingestion | typeLib::ensureKey("ingestion"; "isDoseAnEstimate") |
.isDoseAnEstimate | typeLib::ensureWrapError("experience:isDoseAnEstimate"; typeLib::ensureBool) |
$ingestion | typeLib::ensureKey("ingestion"; "units") |
.units | typeLib::ensureWrapError("experience:units"; typeLib::ensureString) |
$ingestion | typeLib::ensureKey("ingestion"; "notes") |
.notes | typeLib::ensureWrapError("experience:notes"; typeLib::ensureString) |
typeLib::ensureWrapError(
"experience:stomachFullness";
$ingestion | typeLib::ensureIfKey("stomachFullness"; typeLib::ensureNullOr(typeLib::ensureString))
) |
typeLib::ensureWrapError(
"experience:customUnitId";
$ingestion | typeLib::ensureIfKey("customUnitId"; typeLib::ensureNullOr(typeLib::ensureNumber))
) |
$ingestion
end;
def ensureIngestions:
. as $ingestions |
if typeLib::typecheckingEnabled then
(reduce $ingestions[] as $ingestion (null; $ingestion | ensureIngestion))
end;
def ensureExperience:
. as $experience |
if typeLib::typecheckingEnabled then
$experience | typeLib::ensureObject |
$experience | typeLib::ensureKey("experience"; "title") |
$experience.title | typeLib::ensureWrapError("experience:title"; typeLib::ensureString) |
$experience | typeLib::ensureKey("experience"; "text") |
$experience.text | typeLib::ensureWrapError("experience:text"; typeLib::ensureString) |
$experience | typeLib::ensureKey("experience"; "creationDate") |
$experience.creationDate | typeLib::ensureWrapError("experience:creationDate"; typeLib::ensureNumber) |
$experience | typeLib::ensureKey("experience"; "sortDate") |
$experience.sortDate | typeLib::ensureWrapError("experience:sortDate"; typeLib::ensureNumber) |
$experience | typeLib::ensureKey("experience"; "ingestions") |
$experience.ingestions | typeLib::ensureWrapError("experience:ingestions"; typeLib::ensureArray) |
$experience.ingestions | ensureIngestions |
$experience
end;
def ensureExperiences:
. as $experiences |
if typeLib::typecheckingEnabled then
(reduce $experiences[] as $experience (null; $experience | ensureExperience))
end;
def ensureExportData:
. as $exportData |
if typeLib::typecheckingEnabled then
typeLib::ensureObject |
typeLib::ensureKey("exportData"; "customSubstances") |
typeLib::ensureKey("exportData"; "customUnits") |
typeLib::ensureKey("exportData"; "experiences") |
typeLib::ensureKey("exportData"; "substanceCompanions") |
.experiences | ensureExperiences
end;