Skip to content

Commit

Permalink
Merge pull request #92 from UN-OCHA/HPC-8479
Browse files Browse the repository at this point in the history
HPC-8479: Add strict codec for disaggregation model value
  • Loading branch information
Pl217 authored Apr 7, 2022
2 parents 3299cd3 + 1f62742 commit 315e597
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unocha/hpc-api-core",
"version": "4.2.0",
"version": "4.2.1",
"description": "Core libraries supporting HPC.Tools API Backend",
"license": "Apache-2.0",
"private": false,
Expand Down
35 changes: 30 additions & 5 deletions src/db/models/disaggregationModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ export const DISAGGREGATION_MODEL_ID = brandedType<
DisaggregationModelId
>(t.number);

export const DISAGGREGATION_MODEL_CREATOR = t.type({
participantHidId: t.string,
roles: t.array(t.string),
});

const LOCATION_INFO = t.intersection([
t.type({ name: t.string }),
t.partial({
Expand All @@ -36,6 +31,16 @@ const LOCATION_INFO = t.intersection([
}),
]);

/**
* Strict version of `LOCATION_INFO` codec defined above.
* Intended to be used when creating new records, in order
* to enforce stricter type of location ID.
*/
const LOCATION_INFO_STRICT = t.intersection([
LOCATION_INFO.types[0],
t.partial({ id: LOCATION_ID }),
]);

export const DISAGGREGATION_MODEL_VALUE = t.type({
categories: t.array(
t.type({
Expand All @@ -50,6 +55,26 @@ export const DISAGGREGATION_MODEL_VALUE = t.type({
status: t.boolean,
});

/**
* Strict version of `DISAGGREGATION_MODEL_VALUE` codec defined above.
* Intended to be used when creating new records, in order to enforce
* stricter type of location ID.
*
* Location ID used to allow for string type, which is why there are
* many records using location's pcode instead of ID. This stricter
* type should be used for validating new records upon creation.
*/
export const DISAGGREGATION_MODEL_VALUE_STRICT = t.type({
categories: DISAGGREGATION_MODEL_VALUE.props.categories,
status: DISAGGREGATION_MODEL_VALUE.props.status,
locations: t.array(
t.intersection([
LOCATION_INFO_STRICT,
t.partial({ parent: LOCATION_INFO_STRICT }),
])
),
});

export default defineIDModel({
tableName: 'disaggregationModel',
fields: {
Expand Down

0 comments on commit 315e597

Please sign in to comment.