|
| 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! 🚀 |
0 commit comments