|
1 | 1 | # Validator API
|
2 | 2 |
|
3 |
| -TODO |
| 3 | +- [Validator API](#validator-api) |
| 4 | + - [Requirements](#requirements) |
| 5 | + - [Installation](#installation) |
| 6 | + - [Usage](#usage) |
| 7 | + - [Endpoints](#endpoints) |
| 8 | + - [1. Healthcheck](#1-healthcheck) |
| 9 | + - [2. Validate](#2-validate) |
| 10 | + - [Adding a new Airnode validator version](#adding-a-new-airnode-validator-version) |
| 11 | + - [Testing](#testing) |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +Before getting started, you will need to have the following tools installed locally: |
| 16 | + |
| 17 | +- [Node.js](https://github.com/nvm-sh/nvm) |
| 18 | +- [Docker [Optional]](https://www.docker.com/) |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +Install the dependencies by running the following command: |
| 23 | + |
| 24 | +```sh |
| 25 | +npm install |
| 26 | +``` |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +Once the project dependencies have been installed, you can start the server by running one of the following commands |
| 31 | + |
| 32 | +```sh |
| 33 | +# Option 1: Development mode |
| 34 | +# The server restarted whenever a file is changed |
| 35 | +npm run dev |
| 36 | + |
| 37 | +# Option 2: Production mode |
| 38 | +# The files are compiled using TypeScript and run via Node.js |
| 39 | +npm run build |
| 40 | +npm run start |
| 41 | + |
| 42 | +# Option 3: Docker |
| 43 | +docker build -t validator-api:latest . |
| 44 | +docker run |
| 45 | +``` |
| 46 | + |
| 47 | +This will start an HTTP server on the port defined by the environment variable `PORT` (default: `8000`). This can be configured locally by adding a `.env` file to the project root (see `.env.example`). |
| 48 | + |
| 49 | +## Endpoints |
| 50 | + |
| 51 | +Once the server is running the following endpoints are exposed: |
| 52 | + |
| 53 | +### 1. Healthcheck |
| 54 | + |
| 55 | +`GET /` |
| 56 | + |
| 57 | +This endpoint returns a static JSON object indicating that the server is running. |
| 58 | + |
| 59 | +Example: |
| 60 | + |
| 61 | +```sh |
| 62 | +curl 'http://localhost:8000/ |
| 63 | +# { "status": "alive" } |
| 64 | +``` |
| 65 | +
|
| 66 | +### 2. Validate |
| 67 | +
|
| 68 | +`POST /validate` |
| 69 | +
|
| 70 | +This endpoint accepts a single parameter: |
| 71 | +
|
| 72 | +1. `config` - the full Airnode config (usually defined in a config.json file) |
| 73 | +
|
| 74 | +Example: |
| 75 | +
|
| 76 | +```sh |
| 77 | +curl --request POST 'localhost:8000/validate' \ |
| 78 | + --header 'Content-Type: application/json' \ |
| 79 | + --data-raw '{ "config": { ... } }' |
| 80 | +# { value: false, errors: [...] } |
| 81 | +``` |
| 82 | +
|
| 83 | +## Adding a new Airnode validator version |
| 84 | +
|
| 85 | +This package is intended to be kept up-to-date with Airnode releases. When a new Airnode version is released, this will include a new version of the [airnode-validator](https://www.npmjs.com/package/@api3/airnode-validator) package. In order to support a new validator version, the following steps need to be followed: |
| 86 | +
|
| 87 | +1. Add the new version using an alias |
| 88 | +
|
| 89 | +```sh |
| 90 | +# Add support for v1.2 (ignore the patch version in the alias if possible) |
| 91 | +npm install validator-1.2@npm:@api3/[email protected] |
| 92 | +``` |
| 93 | +
|
| 94 | +2. **IMPORTANT**: Add a test fixture |
| 95 | +
|
| 96 | +Place a valid `config.json` in the `test/fixtures` directory into a folder identified by the version. e.g. A valid `config.json` file should be copied to `test/fixtures/1.2/config.json`. |
| 97 | +
|
| 98 | +3. Add a "case" for the new version |
| 99 | +
|
| 100 | +```ts |
| 101 | +// ./src/validation.ts |
| 102 | +import * as validator12 from 'validator-1.2'; |
| 103 | +
|
| 104 | +const getVersionValidator = (semver: Semver) => { |
| 105 | + return match(semver) |
| 106 | + ... |
| 107 | + .with({ major: 1, minor: 2 }, () => validator12.parseConfig) |
| 108 | + .otherwise(() => { |
| 109 | + throw new Error(...); |
| 110 | + }); |
| 111 | +}; |
| 112 | +``` |
| 113 | +
|
| 114 | +## Testing |
| 115 | +
|
| 116 | +The project tests can be run with the following commands: |
| 117 | +
|
| 118 | +```sh |
| 119 | +# Runs all tests without any flags |
| 120 | +npm run test |
| 121 | +
|
| 122 | +# Runs all tests and watches for file changes |
| 123 | +npm run test:watch |
| 124 | +
|
| 125 | +# Runs all tests and outputs the coverage metrics to /coverage |
| 126 | +npm run test:coverage |
| 127 | +``` |
0 commit comments