data-constraints
v1.0.2
Published
The easiest way to validate your data consistency. Ensure the quality of your files without writing code
Maintainers
Readme
Data Constraints
Data Constraints is the easiest way to validate your data streams. Whether you have small JSON files or massive CSV dumps, this tool ensures your data isn't garbage.
🚀 Why Data Constraints?
- Rule-Based: Define your rules in simple JSON/YAML files. No coding required.
- Universal: Works with JSON and CSV out of the box.
- Developer Friendly: Written in JavaScript, typed with TypeScript.
⚡ Basic Use Case (At a glance)
Imagine you have a users.json file and you want to ensure all emails are valid.
1. Your Data (users.json):
[
{ "id": 1, "email": "[email protected]" },
{ "id": 2, "email": "bob-has-no-domain" }
]2. Your Rules (config.json):
{
"sources": {
"users": { "type": "file", "path": "users.json", "format": "json" }
},
"constraints": [
{
"type": "format",
"id": "valid-email",
"service": "users",
"field": "email",
"regex": "^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$",
"message": "Invalid email: {{email}}"
}
]
}3. Run and get results:
$ npx data-constraints validate --config config.json
[INFO] Validating data...
[valid-email] (format) Invalid email: bob-has-no-domain
Validation finished. Found 1 issues.📦 Installation
To use as a CLI globally:
npm install -g data-constraintsOr install locally in your project:
npm install data-constraintsThen you can run npx data-constraints from your project folder.
🧠 Basic Concepts
Data Constraints works with three core files:
- Data Files: Your actual data dumps in
.jsonor.csvformat. - Config File: A JSON file pointing the engine to your data and rules.
- Constraints (Rules): The definitions of what is valid.
Rule Types at a Glance
- 📝 Format: Ensure strings look correct (e.g. Emails).
- Example:
"regex": "^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$"
- Example:
- 🆔 Unique: Ensure no duplicate IDs exist across a file.
- Example:
"field": "employee_id"
- Example:
- 🔗 Foreign Key: Ensure referenced IDs actually exist in another file.
- Example:
order.userIdmust exist inusers.id
- Example:
📚 Documentation
For full documentation, guides and advanced use cases, please check the docs/ directory.
- Key Concepts: Easy-to-understand explanation of file types and constraints.
- User Guide: The comprehensive guide to using the CLI and defining rules.
- Integration Guide: How to integrate the engine programmatically in Node.js.
- Examples: Runnable examples, ranging from simple to e-commerce.
🛠️ Features
| Feature | Description |
| :--- | :--- |
| Format Validation | Regex-based validation for strings (Emails, Phones, Codes). |
| Unique Validation | Ensure IDs and codes are unique across your dataset. |
| Foreign Keys | Validate relationships between different files (e.g. order.userId -> user.id). |
| Multiple Reporters | Output results to Console, JSON, or Markdown files. |
🤝 Contributing
In Data Constraints contributions, bug reports, and feature requests are welcome. If you have ideas, just launch you PRs!
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
