Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPC-8479: Add strict codec for disaggregation model value #92

Merged
merged 3 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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