Skip to content

Commit 33a5084

Browse files
authored
Merge pull request #28 from fairdataihub/validator
feat: ✨ Validator Documentation for Dev + Staging site mention for Zenodo Sandbox API
2 parents 4361965 + cf54f8d commit 33a5084

File tree

4 files changed

+125
-2
lines changed

4 files changed

+125
-2
lines changed

docs/.vitepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ function appSidebarGuide() {
144144
items: [
145145
{ text: 'Bot Feature', link: '/dev/bot.md' },
146146
{ text: 'UI Page', link: '/dev/ui.md' },
147+
{ text: 'Validation Feature', link: '/dev/validator.md' },
147148
],
148149
},
149150
];

docs/dev/bot.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ head:
1212

1313
The best way to learn how to add a new feature to Codefair is to complete our step-by-step tutorial below:
1414

15-
The Codefair repository is split into three main subfolders: `ui`, `bot` and `validator`. This tutorial will guide you through the process of adding a new check feature to Codefair bot. Codefair is modularized in a way to try and make it easy for anyone to plug in a new check with how our code structure is created. We have a main function that calls all the available checks `checkForCompliance()` and within there you will call your new feature. The example feature will below will guide you through the standard we have for creating a new feature to check if a `expecto_patronum.md` file exists. This feature will create append the detailed results to a string that will then be used for the GitHub issue body. A clickable badge to direct a user to the Codefair UI to add the file.
15+
The Codefair repository is split into three main subfolders: `ui`, `bot` and `validator`. This tutorial will guide you through the process of adding a new check feature to Codefair bot. Codefair is modularized in a way to try and make it easy for anyone to plug in a new check with how our code structure is created. We have a main function that calls all the available checks `checkForCompliance()` and within there you will call your new feature. The example feature will below will guide you through the standard we have for creating a new feature to check if a `expecto_patronum.md` file exists. This feature will append the detailed results to an object that will then be used for the GitHub issue renderer. A clickable badge to direct a user to the Codefair UI to add the file is created for each compliance check to allow users to quickly handle a FAIR compliance check.
1616

1717
This page will focus on the steps required to add the new feature to the Codefair bot. To learn how to add a UI feature, you can refer to the [UI documentation](./ui.md).
1818

@@ -74,7 +74,7 @@ const issueBody = await renderIssues(
7474
);
7575
```
7676

77-
> **Note**
77+
> :bulb: **Note**
7878
> The event listeners we have can be found in the `bot/index.js` file. If you need an event listener not already available you can add it in this file. A list of GitHub event listeners is available [here](https://docs.github.com/en/webhooks/webhook-events-and-payloads).
7979
8080
## **Step 3**: Create schema for the new check

docs/dev/validator.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
lang: en-US
3+
title: Validation Usage
4+
description: Adding a validation feature to Codefair
5+
head:
6+
- - meta
7+
- name: og:image
8+
content: https://kalai.fairdataihub.org/api/generate?title=Codefair%20Documentation&description=Adding%20a%20validation%20feature%20to%20Codefair&app=codefair&org=fairdataihub
9+
---
10+
11+
# :hammer_and_wrench: Adding a Validation Endpoint
12+
13+
Extend Codefair’s **Validator Microservice** (Flask + Flask-RESTX) by adding custom HTTP endpoints under `validator/apis`. Follow the sections below to seamlessly integrate your new validation logic.
14+
15+
## :sparkles: Overview
16+
17+
This guide walks through creating a new validation route, securing inputs, updating dependencies, and testing your endpoint locally.
18+
19+
## :bookmark: Prerequisites
20+
21+
- A Python 3.8+ environment
22+
- Familiarity with Flask and RESTful APIs
23+
24+
## :rocket: Register a New API Route
25+
26+
Create a new route in `validator/apis/__init__.py`:
27+
28+
```python
29+
@api.route('/validate-json', endpoint='validate-json')
30+
class ValidateJSON(Resource):
31+
"""Validate a JSON file at given path or URL"""
32+
33+
@api.response(200, 'Success')
34+
@api.response(400, 'Validation Error')
35+
@api.expect(
36+
api.parser()
37+
.add_argument(
38+
'file_path', type=str,
39+
help='The path or URL of the JSON file',
40+
required=True,
41+
)
42+
)
43+
def post(self):
44+
"""Validate a JSON file"""
45+
args = api.payload or {}
46+
file_path = args.get('file_path')
47+
48+
# Input validation
49+
if not file_path:
50+
return {'message': 'Validation Error', 'error': 'file_path is required'}, 400
51+
52+
# Instantiate and run your custom validator
53+
validator = JsonValidator()
54+
result = validator.validate(file_path)
55+
56+
if result.get('valid'):
57+
return {'message': 'valid', 'details': result['message']}, 200
58+
else:
59+
return {'message': 'invalid', 'error': result['message']}, 400
60+
```
61+
62+
> 📌 You can base this on existing validators like `ValidateCWL` for consistent style.
63+
64+
## :lock: Input Sanitization & Security
65+
66+
Ensure robust input handling to prevent attacks:
67+
68+
- **Path traversal:** Reject `..` or leading `/` in `file_path`.
69+
- **Command injection:** Forbid characters like `;`, `&`, `|` or use `shlex.quote()` if invoking subprocesses.
70+
- **Remote URLs:** Allow only `http://` and `https://` when downloading content.
71+
72+
Reuse helper functions (e.g., `clean_output`) from other endpoints to sanitize and parse external tool outputs.
73+
74+
## :package: Update Dependencies & Entrypoint
75+
76+
1. **Dependencies:** If your validator relies on new libraries (e.g., `jsonschema`), add them to `validator/requirements.txt`.
77+
2. **Entrypoint:** No changes required in `validator/app.py`—all `@api.route` decorators are auto-registered via `api.init_app(app)`.
78+
79+
> 🔄 Remember to import your new validator module in `validator/app.py` if it’s not picked up automatically:
80+
>
81+
> ```python
82+
> import validators.json_validator
83+
> ```
84+
85+
## :white_check_mark: Testing Your Endpoint
86+
87+
1. **Activate your environment:**
88+
89+
```bash
90+
python -m venv .venv
91+
source .venv/bin/activate
92+
```
93+
94+
2. **Install dependencies:**
95+
96+
```bash
97+
pip install -r validator/requirements.txt
98+
```
99+
100+
3. **Run the service:**
101+
102+
```bash
103+
python validator/app.py --host $HOST --port $PORT
104+
```
105+
106+
4. **Test via `curl` or Postman:**
107+
108+
```bash
109+
curl -X POST http://localhost:5000/validate-json \
110+
-H 'Content-Type: application/json' \
111+
-d '{"file_path": "path/to/file.json"}'
112+
```
113+
114+
Look for a JSON response indicating `valid` or `invalid` along with details or error messages.
115+
116+
---
117+
118+
Happy validating with Codefair! 🚀

docs/docs/archive.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ head:
1212

1313
In the [FAIR4RS Principles](https://doi.org/10.1038/s41597-022-01710-x), many of the principles aimed at making research software **Findable** and **Accessible** can be fulfilled by archiving the software in a suitable archival repository. This is critical to ensure the long-term preservation and accessibility of the software. One recommended approach, as prescribed by the [FAIR-BioRS guidelines](https://doi.org/10.1038/s41597-023-02463-x), is to archive each release of your software on Zenodo.
1414

15+
::: tip
16+
Our staging site at <https://staging.codefair.io/> uses the Zenodo Sandbox API rather than the production Zenodo API. You can test the full FAIR release workflow here before archiving your software on the live Zenodo platform.
17+
:::
18+
1519
::: warning
1620
There is a webhook method that allows you to [automatically archive each GitHub release on Zenodo](https://docs.github.com/en/repositories/archiving-a-github-repository/referencing-and-citing-content). However, please note that this method does not provide the DOI before the software is archived on Zenodo, meaning the DOI cannot be included in the software's metadata on GitHub beforehand. This does not align with the FAIR4RS Principle F3, which states: "Metadata should clearly and explicitly include the identifier of the software they describe."
1721
:::

0 commit comments

Comments
 (0)