ata-validator 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +275 -0
- package/binding/ata_napi.cpp +744 -0
- package/binding.gyp +33 -0
- package/compat.js +51 -0
- package/deps/simdjson/simdjson.cpp +56221 -0
- package/deps/simdjson/simdjson.h +122784 -0
- package/include/ata.h +89 -0
- package/include/ata_c.h +57 -0
- package/index.js +32 -0
- package/package.json +63 -0
- package/prebuilds/darwin-arm64/ata-validator.node +0 -0
- package/src/ata.cpp +1017 -0
- package/src/ata_c.cpp +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mert Can Altin
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
# ata
|
|
2
|
+
|
|
3
|
+
A blazing-fast C++ JSON Schema validator powered by [simdjson](https://github.com/simdjson/simdjson). Schema compilation 11,000x faster than ajv, JSON validation 2-4x faster. CSP-safe, multi-language, zero JS dependencies.
|
|
4
|
+
|
|
5
|
+
## Performance
|
|
6
|
+
|
|
7
|
+
### Schema Compilation
|
|
8
|
+
|
|
9
|
+
| Validator | ops/sec |
|
|
10
|
+
|---|---|
|
|
11
|
+
| **ata** | **175,548** |
|
|
12
|
+
| ajv | 16 |
|
|
13
|
+
|
|
14
|
+
> ata compiles schemas **11,000x faster** than ajv.
|
|
15
|
+
|
|
16
|
+
### JSON String Validation (real-world scenario)
|
|
17
|
+
|
|
18
|
+
| Payload Size | ata | ajv | Winner |
|
|
19
|
+
|---|---|---|---|
|
|
20
|
+
| 2 KB | 449,447 | 193,181 | **ata 2.3x faster** |
|
|
21
|
+
| 10 KB | 136,301 | 40,644 | **ata 3.4x faster** |
|
|
22
|
+
| 20 KB | 73,142 | 20,459 | **ata 3.6x faster** |
|
|
23
|
+
| 100 KB | 14,388 | 4,062 | **ata 3.5x faster** |
|
|
24
|
+
| 200 KB | 7,590 | 2,021 | **ata 3.8x faster** |
|
|
25
|
+
|
|
26
|
+
> Tested on Apple Silicon. JSON string validation = `JSON.parse()` + `validate()` for ajv vs single `validateJSON()` call for ata. The gap grows with payload size.
|
|
27
|
+
|
|
28
|
+
### JSON Schema Test Suite
|
|
29
|
+
|
|
30
|
+
**97.1%** pass rate on official [JSON Schema Test Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite) (Draft 2020-12).
|
|
31
|
+
|
|
32
|
+
## Features
|
|
33
|
+
|
|
34
|
+
- **Fast**: SIMD-accelerated JSON parsing via simdjson, pre-compiled schemas, cached regex patterns, branchless UTF-8 counting
|
|
35
|
+
- **CSP-Safe**: No `new Function()` or `eval()` — works in strict Content Security Policy environments where ajv cannot
|
|
36
|
+
- **V8 Direct Traversal**: Validates JS objects directly in C++ without `JSON.stringify` overhead
|
|
37
|
+
- **Comprehensive**: Supports JSON Schema Draft 2020-12 keywords including `$ref`, `if/then/else`, `patternProperties`, `prefixItems`, `format`
|
|
38
|
+
- **Multi-Language**: C API (`ata_c.h`) enables bindings for Rust, Python, Go, Ruby, and more
|
|
39
|
+
- **Drop-in Replacement**: ajv-compatible API — switch with one line change
|
|
40
|
+
- **Node.js Binding**: Native N-API addon
|
|
41
|
+
- **Error Details**: Rich error messages with JSON Pointer paths
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
### Node.js
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npm install ata-validator
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### CMake (C++)
|
|
52
|
+
|
|
53
|
+
```cmake
|
|
54
|
+
include(FetchContent)
|
|
55
|
+
FetchContent_Declare(
|
|
56
|
+
ata
|
|
57
|
+
GIT_REPOSITORY https://github.com/mertcanaltin/ata.git
|
|
58
|
+
GIT_TAG main
|
|
59
|
+
)
|
|
60
|
+
FetchContent_MakeAvailable(ata)
|
|
61
|
+
|
|
62
|
+
target_link_libraries(your_target PRIVATE ata::ata)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
### Node.js
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
const { Validator, validate } = require('ata-validator');
|
|
71
|
+
|
|
72
|
+
// Pre-compiled schema (recommended)
|
|
73
|
+
const v = new Validator({
|
|
74
|
+
type: 'object',
|
|
75
|
+
properties: {
|
|
76
|
+
name: { type: 'string', minLength: 1 },
|
|
77
|
+
email: { type: 'string', format: 'email' },
|
|
78
|
+
age: { type: 'integer', minimum: 0 }
|
|
79
|
+
},
|
|
80
|
+
required: ['name', 'email']
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Validate JS objects directly (V8 direct traversal)
|
|
84
|
+
const result = v.validate({ name: 'Mert', email: 'mert@example.com', age: 28 });
|
|
85
|
+
console.log(result.valid); // true
|
|
86
|
+
|
|
87
|
+
// Validate JSON strings (simdjson fast path)
|
|
88
|
+
const r = v.validateJSON('{"name": "Mert", "email": "mert@example.com"}');
|
|
89
|
+
console.log(r.valid); // true
|
|
90
|
+
|
|
91
|
+
// Error details
|
|
92
|
+
const r2 = v.validate({ name: '', age: -1 });
|
|
93
|
+
console.log(r2.errors);
|
|
94
|
+
// [{ code: 4, path: '', message: 'missing required property: email' }, ...]
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Drop-in ajv Replacement
|
|
98
|
+
|
|
99
|
+
```diff
|
|
100
|
+
- const Ajv = require('ajv');
|
|
101
|
+
+ const Ajv = require('ata-validator/compat');
|
|
102
|
+
|
|
103
|
+
const ajv = new Ajv();
|
|
104
|
+
const validate = ajv.compile(schema);
|
|
105
|
+
const valid = validate(data);
|
|
106
|
+
if (!valid) console.log(validate.errors);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### C++
|
|
110
|
+
|
|
111
|
+
```cpp
|
|
112
|
+
#include "ata.h"
|
|
113
|
+
#include <iostream>
|
|
114
|
+
|
|
115
|
+
int main() {
|
|
116
|
+
auto schema = ata::compile(R"({
|
|
117
|
+
"type": "object",
|
|
118
|
+
"properties": {
|
|
119
|
+
"name": {"type": "string"},
|
|
120
|
+
"age": {"type": "integer", "minimum": 0}
|
|
121
|
+
},
|
|
122
|
+
"required": ["name"]
|
|
123
|
+
})");
|
|
124
|
+
|
|
125
|
+
auto result = ata::validate(schema, R"({"name": "Mert", "age": 28})");
|
|
126
|
+
|
|
127
|
+
if (result) {
|
|
128
|
+
std::cout << "Valid!" << std::endl;
|
|
129
|
+
} else {
|
|
130
|
+
for (const auto& err : result.errors) {
|
|
131
|
+
std::cout << err.path << ": " << err.message << std::endl;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return 0;
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### C API
|
|
139
|
+
|
|
140
|
+
```c
|
|
141
|
+
#include "ata_c.h"
|
|
142
|
+
#include <stdio.h>
|
|
143
|
+
#include <string.h>
|
|
144
|
+
|
|
145
|
+
int main(void) {
|
|
146
|
+
const char* schema = "{\"type\":\"string\",\"minLength\":3}";
|
|
147
|
+
ata_schema s = ata_compile(schema, strlen(schema));
|
|
148
|
+
|
|
149
|
+
const char* doc = "\"hello\"";
|
|
150
|
+
ata_result r = ata_validate(s, doc, strlen(doc));
|
|
151
|
+
|
|
152
|
+
if (r.valid) {
|
|
153
|
+
printf("Valid!\n");
|
|
154
|
+
} else {
|
|
155
|
+
for (size_t i = 0; i < r.error_count; i++) {
|
|
156
|
+
ata_string msg = ata_get_error_message(i);
|
|
157
|
+
printf("Error: %.*s\n", (int)msg.length, msg.data);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
ata_schema_free(s);
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Supported Keywords
|
|
167
|
+
|
|
168
|
+
| Category | Keywords |
|
|
169
|
+
|----------|----------|
|
|
170
|
+
| Type | `type` (string, number, integer, boolean, null, array, object, union) |
|
|
171
|
+
| Numeric | `minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum`, `multipleOf` |
|
|
172
|
+
| String | `minLength`, `maxLength`, `pattern`, `format` |
|
|
173
|
+
| Array | `items`, `prefixItems`, `minItems`, `maxItems`, `uniqueItems` |
|
|
174
|
+
| Object | `properties`, `required`, `additionalProperties`, `patternProperties`, `minProperties`, `maxProperties` |
|
|
175
|
+
| Enum/Const | `enum`, `const` |
|
|
176
|
+
| Composition | `allOf`, `anyOf`, `oneOf`, `not` |
|
|
177
|
+
| Conditional | `if`, `then`, `else` |
|
|
178
|
+
| References | `$ref`, `$defs`, `definitions`, `$id` |
|
|
179
|
+
| Boolean | `true` (accept all), `false` (reject all) |
|
|
180
|
+
|
|
181
|
+
### Format Validators
|
|
182
|
+
|
|
183
|
+
`email`, `date`, `date-time`, `time`, `uri`, `uri-reference`, `ipv4`, `ipv6`, `uuid`, `hostname`
|
|
184
|
+
|
|
185
|
+
## Why ata over ajv?
|
|
186
|
+
|
|
187
|
+
| | ata | ajv |
|
|
188
|
+
|---|---|---|
|
|
189
|
+
| Schema compilation | **11,000x faster** | Slow (code generation) |
|
|
190
|
+
| JSON string validation | **2-4x faster** | JSON.parse + validate |
|
|
191
|
+
| CSP compatible | Yes | No (`new Function()`) |
|
|
192
|
+
| Multi-language | C, C++, Rust, Python, Go | JavaScript only |
|
|
193
|
+
| Bundle size | ~20KB JS + native | ~150KB minified |
|
|
194
|
+
| Node.js core candidate | Yes (like ada-url, simdutf) | No (JS dependency) |
|
|
195
|
+
|
|
196
|
+
## Building from Source
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
# C++ library + tests
|
|
200
|
+
cmake -B build
|
|
201
|
+
cmake --build build
|
|
202
|
+
ctest --test-dir build
|
|
203
|
+
|
|
204
|
+
# With benchmarks
|
|
205
|
+
cmake -B build -DATA_BENCHMARKS=ON
|
|
206
|
+
cmake --build build
|
|
207
|
+
./build/ata_bench
|
|
208
|
+
|
|
209
|
+
# Node.js addon
|
|
210
|
+
npm install
|
|
211
|
+
node test.js
|
|
212
|
+
|
|
213
|
+
# Run JSON Schema Test Suite
|
|
214
|
+
node tests/run_suite.js
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Build Options
|
|
218
|
+
|
|
219
|
+
| Option | Default | Description |
|
|
220
|
+
|--------|---------|-------------|
|
|
221
|
+
| `ATA_TESTING` | `ON` | Build test suite |
|
|
222
|
+
| `ATA_BENCHMARKS` | `OFF` | Build benchmarks |
|
|
223
|
+
| `ATA_SANITIZE` | `OFF` | Enable address sanitizer |
|
|
224
|
+
|
|
225
|
+
## API Reference
|
|
226
|
+
|
|
227
|
+
### C++ API
|
|
228
|
+
|
|
229
|
+
#### `ata::compile(schema_json) -> schema_ref`
|
|
230
|
+
Compile a JSON Schema string. Returns a reusable `schema_ref` (falsy on error).
|
|
231
|
+
|
|
232
|
+
#### `ata::validate(schema_ref, json, opts) -> validation_result`
|
|
233
|
+
Validate a JSON string against a pre-compiled schema. Pass `{.all_errors = false}` to stop at first error (faster).
|
|
234
|
+
|
|
235
|
+
#### `ata::validation_result`
|
|
236
|
+
```cpp
|
|
237
|
+
struct validation_result {
|
|
238
|
+
bool valid;
|
|
239
|
+
std::vector<validation_error> errors;
|
|
240
|
+
explicit operator bool() const noexcept { return valid; }
|
|
241
|
+
};
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Node.js API
|
|
245
|
+
|
|
246
|
+
#### `new Validator(schema)`
|
|
247
|
+
Create a validator with a pre-compiled schema. `schema` can be an object or JSON string.
|
|
248
|
+
|
|
249
|
+
#### `validator.validate(data) -> { valid, errors }`
|
|
250
|
+
Validate any JS value directly via V8 traversal (no serialization).
|
|
251
|
+
|
|
252
|
+
#### `validator.validateJSON(jsonString) -> { valid, errors }`
|
|
253
|
+
Validate a JSON string via simdjson (fastest path for string input).
|
|
254
|
+
|
|
255
|
+
#### `validate(schema, data) -> { valid, errors }`
|
|
256
|
+
One-shot validation without pre-compilation.
|
|
257
|
+
|
|
258
|
+
### ajv-compatible API (`compat.js`)
|
|
259
|
+
|
|
260
|
+
```javascript
|
|
261
|
+
const Ata = require('ata-validator/compat');
|
|
262
|
+
const ata = new Ata();
|
|
263
|
+
const validate = ata.compile(schema);
|
|
264
|
+
const valid = validate(data);
|
|
265
|
+
if (!valid) console.log(validate.errors);
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## License
|
|
269
|
+
|
|
270
|
+
Licensed under either of
|
|
271
|
+
|
|
272
|
+
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
273
|
+
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
274
|
+
|
|
275
|
+
at your option.
|