-
-
Notifications
You must be signed in to change notification settings - Fork 588
/
Copy pathvalidator-async.js
63 lines (58 loc) · 1.2 KB
/
validator-async.js
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
const ServiceBroker = require("../src/service-broker");
const broker = new ServiceBroker({
validator: {
type: "Fastest",
options: {
useNewCustomCheckerFunction: true,
messages: {
invalidOwner: "Invalid user ID in '{field}'!"
}
}
},
tracing: {
enabled: true,
exporter: "Console"
}
});
broker.createService({
name: "users",
actions: {
isValid: {
params: {
id: "number"
},
async handler(ctx) {
return !!(ctx.params.id % 2);
}
}
}
});
broker.createService({
name: "posts",
actions: {
create: {
params: {
$$async: true,
title: "string",
owner: {
type: "number",
custom: async (value, errors, schema, name, parent, context) => {
const ctx = context.meta;
const res = await ctx.call("users.isValid", { id: value });
if (res !== true) errors.push({ type: "invalidOwner", actual: value });
return value;
}
}
},
handler(ctx) {
return `Post created for owner '${ctx.params.owner}'`;
}
}
}
});
broker
.start()
.then(() => broker.repl())
.then(() => broker.call("posts.create", { title: "Post #1", owner: 2 }))
.then(res => broker.logger.info("Result:", res))
.catch(err => broker.logger.error(err));