-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate.test.ts
More file actions
149 lines (125 loc) · 3.27 KB
/
validate.test.ts
File metadata and controls
149 lines (125 loc) · 3.27 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
import assert from "node:assert";
import test, { describe } from "node:test";
import { parse, validate } from "./validate.js";
describe("validate", () => {
test("should validate a valid config", () => {
const input = {};
const valid = validate(input);
assert.ok(valid);
assert(validate.errors === null);
});
test(
"given additional property should be invalid",
{ skip: "if/then/else does not work with additionalProperties:false" },
() => {
const input = { foo: 42 };
const valid = validate(input);
assert.ok(!valid);
assert.deepEqual(validate.errors, [
{
instancePath: "",
schemaPath: "#/additionalProperties",
keyword: "additionalProperties",
params: { additionalProperty: "foo" },
message: "must NOT have additional properties",
},
]);
},
);
test("given string should coerce to number", () => {
const input = { sw_ml: true, h: "42" };
validate(input);
assert.ok(typeof input.h === "number");
});
});
describe("parse", () => {
test("given emtpy object should return default config", () => {
const input = {};
const output = parse(input);
const expected = {
t0: output.t0, // dynamic default
h: 200,
theta: 288,
dtheta: 1,
qt: 0.008,
dqt: -0.001,
wtheta: [0.1],
advtheta: 0,
gamma_theta: [0.006],
wq: [0.0001],
advq: 0,
gamma_qt: [0],
divU: 0,
beta: 0.2,
p0: 101300,
z_theta: [5000],
z_qt: [5000],
u: 6,
du: 4,
advu: 0,
gamma_u: [0],
z_u: [5000],
v: -4,
dv: 4,
advv: 0,
gamma_v: [0],
z_v: [5000],
ustar: 0.3,
L: 1000,
d: 10,
h0: 20,
C: 18.6208e6,
Cq: 0,
omega: 0.1,
spread: 1.5,
radiativeLoss: 0.7,
name: "",
description: "",
dt: 60,
runtime: 43200,
sw_ml: true,
sw_wind: false,
sw_fire: false,
sw_dc: false,
t_dc: 13,
};
assert.deepEqual(output, expected);
});
test("given partial config should return full config", () => {
const input = { h: 100, sw_ml: true };
const output = parse(input);
const expected = parse({});
if (!expected.sw_ml) {
throw new Error("sw_ml is enabled");
}
expected.h = 100;
assert.deepEqual(output, expected);
});
test("given partial string config should return full coerced config", () => {
const input = { h: "100", sw_ml: true };
const output = parse(input);
const expected = parse({});
if (!expected.sw_ml) {
throw new Error("sw_ml is enabled");
}
expected.h = 100;
assert.deepEqual(output, expected);
});
test("given emptry string should return default", () => {
const input = { h: "", sw_ml: true };
const output = parse(input);
const expected = parse({});
assert.deepEqual(output, expected);
});
test(
"given additional property should throw",
{ skip: "if/then/else does not work with additionalProperties:false" },
() => {
const input = { foo: 42 };
assert.throws(() => parse(input), {
name: "ValidationError",
message: "Invalid input: data must NOT have additional properties",
});
},
);
});