-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest.js
More file actions
289 lines (241 loc) · 9.16 KB
/
Copy pathtest.js
File metadata and controls
289 lines (241 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
const { Validator, validate, version } = require("./index");
let passed = 0;
let failed = 0;
function test(name, fn) {
try {
fn();
console.log(` PASS ${name}`);
passed++;
} catch (e) {
console.log(` FAIL ${name}: ${e.message}`);
failed++;
}
}
function assert(cond, msg) {
if (!cond) throw new Error(msg || "assertion failed");
}
console.log(`\nata v${version()} - Node.js Binding Tests\n`);
// --- Validator class ---
test("Validator: valid document", () => {
const v = new Validator({ type: "string" });
const r = v.validate("hello");
assert(r.valid, "should be valid");
});
test("Validator: invalid document", () => {
const v = new Validator({ type: "string" });
const r = v.validate(42);
assert(!r.valid, "should be invalid");
assert(r.errors.length > 0, "should have errors");
});
test("Validator: complex schema", () => {
const v = new Validator({
type: "object",
properties: {
name: { type: "string", minLength: 1 },
age: { type: "integer", minimum: 0 },
},
required: ["name"],
});
const r1 = v.validate({ name: "Mert", age: 25 });
assert(r1.valid, "valid doc should pass");
const r2 = v.validate({ age: -1 });
assert(!r2.valid, "missing required should fail");
});
test("Validator: accepts JS objects", () => {
const v = new Validator({ type: "object" });
const r = v.validate({ key: "value" });
assert(r.valid, "should accept JS object");
});
// --- One-shot validate ---
test("validate(): one-shot valid", () => {
const r = validate({ type: "number" }, 42);
assert(r.valid);
});
test("validate(): one-shot invalid", () => {
const r = validate({ type: "number" }, "hello");
assert(!r.valid);
});
// --- Format validation ---
test("format: email", () => {
const v = new Validator({ type: "string", format: "email" });
assert(v.validate("user@example.com").valid);
assert(!v.validate("not-email").valid);
});
test("format: date", () => {
const v = new Validator({ type: "string", format: "date" });
assert(v.validate("2026-03-21").valid);
assert(!v.validate("nope").valid);
});
// --- Error details ---
test("error details include path and message", () => {
const v = new Validator({
type: "object",
properties: {
name: { type: "string" },
age: { type: "integer" },
},
});
const r = v.validate({ name: 123, age: "old" });
assert(!r.valid);
assert(r.errors.length >= 2, "should have at least 2 errors");
assert(r.errors.some((e) => e.instancePath.includes("name")));
assert(r.errors.some((e) => e.instancePath.includes("age")));
});
// --- Schema reuse ---
test("schema reuse across validations", () => {
const v = new Validator({ type: "string", maxLength: 5 });
assert(v.validate("hi").valid);
assert(v.validate("hello").valid);
assert(!v.validate("toolong").valid);
assert(!v.validate(42).valid);
});
// --- V8 Fast API: isValid ---
test("isValid: valid buffer", () => {
const v = new Validator({ type: "object", properties: { id: { type: "integer" } }, required: ["id"] });
assert(v.isValid(Buffer.from('{"id":1}')) === true, "should be valid");
});
test("isValid: invalid buffer", () => {
const v = new Validator({ type: "object", properties: { id: { type: "integer" } }, required: ["id"] });
assert(v.isValid(Buffer.from('{"id":"not_int"}')) === false, "should be invalid");
});
test("isValid: string input", () => {
const v = new Validator({ type: "string" });
assert(v.isValid('"hello"') === true, "should accept string input");
assert(v.isValid('42') === false, "should reject non-string");
});
test("isValid: empty object missing required", () => {
const v = new Validator({ type: "object", required: ["name"] });
assert(v.isValid(Buffer.from('{}')) === false, "should fail required");
});
test("isValid: invalid JSON", () => {
const v = new Validator({ type: "object" });
assert(v.isValid(Buffer.from('{bad json}')) === false, "should reject bad JSON");
});
test("isValid: complex schema", () => {
const v = new Validator({
type: "object",
properties: {
name: { type: "string", minLength: 1 },
age: { type: "integer", minimum: 0, maximum: 150 },
email: { type: "string", format: "email" },
},
required: ["name", "age"],
});
assert(v.isValid(Buffer.from('{"name":"Mert","age":26,"email":"m@e.com"}')) === true);
assert(v.isValid(Buffer.from('{"name":"","age":26}')) === false, "minLength");
assert(v.isValid(Buffer.from('{"name":"Mert","age":-1}')) === false, "minimum");
assert(v.isValid(Buffer.from('{"name":"Mert"}')) === false, "required age");
});
test("isValid: Uint8Array input", () => {
const v = new Validator({ type: "number" });
const buf = Buffer.from("42");
const u8 = new Uint8Array(buf.buffer, buf.byteOffset, buf.length);
assert(v.isValid(u8) === true, "should accept Uint8Array");
});
test("isValid: rejects parsed object", () => {
const v = new Validator({ type: "object", required: ["id"] });
let threw = false;
try { v.isValid({ id: 1 }); } catch (e) { threw = e instanceof TypeError && /Buffer/.test(e.message); }
assert(threw, "should throw TypeError pointing to Buffer/Uint8Array/string");
});
test("countValid: rejects non-buffer", () => {
const v = new Validator({ type: "number" });
let threw = false;
try { v.countValid({}); } catch (e) { threw = e instanceof TypeError; }
assert(threw, "should throw TypeError on plain object");
});
test("batchIsValid: rejects non-buffer element", () => {
const v = new Validator({ type: "object", required: ["id"] });
let threw = false;
try { v.batchIsValid([Buffer.from('{"id":1}'), { id: 2 }]); } catch (e) { threw = e instanceof TypeError; }
assert(threw, "should throw TypeError when an element isn't a Buffer/Uint8Array");
});
test("isValid: string enum (on-demand fast path)", () => {
const v = new Validator({
type: "object",
properties: { role: { enum: ["admin", "user", "moderator"] } },
required: ["role"],
});
const long = "a".repeat(100); // > 32 byte threshold for OD path
assert(v.isValid(Buffer.from(`{"role":"admin","_pad":"${long}"}`)) === true);
assert(v.isValid(Buffer.from(`{"role":"user","_pad":"${long}"}`)) === true);
assert(v.isValid(Buffer.from(`{"role":"hacker","_pad":"${long}"}`)) === false);
});
test("isValid: number enum", () => {
const v = new Validator({
type: "object",
properties: { code: { enum: [1, 2, 3] } },
required: ["code"],
});
const long = "a".repeat(100);
assert(v.isValid(Buffer.from(`{"code":1,"_pad":"${long}"}`)) === true);
assert(v.isValid(Buffer.from(`{"code":7,"_pad":"${long}"}`)) === false);
});
test("isValid: mixed enum with null", () => {
const v = new Validator({ enum: ["a", null, true] });
// small payloads use DOM path, but should still be correct
assert(v.isValid(Buffer.from('"a"')) === true);
assert(v.isValid(Buffer.from('null')) === true);
assert(v.isValid(Buffer.from('true')) === true);
assert(v.isValid(Buffer.from('"b"')) === false);
assert(v.isValid(Buffer.from('false')) === false);
});
test("isValid: object enum falls back to DOM correctly", () => {
const v = new Validator({
type: "object",
properties: { tag: { enum: [{ a: 1 }, { b: 2 }] } },
required: ["tag"],
});
const long = "a".repeat(100);
assert(v.isValid(Buffer.from(`{"tag":{"a":1},"_pad":"${long}"}`)) === true);
assert(v.isValid(Buffer.from(`{"tag":{"a":2},"_pad":"${long}"}`)) === false);
});
// --- V8 Fast API: countValid ---
test("countValid: NDJSON buffer", () => {
const v = new Validator({ type: "object", required: ["id"] });
const ndjson = Buffer.from('{"id":1}\n{"id":2}\n{"bad":true}\n');
assert(v.countValid(ndjson) === 2, "should count 2 valid");
});
test("countValid: all valid", () => {
const v = new Validator({ type: "number" });
const ndjson = Buffer.from("1\n2\n3\n");
assert(v.countValid(ndjson) === 3, "all 3 valid");
});
test("countValid: all invalid", () => {
const v = new Validator({ type: "number" });
const ndjson = Buffer.from('"a"\n"b"\n');
assert(v.countValid(ndjson) === 0, "none valid");
});
test("countValid: empty buffer", () => {
const v = new Validator({ type: "number" });
assert(v.countValid(Buffer.alloc(0)) === 0, "empty = 0");
});
test("countValid: string input", () => {
const v = new Validator({ type: "number" });
assert(v.countValid("1\n2\n3\n") === 3, "should accept string");
});
// --- V8 Fast API: batchIsValid ---
test("batchIsValid: mixed buffers", () => {
const v = new Validator({ type: "object", required: ["id"] });
const bufs = [
Buffer.from('{"id":1}'),
Buffer.from('{"nope":true}'),
Buffer.from('{"id":3}'),
];
assert(v.batchIsValid(bufs) === 2, "2 of 3 valid");
});
test("batchIsValid: empty array", () => {
const v = new Validator({ type: "number" });
assert(v.batchIsValid([]) === 0, "empty = 0");
});
test("batchIsValid: single buffer", () => {
const v = new Validator({ type: "string" });
assert(v.batchIsValid([Buffer.from('"hello"')]) === 1, "1 valid");
});
test("batchIsValid: all invalid", () => {
const v = new Validator({ type: "integer" });
const bufs = [Buffer.from('"a"'), Buffer.from('"b"')];
assert(v.batchIsValid(bufs) === 0, "none valid");
});
console.log(`\n${passed}/${passed + failed} tests passed.\n`);
process.exit(failed > 0 ? 1 : 0);