Skip to content

Commit

Permalink
Define BUDGET_SEGMENT_BREAKDOWN_CONTENT_STRICT codec
Browse files Browse the repository at this point in the history
  • Loading branch information
Pl217 committed Apr 11, 2022
1 parent 9624a1f commit a493b59
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 a493b59

Please sign in to comment.