Skip to content

Commit 1173423

Browse files
committed
Refactoring validation and remove 'version' parameter
1 parent 452d4d1 commit 1173423

File tree

12 files changed

+12055
-4837
lines changed

12 files changed

+12055
-4837
lines changed

Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ WORKDIR /app
55

66
COPY . .
77

8-
RUN yarn install --frozen-lockfile
8+
RUN npm ci
9+
10+
ENV PORT=8000
11+
ENV NODE_ENV=production
12+
13+
RUN npm run build
914

1015
EXPOSE 8000
11-
CMD ["yarn", "start"]
16+
CMD ["npm", "run", "start"]
1217

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 ChainAPI
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,127 @@
11
# Validator API
22

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

Comments
 (0)