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/include/ata.h
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cstdint>
|
|
4
|
+
#include <memory>
|
|
5
|
+
#include <optional>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <string_view>
|
|
8
|
+
#include <variant>
|
|
9
|
+
#include <vector>
|
|
10
|
+
|
|
11
|
+
namespace ata {
|
|
12
|
+
|
|
13
|
+
inline constexpr uint32_t VERSION_MAJOR = 0;
|
|
14
|
+
inline constexpr uint32_t VERSION_MINOR = 1;
|
|
15
|
+
inline constexpr uint32_t VERSION_REVISION = 0;
|
|
16
|
+
|
|
17
|
+
inline constexpr std::string_view version() noexcept {
|
|
18
|
+
return "0.1.0";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
enum class error_code : uint8_t {
|
|
22
|
+
ok = 0,
|
|
23
|
+
invalid_json,
|
|
24
|
+
invalid_schema,
|
|
25
|
+
type_mismatch,
|
|
26
|
+
required_property_missing,
|
|
27
|
+
additional_property_not_allowed,
|
|
28
|
+
enum_mismatch,
|
|
29
|
+
const_mismatch,
|
|
30
|
+
minimum_violation,
|
|
31
|
+
maximum_violation,
|
|
32
|
+
exclusive_minimum_violation,
|
|
33
|
+
exclusive_maximum_violation,
|
|
34
|
+
min_length_violation,
|
|
35
|
+
max_length_violation,
|
|
36
|
+
pattern_mismatch,
|
|
37
|
+
format_mismatch,
|
|
38
|
+
min_items_violation,
|
|
39
|
+
max_items_violation,
|
|
40
|
+
unique_items_violation,
|
|
41
|
+
min_properties_violation,
|
|
42
|
+
max_properties_violation,
|
|
43
|
+
multiple_of_violation,
|
|
44
|
+
all_of_failed,
|
|
45
|
+
any_of_failed,
|
|
46
|
+
one_of_failed,
|
|
47
|
+
not_failed,
|
|
48
|
+
ref_not_found,
|
|
49
|
+
if_then_else_failed,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
struct validation_error {
|
|
53
|
+
error_code code;
|
|
54
|
+
std::string path;
|
|
55
|
+
std::string message;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
struct validation_result {
|
|
59
|
+
bool valid;
|
|
60
|
+
std::vector<validation_error> errors;
|
|
61
|
+
|
|
62
|
+
explicit operator bool() const noexcept { return valid; }
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
struct compiled_schema;
|
|
66
|
+
|
|
67
|
+
struct schema_ref {
|
|
68
|
+
std::shared_ptr<compiled_schema> impl;
|
|
69
|
+
|
|
70
|
+
explicit operator bool() const noexcept { return impl != nullptr; }
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
struct validate_options {
|
|
74
|
+
bool all_errors = true; // false = stop at first error (faster)
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// Compile a JSON Schema string into an internal representation.
|
|
78
|
+
schema_ref compile(std::string_view schema_json);
|
|
79
|
+
|
|
80
|
+
// Validate a JSON document against a compiled schema.
|
|
81
|
+
validation_result validate(const schema_ref& schema, std::string_view json,
|
|
82
|
+
const validate_options& opts = {});
|
|
83
|
+
|
|
84
|
+
// Validate a JSON document against a schema (compiles schema each time).
|
|
85
|
+
validation_result validate(std::string_view schema_json,
|
|
86
|
+
std::string_view json,
|
|
87
|
+
const validate_options& opts = {});
|
|
88
|
+
|
|
89
|
+
} // namespace ata
|
package/include/ata_c.h
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#ifndef ATA_C_H
|
|
2
|
+
#define ATA_C_H
|
|
3
|
+
|
|
4
|
+
#include <stdbool.h>
|
|
5
|
+
#include <stddef.h>
|
|
6
|
+
#include <stdint.h>
|
|
7
|
+
|
|
8
|
+
#ifdef __cplusplus
|
|
9
|
+
extern "C" {
|
|
10
|
+
#endif
|
|
11
|
+
|
|
12
|
+
typedef struct ata_schema_s* ata_schema;
|
|
13
|
+
|
|
14
|
+
typedef struct {
|
|
15
|
+
const char* data;
|
|
16
|
+
size_t length;
|
|
17
|
+
} ata_string;
|
|
18
|
+
|
|
19
|
+
typedef struct {
|
|
20
|
+
bool valid;
|
|
21
|
+
size_t error_count;
|
|
22
|
+
} ata_result;
|
|
23
|
+
|
|
24
|
+
typedef struct {
|
|
25
|
+
uint32_t major;
|
|
26
|
+
uint32_t minor;
|
|
27
|
+
uint32_t revision;
|
|
28
|
+
} ata_version_components;
|
|
29
|
+
|
|
30
|
+
// Compile a JSON Schema. Returns NULL on failure.
|
|
31
|
+
ata_schema ata_compile(const char* schema_json, size_t length);
|
|
32
|
+
|
|
33
|
+
// Free a compiled schema. NULL-safe.
|
|
34
|
+
void ata_schema_free(ata_schema schema);
|
|
35
|
+
|
|
36
|
+
// Validate JSON against a compiled schema.
|
|
37
|
+
ata_result ata_validate(ata_schema schema, const char* json, size_t length);
|
|
38
|
+
|
|
39
|
+
// Validate JSON against a schema string (compiles each time).
|
|
40
|
+
ata_result ata_validate_oneshot(const char* schema_json, size_t schema_length,
|
|
41
|
+
const char* json, size_t json_length);
|
|
42
|
+
|
|
43
|
+
// Get error message at index from last validation.
|
|
44
|
+
ata_string ata_get_error_message(size_t index);
|
|
45
|
+
|
|
46
|
+
// Get error path at index from last validation.
|
|
47
|
+
ata_string ata_get_error_path(size_t index);
|
|
48
|
+
|
|
49
|
+
// Version info
|
|
50
|
+
const char* ata_get_version(void);
|
|
51
|
+
ata_version_components ata_get_version_components(void);
|
|
52
|
+
|
|
53
|
+
#ifdef __cplusplus
|
|
54
|
+
}
|
|
55
|
+
#endif
|
|
56
|
+
|
|
57
|
+
#endif // ATA_C_H
|
package/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const native = require("node-gyp-build")(__dirname);
|
|
2
|
+
|
|
3
|
+
class Validator {
|
|
4
|
+
constructor(schema) {
|
|
5
|
+
const schemaStr =
|
|
6
|
+
typeof schema === "string" ? schema : JSON.stringify(schema);
|
|
7
|
+
this._compiled = new native.CompiledSchema(schemaStr);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Validates data directly — no JSON.stringify overhead
|
|
11
|
+
// Accepts JS objects, arrays, strings, numbers, booleans, null
|
|
12
|
+
validate(data) {
|
|
13
|
+
return this._compiled.validate(data);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Force JSON string path (simdjson parse + validate)
|
|
17
|
+
validateJSON(jsonStr) {
|
|
18
|
+
return this._compiled.validateJSON(jsonStr);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function validate(schema, data) {
|
|
23
|
+
const schemaStr =
|
|
24
|
+
typeof schema === "string" ? schema : JSON.stringify(schema);
|
|
25
|
+
return native.validate(schemaStr, data);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function version() {
|
|
29
|
+
return native.version();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = { Validator, validate, version };
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ata-validator",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Ultra-fast JSON Schema validator powered by simdjson. 11,000x faster schema compilation, 2-4x faster validation than ajv.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"install": "node-gyp-build",
|
|
8
|
+
"build": "node-gyp rebuild",
|
|
9
|
+
"prebuild": "prebuildify --napi --strip",
|
|
10
|
+
"prebuild-all": "prebuildify --napi --strip --arch x64 && prebuildify --napi --strip --arch arm64",
|
|
11
|
+
"test": "node test.js",
|
|
12
|
+
"test:suite": "node tests/run_suite.js",
|
|
13
|
+
"test:compat": "node tests/test_compat.js",
|
|
14
|
+
"bench": "node benchmark/bench_large.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"json",
|
|
18
|
+
"json-schema",
|
|
19
|
+
"schema",
|
|
20
|
+
"validator",
|
|
21
|
+
"validation",
|
|
22
|
+
"fast",
|
|
23
|
+
"native",
|
|
24
|
+
"simdjson",
|
|
25
|
+
"napi",
|
|
26
|
+
"ajv",
|
|
27
|
+
"ajv-alternative"
|
|
28
|
+
],
|
|
29
|
+
"author": "Mert Can Altin <mertcanaltin01@gmail.com>",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/mertcanaltin/ata.git"
|
|
34
|
+
},
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/mertcanaltin/ata/issues"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/mertcanaltin/ata#readme",
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=18.0.0"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"index.js",
|
|
44
|
+
"compat.js",
|
|
45
|
+
"binding.gyp",
|
|
46
|
+
"binding/",
|
|
47
|
+
"include/",
|
|
48
|
+
"src/",
|
|
49
|
+
"deps/",
|
|
50
|
+
"prebuilds/",
|
|
51
|
+
"README.md",
|
|
52
|
+
"LICENSE"
|
|
53
|
+
],
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"node-addon-api": "^8.0.0"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"node-gyp": "^11.0.0",
|
|
59
|
+
"node-gyp-build": "^4.8.4",
|
|
60
|
+
"prebuildify": "^6.0.1"
|
|
61
|
+
},
|
|
62
|
+
"gypfile": true
|
|
63
|
+
}
|
|
Binary file
|