-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathafter.js
More file actions
46 lines (40 loc) · 1.18 KB
/
Copy pathafter.js
File metadata and controls
46 lines (40 loc) · 1.18 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
// After: using ata-validator
const { Validator } = require("ata-validator");
// Same schemas — no changes needed
const addressSchema = {
$id: "https://example.com/address",
type: "object",
properties: {
street: { type: "string" },
city: { type: "string", minLength: 1 },
zip: { type: "string", pattern: "^\\d{5}$" },
},
required: ["street", "city"],
};
const userSchema = {
type: "object",
properties: {
name: { type: "string", minLength: 1 },
email: { type: "string", format: "email" },
age: { type: "integer", minimum: 0 },
address: { $ref: "https://example.com/address" },
},
required: ["name", "email"],
additionalProperties: false,
};
// One step: schema + references together
const v = new Validator(userSchema, {
schemas: [addressSchema],
});
// Validate
const result = v.validate({
name: "Mert",
email: "mert@example.com",
age: 26,
address: { street: "Main St", city: "Istanbul", zip: "34000" },
});
console.log("valid:", result.valid); // true
console.log("errors:", result.errors); // []
const invalid = v.validate({ name: "", email: "bad" });
console.log("valid:", invalid.valid); // false
console.log("errors:", invalid.errors);