Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #54 - Array of Object validation #65

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x, 21.x]
node-version: [18.x, 20.x, 22.x, 23.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -24,7 +24,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x, 21.x]
node-version: [18.x, 20.x, 22.x, 23.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -42,7 +42,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 20.x ]
node-version: [ 22.x ]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## [2.2.0 (2025-01-13)](https://github.com/axe-api/axe-api/compare/2.2.0...2.2.1)

- Array and object validation [#54](https://github.com/axe-api/validator/issues/54)

## [2.1.1 (2024-10-27)](https://github.com/axe-api/axe-api/compare/2.1.1...2.1.0)

- TypeError: list.map is not a function [#62](https://github.com/axe-api/validator/issues/62)
Expand Down
87 changes: 87 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,90 @@ By the example, you would get the following response:
}
}
```

## Nested data validation

This feature allows dynamic traversal of nested data structures, supporting complex validation rules for paths like `users.*.addresses.*.city`.

It is inspired by Laravel's validation system and works seamlessly with arrays and objects, including deeply nested data.

```ts
import { validate, setLocales, en } from "robust-validator";

setLocales(en);

const data = {
secret: "some secret",
users: [
{
addresses: [
{
city: "New York",
},
{
city: "Istanbul",
},
],
},
{
addresses: [
{
city: "New York",
},
{
street: "Wall Street",
},
],
},
],
permissons: {
read: true,
write: true,
},
};

const definition = {
secret: "required|min:100",
"users.*.addresses.*.city": "required",
"permissons.read": "required|boolean",
"permissons.delete": "required|boolean",
};

const result = await validate(data, definition);
console.log(result);
```

And this is the content of the `result` variable:

```json
{
"isValid": false,
"isInvalid": true,
"fields": {
"secret": false,
"users.*.addresses.*.city": false,
"permissons.read": true,
"permissons.delete": false
},
"errors": {
"secret": [
{
"rule": "min",
"message": "The field must be at least 100."
}
],
"users.1.addresses.1.city": [
{
"rule": "required",
"message": "The field is required."
}
],
"permissons.delete": [
{
"rule": "required",
"message": "The field is required."
}
]
}
}
```
7 changes: 5 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ hero:
name: "Robust Validator"
text: "Rule-based data validation in JS"
tagline: Extendable, function-oriented, i18n-supported
image:
src: /logo.png
alt: Robust Validator
actions:
- theme: brand
text: Getting started
Expand All @@ -15,9 +18,9 @@ features:
- title: Declarative ✍🏽
details: Declarative rule definition allows you to save your rules in different places such as configuration files, databases, etc.
- title: Simple 🐤
details: Starting to validate data is very fast instead of creating complicated validation rules. You just need seconds.
details: Starting to validate data is very fast. Instead of creating complicated validation rules, you just need seconds.
- title: Proof of work 💪
details: Laravel-ish data validation rules are well-tested as a concept. This library is just another implementation for JavaScript.
- title: i18n 🇺🇳
details: Multi-language error messages are supported internally, unlike other libraries. It provides consistency.
details: Multi-language error messages are supported internally, unlike other libraries.
---
Binary file added docs/public/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "robust-validator",
"version": "2.1.1",
"version": "2.2.0",
"description": "Rule-based data validation library",
"type": "module",
"main": "dist/index.cjs",
Expand Down
24 changes: 23 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
# Robust Validator
#

<h1 align="center">
<br>
<a href="https://validator.axe-api.com/">
<img src="https://validator.axe-api.com/logo.png" width="200">
</a>
<br>
Robust Validator
<br>
<a href="https://badge.fury.io/js/robust-validator">
<img src="https://badge.fury.io/js/robust-validator.svg" alt="npm version" height="18">
</a>
<a href="https://github.com/axe-api/validator/actions/workflows/npm-release-publish.yml" target="_blank">
<img src="https://github.com/axe-api/validator/actions/workflows/npm-release-publish.yml/badge.svg?branch=master">
</a>
<a href="https://github.com/axe-api/validator/issues" target="_blank">
<img src="https://img.shields.io/github/issues/axe-api/validator.svg">
</a>
<a href="https://opensource.org/licenses/MIT" target="_blank">
<img src="https://img.shields.io/badge/license-MIT-blue.svg">
</a>
</h1>

Rule-based data validation in JS. Extendable, function-oriented, i18n-supported

Expand Down
11 changes: 11 additions & 0 deletions src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ export interface ILocale {
key: LanguageType;
values: Translation;
}

export interface ITraversePair {
path: string;
value: any;
}

export interface ITraverseItem {
path: string;
rules: string | string[];
resolved: Array<ITraversePair>;
}
Loading
Loading