Skip to content

Commit

Permalink
Create validation package (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
faihegberg committed Feb 8, 2023
1 parent c370d93 commit 46b9632
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-emus-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@actnowcoalition/actnow.js": minor
---

Create validation package
10 changes: 2 additions & 8 deletions src/assert/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# @actnowcoalition/assert
## Usage

Assertion functions

## Installing

```sh
yarn add @actnowcoalition/assert
```
> Assertion functions
## License

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./number-format";
export * from "./regions";
export * from "./time-utils";
export * from "./ui-components";
export * from "./validate";
10 changes: 1 addition & 9 deletions src/metrics/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
# @actnowcoalition/metrics
## Design Overview / Usage

> Classes for representing metrics and loading metric data
## Installing

```sh
yarn add @actnowcoalition/metrics
```

## Design Overview / Usage

### Main classes

The metrics package contains a few key classes:
Expand Down
8 changes: 1 addition & 7 deletions src/number-format/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# @actnowcoalition/number-format
## Usage

> Number formatting utility functions
## Installing

```sh
yarn add @actnowcoalition/number-format
```

## License

[MIT](./LICENSE)
10 changes: 1 addition & 9 deletions src/regions/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# @actnowcoalition/regions
## Usage

> A package containing classes and datasets to represent geographic areas and administrative divisions with a uniform interface.
## Usage

The `Region` class defines the main elements that represent an administrative division.

```tsx
Expand Down Expand Up @@ -73,12 +71,6 @@ const irl = nations.findByRegionId("IRL");
console.log(irl.fullName); // Ireland
```

## Installing

```sh
yarn add @actnowcoalition/regions
```

## License

[MIT](./LICENSE)
8 changes: 1 addition & 7 deletions src/time-utils/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# @actnowcoalition/time-utils
## Usage

> Utility functions to handle and format time
## Installing

```sh
yarn add @actnowcoalition/time-utils
```

## License

[MIT](./LICENSE)
8 changes: 1 addition & 7 deletions src/ui-components/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# @actnowcoalition/ui-components
## Usage

> UI components for Act Now
## Installing

```sh
yarn add @actnowcoalition/ui-components
```

## License

[MIT](./LICENSE)
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from "react";

import { Stack, Typography } from "@mui/material";

import { assert } from "../../../assert";
import { Category, Metric } from "../../../metrics";
import { validate } from "../../../validate";
import { LegendCategorical } from "../LegendCategorical";
import { useMetricCatalog } from "../MetricCatalogContext";

Expand All @@ -29,7 +29,7 @@ export const MetricLegendCategorical = ({
const metricCatalog = useMetricCatalog();
metric = metricCatalog.getMetric(metric);
const items = metric.categorySet?.categories;
assert(
validate(
items,
"Metric must define categories in order to use MetricLegendCategorical. " +
`No categories found for metric: ${metric}`
Expand Down
7 changes: 7 additions & 0 deletions src/validate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Usage

> Validation functions
## License

[MIT](./LICENSE)
26 changes: 26 additions & 0 deletions src/validate/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { throwValidationError, validate } from "./index";

describe("validate", () => {
test("throws an error if condition is falsy", () => {
const errorMsg = "Error message";
expect(() => validate(false)).toThrow();
expect(() => validate(0)).toThrow();
expect(() => validate(undefined)).toThrow();
expect(() => validate(null)).toThrow();
expect(() => validate("")).toThrow();
expect(() => validate(false, errorMsg)).toThrowError(errorMsg);
});

test("pass if condition is truthy", () => {
expect(() => validate(true)).not.toThrow();
expect(() => validate(1)).not.toThrow();
});
});

describe("throwActNowJsError", () => {
const errorMsg = "Error message";
test("throws an error with the given message", () => {
expect(() => throwValidationError()).toThrow();
expect(() => throwValidationError(errorMsg)).toThrowError(errorMsg);
});
});
30 changes: 30 additions & 0 deletions src/validate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Test if the `condition` is truthy. If `condition` is false, it throws
* an error with an optional message. If the message parameter is missing,
* it throws an error with a default message.
*/
export function validate(
condition: unknown,
errorMessage?: string
): asserts condition {
if (!condition) {
throw new ActNowJsError(errorMessage);
}
}

/**
* Throws an Error with the (optionally) provided error message.
*/
export function throwValidationError(errorMessage?: string): never {
throw new ActNowJsError(errorMessage);
}

/**
* An ActNowJsError class to differentiate from default and internal errors.
*/
export class ActNowJsError extends Error {
constructor(public readonly errorMessage?: string) {
super(errorMessage);
this.name = "ActNowJsError";
}
}

0 comments on commit 46b9632

Please sign in to comment.