Skip to content

Commit

Permalink
Merge pull request #93 from UN-OCHA/HPC-8461
Browse files Browse the repository at this point in the history
Define `BUDGET_SEGMENT_BREAKDOWN_CONTENT_STRICT` codec
  • Loading branch information
czmj authored Apr 11, 2022
2 parents 9624a1f + a493b59 commit bb9d12e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/db/models/budgetSegmentBreakdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ export const BUDGET_SEGMENT_BREAKDOWN_CONTENT = t.intersection([
}),
]);

/**
* Strict version of `BUDGET_SEGMENT_BREAKDOWN_CONTENT` codec
* defined above. Intended to be used when creating new records,
* in order to enforce stricter type of `amount` and `percent`.
*
* `percent` used to allow for string type, while `amount` allowed
* for both `string` and `null` (in addition to proper `number` type),
* which is why there are many records with "corrupt" data. This stricter
* type should be used for validating new records upon creation.
*/
export const BUDGET_SEGMENT_BREAKDOWN_CONTENT_STRICT = t.intersection([
t.type({ amount: t.number }),
t.partial({
originalAmount:
BUDGET_SEGMENT_BREAKDOWN_CONTENT.types[1].props.originalAmount,
percent: t.number,
fixed: BUDGET_SEGMENT_BREAKDOWN_CONTENT.types[1].props.fixed,
breakdown: BUDGET_SEGMENT_BREAKDOWN_CONTENT.types[1].props.breakdown,
}),
]);

export type BudgetSegmentBreakdownContentStrict = t.TypeOf<
typeof BUDGET_SEGMENT_BREAKDOWN_CONTENT_STRICT
>;

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

0 comments on commit bb9d12e

Please sign in to comment.