-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.test.js
72 lines (59 loc) · 2.58 KB
/
debug.test.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
64
65
66
67
68
69
70
71
72
import assert from 'node:assert';
import process from 'node:process';
import { Guard, Validators, exit } from './dist/index.js';
process.on('exit', (code) => {
console.log(`About to exit with code: ${code}`);
exit();
});
async function main() {
try {
const objRailStr = `
<rail version="0.1">
<output>
<list name="user_orders" description="Generate a list of user, and how many orders they have placed in the past." format="hub://guardrails/valid_length: 1 10" on-fail-length="fix">
<object>
<string name="user_id" description="The user's id." />
<string name="user_name" description="The user's first name and last name" format="two-words" on-fail-two-words="reask" />
<integer name="num_orders" description="The number of orders the user has placed" format="valid-range: 0 50" on-fail-valid-range="fix" />
<date name="last_order_date" description="Date of last order" />
<string name="portal_url" description="The url for the portal where the user submitted their order." format="valid-url" on-fail-valid-url="reask" />
<string name="user_middle_name" description="The users middle name." format="hub://guardrails/valid_length: 1" on-fail-length="filter" />
<string name="account_is_private" description="The users middle name." format="valid-choices: {['false']}" on-fail-valid-choices="refrain" />
</object>
</list>
</output>
<prompt>
Generate a dataset of fake user orders. Each row of the dataset should be valid.
\${gr.complete_json_suffix}
</prompt>
</rail>
`;
const objRailStrGuard = await Guard.fromRailString(objRailStr);
const invalid = {
"user_orders": [
{
"user_id": "Mock_User_Id",
"user_name": "Mock User Name",
"num_orders": 51,
"last_order_date": "2023-06-28",
"portal_url": "not a url",
// "portal_url": "http://mock-portal.com",
"user_middle_name": "Mockerson",
"account_is_private": "true"
}
]
};
const response = await objRailStrGuard.parse(
JSON.stringify(invalid)
);
console.log('response: ', JSON.stringify(response, null, 2));
console.log('Call.validationOutput', JSON.stringify(objRailStrGuard.history.first.validationOutput, null, 2));
console.log('Call.fixedOutput', JSON.stringify(objRailStrGuard.history.first.fixedOutput, null, 2));
assert.equal(response?.validationPassed, false);
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
}
await main();