teptris 0.3.19.1 → 0.3.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Steepfile +1 -0
- data/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/schema.h +107 -0
- data/ext/teptris_ext/engine/src/include/teptris/version.h +2 -2
- data/ext/teptris_ext/engine/src/teptris/schema/formats.c +656 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.c +440 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.h +34 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.c +1090 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.h +25 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema.c +734 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema_internal.h +221 -0
- data/ext/teptris_ext/engine/src/teptris/schema/validate.c +1034 -0
- data/ext/teptris_ext/materialize.c +87 -1
- data/lib/teptris/schema.rb +64 -0
- data/lib/teptris/version.rb +1 -1
- data/lib/teptris.rb +1 -0
- data/sig/teptris.rbs +32 -0
- metadata +12 -2
|
@@ -0,0 +1,1034 @@
|
|
|
1
|
+
/* The validation walk: a compiled schema against a converted
|
|
2
|
+
* document. One tagged-value conversion per validate call (validation
|
|
3
|
+
* is a separate API; the parse hot path is untouched), then a single
|
|
4
|
+
* recursive walk collecting every error, up to 256, as a JSON array. */
|
|
5
|
+
#include <math.h>
|
|
6
|
+
#include <stdarg.h>
|
|
7
|
+
#include <stdio.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <string.h>
|
|
10
|
+
|
|
11
|
+
#include "schema_internal.h"
|
|
12
|
+
#include "teptris/teptris.h"
|
|
13
|
+
|
|
14
|
+
#define MAX_ERRORS 256
|
|
15
|
+
#define MAX_DEPTH 512
|
|
16
|
+
|
|
17
|
+
typedef struct {
|
|
18
|
+
teptris_arena *scratch;
|
|
19
|
+
char *buf;
|
|
20
|
+
size_t n, cap;
|
|
21
|
+
int n_errors;
|
|
22
|
+
int mute; /* trial validations: walk, but record nothing */
|
|
23
|
+
int pings; /* every verr fire, muted or not - the trial signal */
|
|
24
|
+
int ref_depth; /* $ref hops in flight (cycle guard) */
|
|
25
|
+
/* TOML path stack */
|
|
26
|
+
char *segs[128];
|
|
27
|
+
int depth;
|
|
28
|
+
/* schema-path stack */
|
|
29
|
+
const char *ssegs[64];
|
|
30
|
+
char sseg_buf[64][64];
|
|
31
|
+
int sdepth;
|
|
32
|
+
int truncated;
|
|
33
|
+
} vctx;
|
|
34
|
+
|
|
35
|
+
/* ---------------------------------------------------------- output -- */
|
|
36
|
+
|
|
37
|
+
static void out_put(vctx *vc, const char *s, size_t len) {
|
|
38
|
+
if (vc->n + len + 1 > vc->cap) {
|
|
39
|
+
vc->cap = vc->cap == 0 ? 256 : vc->cap * 2;
|
|
40
|
+
while (vc->n + len + 1 > vc->cap) vc->cap *= 2;
|
|
41
|
+
char *nb = realloc(vc->buf, vc->cap);
|
|
42
|
+
if (nb == NULL) return;
|
|
43
|
+
vc->buf = nb;
|
|
44
|
+
}
|
|
45
|
+
memcpy(vc->buf + vc->n, s, len);
|
|
46
|
+
vc->n += len;
|
|
47
|
+
vc->buf[vc->n] = '\0';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static void out_json_escaped(vctx *vc, const char *s) {
|
|
51
|
+
for (const char *p = s; *p != '\0'; p++) {
|
|
52
|
+
if (*p == '"' || *p == '\\') {
|
|
53
|
+
out_put(vc, "\\", 1);
|
|
54
|
+
out_put(vc, p, 1);
|
|
55
|
+
} else if ((unsigned char)*p < 0x20) {
|
|
56
|
+
char esc[8];
|
|
57
|
+
snprintf(esc, sizeof esc, "\\u%04x", (unsigned char)*p);
|
|
58
|
+
out_put(vc, esc, 6);
|
|
59
|
+
} else {
|
|
60
|
+
out_put(vc, p, 1);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* ------------------------------------------------------------- path -- */
|
|
66
|
+
|
|
67
|
+
static bool plain_key(const char *k) {
|
|
68
|
+
if (k[0] == '\0') return false;
|
|
69
|
+
for (const char *p = k; *p != '\0'; p++) {
|
|
70
|
+
char ch = *p;
|
|
71
|
+
if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
|
|
72
|
+
!(ch >= '0' && ch <= '9') && ch != '_' && ch != '-') {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static void path_push(vctx *vc, const char *seg) {
|
|
80
|
+
if (vc->depth >= 128) return;
|
|
81
|
+
size_t len = strlen(seg);
|
|
82
|
+
char *copy = teptris_arena_alloc(vc->scratch, len + 3);
|
|
83
|
+
if (plain_key(seg)) {
|
|
84
|
+
memcpy(copy, seg, len + 1);
|
|
85
|
+
} else {
|
|
86
|
+
copy[0] = '"';
|
|
87
|
+
memcpy(copy + 1, seg, len);
|
|
88
|
+
copy[len + 1] = '"';
|
|
89
|
+
copy[len + 2] = '\0';
|
|
90
|
+
}
|
|
91
|
+
vc->segs[vc->depth++] = copy;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static void path_push_idx(vctx *vc, long idx) {
|
|
95
|
+
if (vc->depth >= 128) return;
|
|
96
|
+
char *copy = teptris_arena_alloc(vc->scratch, 24);
|
|
97
|
+
snprintf(copy, 24, "%ld", idx);
|
|
98
|
+
vc->segs[vc->depth++] = copy;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static void path_pop(vctx *vc) {
|
|
102
|
+
if (vc->depth > 0) vc->depth--;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/* the materialized TOML path as one JSON string: "a.b.\"c.d\".0"
|
|
106
|
+
* (segments are pre-formatted: bare when plain, quoted otherwise) */
|
|
107
|
+
static void out_path(vctx *vc) {
|
|
108
|
+
out_put(vc, "\"", 1);
|
|
109
|
+
for (int i = 0; i < vc->depth; i++) {
|
|
110
|
+
if (i > 0) out_put(vc, ".", 1);
|
|
111
|
+
out_json_escaped(vc, vc->segs[i]);
|
|
112
|
+
}
|
|
113
|
+
out_put(vc, "\"", 1);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static void spush(vctx *vc, const char *seg) {
|
|
117
|
+
if (vc->sdepth >= 64) return;
|
|
118
|
+
snprintf(vc->sseg_buf[vc->sdepth], sizeof vc->sseg_buf[vc->sdepth], "%s",
|
|
119
|
+
seg);
|
|
120
|
+
vc->ssegs[vc->sdepth] = vc->sseg_buf[vc->sdepth];
|
|
121
|
+
vc->sdepth++;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static void spush_named(vctx *vc, const char *kind, const char *name) {
|
|
125
|
+
if (vc->sdepth >= 64) return;
|
|
126
|
+
snprintf(vc->sseg_buf[vc->sdepth], sizeof vc->sseg_buf[vc->sdepth],
|
|
127
|
+
"%s.%s", kind, name);
|
|
128
|
+
vc->ssegs[vc->sdepth] = vc->sseg_buf[vc->sdepth];
|
|
129
|
+
vc->sdepth++;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
static void spop(vctx *vc) {
|
|
133
|
+
if (vc->sdepth > 0) vc->sdepth--;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
static void out_schema_path(vctx *vc) {
|
|
137
|
+
out_put(vc, "\"#", 2);
|
|
138
|
+
for (int i = 0; i < vc->sdepth; i++) {
|
|
139
|
+
out_put(vc, "/", 1);
|
|
140
|
+
out_json_escaped(vc, vc->ssegs[i]);
|
|
141
|
+
}
|
|
142
|
+
out_put(vc, "\"", 1);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static void verr(vctx *vc, const char *fmt, ...) {
|
|
146
|
+
vc->pings++;
|
|
147
|
+
if (vc->mute > 0) return;
|
|
148
|
+
if (vc->n_errors >= MAX_ERRORS) {
|
|
149
|
+
vc->truncated = 1;
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (vc->n_errors > 0) out_put(vc, ",", 1);
|
|
153
|
+
out_put(vc, "{\"path\":", 8);
|
|
154
|
+
out_path(vc);
|
|
155
|
+
out_put(vc, ",\"schemaPath\":", 14);
|
|
156
|
+
out_schema_path(vc);
|
|
157
|
+
out_put(vc, ",\"message\":\"", 12);
|
|
158
|
+
char msg[256];
|
|
159
|
+
va_list ap;
|
|
160
|
+
va_start(ap, fmt);
|
|
161
|
+
vsnprintf(msg, sizeof msg, fmt, ap);
|
|
162
|
+
va_end(ap);
|
|
163
|
+
for (const char *p = msg; *p != '\0'; p++) {
|
|
164
|
+
if (*p == '"' || *p == '\\') out_put(vc, "\\", 1);
|
|
165
|
+
out_put(vc, p, 1);
|
|
166
|
+
}
|
|
167
|
+
out_put(vc, "\"}", 2);
|
|
168
|
+
vc->n_errors++;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/* --------------------------------------------------- value equality -- */
|
|
172
|
+
|
|
173
|
+
static bool sv_equal(const sch_svalue *a, const sch_svalue *b) {
|
|
174
|
+
if (a == NULL || b == NULL) return a == b;
|
|
175
|
+
if (a->kind != b->kind) {
|
|
176
|
+
/* policy 3: a TOML datetime compares with a schema-side string
|
|
177
|
+
* by canonical text (JSON cannot carry a datetime literal, so
|
|
178
|
+
* enum/const entries for datetimes are strings); a date never
|
|
179
|
+
* equals a datetime text */
|
|
180
|
+
int ad = a->kind >= SV_DATETIME;
|
|
181
|
+
int bd = b->kind >= SV_DATETIME;
|
|
182
|
+
if ((ad || bd) && (a->kind == SV_STRING || b->kind == SV_STRING)) {
|
|
183
|
+
const sch_svalue *dt = ad ? a : b;
|
|
184
|
+
const sch_svalue *st = ad ? b : a;
|
|
185
|
+
if (dt->str_len == st->str_len &&
|
|
186
|
+
memcmp(dt->str, st->str, dt->str_len) == 0) {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
/* taplo re-formats offset datetimes with the time crate,
|
|
190
|
+
* which spells UTC as "+00:00" where the canonical TOML
|
|
191
|
+
* form is "Z"; both spellings name the same instant, so
|
|
192
|
+
* const/enum entries match either way */
|
|
193
|
+
if (dt->kind == SV_DATETIME && dt->str_len > 0 &&
|
|
194
|
+
dt->str[dt->str_len - 1] == 'Z' && st->str_len > 6 &&
|
|
195
|
+
memcmp(st->str + st->str_len - 6, "+00:00", 6) == 0 &&
|
|
196
|
+
dt->str_len - 1 == st->str_len - 6 &&
|
|
197
|
+
memcmp(dt->str, st->str, st->str_len - 6) == 0) {
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
return false; /* type-strict (policy 2) */
|
|
203
|
+
}
|
|
204
|
+
switch (a->kind) {
|
|
205
|
+
case SV_STRING:
|
|
206
|
+
return a->str_len == b->str_len && memcmp(a->str, b->str, a->str_len) == 0;
|
|
207
|
+
case SV_INT: return a->i == b->i;
|
|
208
|
+
case SV_FLOAT: return a->f == b->f;
|
|
209
|
+
case SV_BOOL: return a->b == b->b;
|
|
210
|
+
case SV_NULL: return true;
|
|
211
|
+
case SV_DATETIME: case SV_DATE: case SV_TIME:
|
|
212
|
+
return a->str_len == b->str_len && memcmp(a->str, b->str, a->str_len) == 0;
|
|
213
|
+
case SV_ARRAY:
|
|
214
|
+
if (a->n_items != b->n_items) return false;
|
|
215
|
+
for (size_t i = 0; i < a->n_items; i++) {
|
|
216
|
+
if (!sv_equal(a->items[i], b->items[i])) return false;
|
|
217
|
+
}
|
|
218
|
+
return true;
|
|
219
|
+
case SV_TABLE:
|
|
220
|
+
if (a->n_members != b->n_members) return false;
|
|
221
|
+
for (size_t i = 0; i < a->n_members; i++) {
|
|
222
|
+
sch_svalue *other = NULL;
|
|
223
|
+
for (size_t k = 0; k < b->n_members; k++) {
|
|
224
|
+
if (strcmp(b->members[k].key, a->members[i].key) == 0) {
|
|
225
|
+
other = b->members[k].val;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (other == NULL) return false;
|
|
230
|
+
if (!sv_equal(a->members[i].val, other)) return false;
|
|
231
|
+
}
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/* --------------------------------------------------------- the walk -- */
|
|
238
|
+
|
|
239
|
+
static sch_svalue *jv_to_sv(vctx *vc, jv *v);
|
|
240
|
+
static void val(vctx *vc, sch_node *n, const sch_svalue *v);
|
|
241
|
+
|
|
242
|
+
static bool passes(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
243
|
+
int save_pings = vc->pings;
|
|
244
|
+
size_t save_n = vc->n;
|
|
245
|
+
int save_depth = vc->sdepth;
|
|
246
|
+
vc->mute++;
|
|
247
|
+
val(vc, n, v);
|
|
248
|
+
vc->mute--;
|
|
249
|
+
if (vc->pings == save_pings) {
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
/* discard the trial's errors: rewind the buffer to save_n */
|
|
253
|
+
vc->n = save_n;
|
|
254
|
+
if (vc->buf != NULL) vc->buf[vc->n] = '\0';
|
|
255
|
+
vc->sdepth = save_depth;
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
static size_t cp_count(const char *s, size_t len) {
|
|
260
|
+
size_t count = 0;
|
|
261
|
+
size_t i = 0;
|
|
262
|
+
while (i < len) {
|
|
263
|
+
unsigned char b = (unsigned char)s[i];
|
|
264
|
+
size_t step = b < 0x80 ? 1
|
|
265
|
+
: b < 0xE0 ? 2
|
|
266
|
+
: b < 0xF0 ? 3
|
|
267
|
+
: 4;
|
|
268
|
+
i += step > len - i ? 1 : step;
|
|
269
|
+
count++;
|
|
270
|
+
}
|
|
271
|
+
return count;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/* RFC 3339 shapes for the TOML datetime kinds live in formats.c; the
|
|
275
|
+
* walk only decides whether a kind participates in a format (policy
|
|
276
|
+
* 3) and hands plain strings to the format battery */
|
|
277
|
+
static bool format_ok(vctx *vc, int format, const sch_svalue *v) {
|
|
278
|
+
if (format == FMT_NONE) return true;
|
|
279
|
+
if (v->kind == SV_DATE) return format == FMT_DATE;
|
|
280
|
+
if (v->kind == SV_TIME) return format == FMT_TIME;
|
|
281
|
+
if (v->kind == SV_DATETIME) return format == FMT_DATETIME;
|
|
282
|
+
if (v->kind != SV_STRING) return format == FMT_NONE;
|
|
283
|
+
return sch_format_string_check(format, v->str, v->str_len, vc->scratch);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/* --------------------------------------------------------- content -- */
|
|
287
|
+
|
|
288
|
+
static bool str_is_base64(const char *s, size_t len) {
|
|
289
|
+
if (len == 0) return true; /* "" decodes to empty (the crate's take) */
|
|
290
|
+
if (len % 4 != 0) return false;
|
|
291
|
+
size_t pad = 0;
|
|
292
|
+
for (size_t i = 0; i < len; i++) {
|
|
293
|
+
char c = s[i];
|
|
294
|
+
bool alnum = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
|
|
295
|
+
(c >= '0' && c <= '9');
|
|
296
|
+
if (!(alnum || c == '+' || c == '/' || c == '=')) return false;
|
|
297
|
+
if (c == '=') {
|
|
298
|
+
pad++;
|
|
299
|
+
} else if (pad > 0) {
|
|
300
|
+
return false; /* padding only at the end */
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return pad <= 2;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
static int b64_val(char c) {
|
|
307
|
+
if (c >= 'A' && c <= 'Z') return c - 'A';
|
|
308
|
+
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
|
309
|
+
if (c >= '0' && c <= '9') return c - '0' + 52;
|
|
310
|
+
if (c == '+') return 62;
|
|
311
|
+
if (c == '/') return 63;
|
|
312
|
+
return -1;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/* contentEncoding/contentMediaType, the taplo parity pair: base64 is
|
|
316
|
+
* the one encoding checked (and decoded for the media step),
|
|
317
|
+
* application/json the one media type parsed; unknown names on either
|
|
318
|
+
* side are no-ops, exactly like the crate's map misses */
|
|
319
|
+
static void check_content(vctx *vc, const sch_node *n, const sch_svalue *v) {
|
|
320
|
+
if (v->kind != SV_STRING) return;
|
|
321
|
+
const char *text = v->str;
|
|
322
|
+
size_t tlen = v->str_len;
|
|
323
|
+
bool b64 = n->content_encoding != NULL &&
|
|
324
|
+
strcmp(n->content_encoding, "base64") == 0;
|
|
325
|
+
if (b64 && !str_is_base64(text, tlen)) {
|
|
326
|
+
verr(vc, "string is not valid base64 (contentEncoding)");
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if (n->content_media_type == NULL ||
|
|
330
|
+
strcmp(n->content_media_type, "application/json") != 0) {
|
|
331
|
+
return; /* unknown media names: annotation */
|
|
332
|
+
}
|
|
333
|
+
const char *payload = text;
|
|
334
|
+
size_t plen = tlen;
|
|
335
|
+
if (b64) {
|
|
336
|
+
size_t dn = tlen / 4 * 3;
|
|
337
|
+
if (tlen > 0 && text[tlen - 1] == '=') dn--;
|
|
338
|
+
if (tlen > 1 && text[tlen - 2] == '=') dn--;
|
|
339
|
+
char *out = teptris_arena_alloc(vc->scratch, dn + 1);
|
|
340
|
+
size_t o = 0;
|
|
341
|
+
int acc = 0, bits = 0;
|
|
342
|
+
for (size_t i = 0; i < tlen && o < dn; i++) {
|
|
343
|
+
if (text[i] == '=') break;
|
|
344
|
+
int d = b64_val(text[i]);
|
|
345
|
+
if (d < 0) break;
|
|
346
|
+
acc = (acc << 6) | d;
|
|
347
|
+
bits += 6;
|
|
348
|
+
if (bits >= 8) {
|
|
349
|
+
bits -= 8;
|
|
350
|
+
out[o++] = (char)((acc >> bits) & 0xFF);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
/* the media check runs on the decoded text only when it is
|
|
354
|
+
* UTF-8 (the crate skips the check otherwise) */
|
|
355
|
+
bool utf8 = true;
|
|
356
|
+
for (size_t i = 0; i < o && utf8; i++) {
|
|
357
|
+
unsigned char b = (unsigned char)out[i];
|
|
358
|
+
if (b < 0x80) continue;
|
|
359
|
+
int need = b < 0xE0 ? 1 : b < 0xF0 ? 2 : 3;
|
|
360
|
+
if (i + need >= o || (b & 0xC0) != 0xC0) utf8 = false;
|
|
361
|
+
for (int k = 1; k <= need && utf8; k++) {
|
|
362
|
+
if (((unsigned char)out[i + k] & 0xC0) != 0x80) utf8 = false;
|
|
363
|
+
}
|
|
364
|
+
i += need;
|
|
365
|
+
}
|
|
366
|
+
if (!utf8) return;
|
|
367
|
+
out[o] = '\0';
|
|
368
|
+
payload = out;
|
|
369
|
+
plen = o;
|
|
370
|
+
}
|
|
371
|
+
char jerr[64] = {0};
|
|
372
|
+
if (json_read(vc->scratch, payload, plen, jerr, sizeof jerr) == NULL) {
|
|
373
|
+
verr(vc, "string is not valid JSON (contentMediaType)");
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/* ------------------------------------------------------ multipleOf -- */
|
|
378
|
+
|
|
379
|
+
/* value / multipleOf decided exactly on the decimal representations:
|
|
380
|
+
* value = vm * 10^ve, mult = mm * 10^me, and the ratio is an integer
|
|
381
|
+
* iff the mantissas divide once the 2s and 5s are moved across —
|
|
382
|
+
* plain int64 arithmetic, so it runs identically where __int128 does
|
|
383
|
+
* not exist (MSVC) and needs no floating division at all */
|
|
384
|
+
static bool sch_mult_ok(const sch_node *n, const sch_svalue *v) {
|
|
385
|
+
if (v->kind == SV_INT && n->mult_is_int) {
|
|
386
|
+
return n->imult != 0 && v->i % n->imult == 0;
|
|
387
|
+
}
|
|
388
|
+
double fv = v->kind == SV_INT ? (double)v->i : v->f;
|
|
389
|
+
if (!(fv == fv) || fv == INFINITY || fv == -INFINITY) {
|
|
390
|
+
return false; /* nan/inf are never multiples */
|
|
391
|
+
}
|
|
392
|
+
int64_t vm, mm;
|
|
393
|
+
int ve, me;
|
|
394
|
+
if (v->kind == SV_INT) {
|
|
395
|
+
vm = v->i;
|
|
396
|
+
ve = 0;
|
|
397
|
+
} else if (!sch_decimal_of_double(v->f, &vm, &ve)) {
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
if (n->mult_is_int) {
|
|
401
|
+
mm = n->imult;
|
|
402
|
+
me = 0;
|
|
403
|
+
} else if (n->mult_exact) {
|
|
404
|
+
mm = n->mult_mant;
|
|
405
|
+
me = n->mult_exp;
|
|
406
|
+
} else {
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
if (mm == 0) return false;
|
|
410
|
+
|
|
411
|
+
/* strip the 2s and 5s out of the multiplier; the remainder is
|
|
412
|
+
* coprime to 10, so it must divide the mantissa itself */
|
|
413
|
+
int b = 0, c = 0;
|
|
414
|
+
while (mm % 2 == 0) {
|
|
415
|
+
mm /= 2;
|
|
416
|
+
b++;
|
|
417
|
+
}
|
|
418
|
+
while (mm % 5 == 0) {
|
|
419
|
+
mm /= 5;
|
|
420
|
+
c++;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/* the numerator vm * 10^k (k = ve - me) carries 2^(k+v2) * 5^(k+v5) */
|
|
424
|
+
int k = ve - me;
|
|
425
|
+
uint64_t mag = vm < 0 ? (uint64_t)(-(vm + 1)) + 1 : (uint64_t)vm;
|
|
426
|
+
int u2 = 0, u5 = 0;
|
|
427
|
+
uint64_t m = mag;
|
|
428
|
+
while (m % 2 == 0 && u2 < 64) {
|
|
429
|
+
m /= 2;
|
|
430
|
+
u2++;
|
|
431
|
+
}
|
|
432
|
+
m = mag;
|
|
433
|
+
while (m % 5 == 0 && u5 < 64) {
|
|
434
|
+
m /= 5;
|
|
435
|
+
u5++;
|
|
436
|
+
}
|
|
437
|
+
if (k + u2 < b || k + u5 < c) return false;
|
|
438
|
+
return mag % (uint64_t)mm == 0;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
static int sv_to_st(const sch_svalue *v) {
|
|
442
|
+
switch (v->kind) {
|
|
443
|
+
case SV_STRING: return ST_STRING;
|
|
444
|
+
case SV_INT: return ST_INTEGER | ST_NUMBER;
|
|
445
|
+
case SV_FLOAT: return ST_NUMBER;
|
|
446
|
+
case SV_BOOL: return ST_BOOLEAN;
|
|
447
|
+
case SV_TABLE: return ST_OBJECT;
|
|
448
|
+
case SV_ARRAY: return ST_ARRAY;
|
|
449
|
+
case SV_DATETIME: case SV_DATE: case SV_TIME: return ST_STRING;
|
|
450
|
+
default: return 0;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
455
|
+
if (n == NULL || v == NULL) return;
|
|
456
|
+
if (vc->depth >= MAX_DEPTH) {
|
|
457
|
+
verr(vc, "schema depth exceeded");
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (n->ref != NULL) {
|
|
462
|
+
/* a $ref cycle that consumes no document structure (a schema
|
|
463
|
+
* that is its own ref target) would loop forever; legal
|
|
464
|
+
* cycles descend the document on every lap and stay shallow */
|
|
465
|
+
if (vc->ref_depth >= MAX_DEPTH) {
|
|
466
|
+
verr(vc, "cyclic $ref recursion");
|
|
467
|
+
} else {
|
|
468
|
+
vc->ref_depth++;
|
|
469
|
+
spush(vc, "$ref");
|
|
470
|
+
val(vc, n->ref, v);
|
|
471
|
+
spop(vc);
|
|
472
|
+
vc->ref_depth--;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (n->is_false) {
|
|
477
|
+
verr(vc, "no value is allowed here (the false schema)");
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/* type */
|
|
482
|
+
if (n->types != ST_ANY) {
|
|
483
|
+
int bits = sv_to_st(v);
|
|
484
|
+
if ((n->types & (unsigned)bits) == 0) {
|
|
485
|
+
const char *found = v->kind == SV_TABLE ? "table"
|
|
486
|
+
: v->kind == SV_ARRAY ? "array"
|
|
487
|
+
: v->kind == SV_STRING ? "string"
|
|
488
|
+
: v->kind == SV_INT ? "integer"
|
|
489
|
+
: v->kind == SV_FLOAT ? "float"
|
|
490
|
+
: v->kind == SV_BOOL ? "boolean"
|
|
491
|
+
: "value";
|
|
492
|
+
verr(vc, "expected a matching type, found %s", found);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/* enum / const */
|
|
497
|
+
if (n->has_const && n->const_val != NULL) {
|
|
498
|
+
sch_svalue *cv = NULL;
|
|
499
|
+
/* convert lazily into the scratch arena */
|
|
500
|
+
cv = jv_to_sv(vc, n->const_val);
|
|
501
|
+
if (!sv_equal(cv, v)) {
|
|
502
|
+
verr(vc, "value does not equal the constant");
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (n->n_enum > 0) {
|
|
506
|
+
bool hit = false;
|
|
507
|
+
for (size_t i = 0; i < n->n_enum && !hit; i++) {
|
|
508
|
+
sch_svalue *ev = jv_to_sv(vc, n->enum_vals[i]);
|
|
509
|
+
if (sv_equal(ev, v)) hit = true;
|
|
510
|
+
}
|
|
511
|
+
if (!hit) verr(vc, "value is not one of the %zu enum entries",
|
|
512
|
+
n->n_enum);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/* numeric */
|
|
516
|
+
bool is_num = v->kind == SV_INT || v->kind == SV_FLOAT;
|
|
517
|
+
if (is_num) {
|
|
518
|
+
double fv = v->kind == SV_INT ? (double)v->i : v->f;
|
|
519
|
+
if (n->has_min) {
|
|
520
|
+
bool ok = v->kind == SV_INT && n->fmin == (double)(int64_t)n->fmin
|
|
521
|
+
? v->i >= n->imin
|
|
522
|
+
: fv >= n->fmin;
|
|
523
|
+
if (!ok) verr(vc, "value is below the minimum");
|
|
524
|
+
}
|
|
525
|
+
if (n->has_max) {
|
|
526
|
+
bool ok = v->kind == SV_INT && n->fmax == (double)(int64_t)n->fmax
|
|
527
|
+
? v->i <= n->imax
|
|
528
|
+
: fv <= n->fmax;
|
|
529
|
+
if (!ok) verr(vc, "value is above the maximum");
|
|
530
|
+
}
|
|
531
|
+
if (n->has_ex_min) {
|
|
532
|
+
bool ok = v->kind == SV_INT && n->fex_min == (double)(int64_t)n->fex_min
|
|
533
|
+
? v->i > n->iex_min
|
|
534
|
+
: fv > n->fex_min;
|
|
535
|
+
if (!ok) verr(vc, "value is not above the exclusive minimum");
|
|
536
|
+
}
|
|
537
|
+
if (n->has_ex_max) {
|
|
538
|
+
bool ok = v->kind == SV_INT && n->fex_max == (double)(int64_t)n->fex_max
|
|
539
|
+
? v->i < n->iex_max
|
|
540
|
+
: fv < n->fex_max;
|
|
541
|
+
if (!ok) verr(vc, "value is not below the exclusive maximum");
|
|
542
|
+
}
|
|
543
|
+
if (n->has_mult) {
|
|
544
|
+
bool ok = sch_mult_ok(n, v);
|
|
545
|
+
if (!ok) verr(vc, "value is not a multiple of multipleOf");
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/* string */
|
|
550
|
+
if (v->kind == SV_STRING || v->kind == SV_DATE || v->kind == SV_TIME ||
|
|
551
|
+
v->kind == SV_DATETIME) {
|
|
552
|
+
if (n->has_min_length &&
|
|
553
|
+
(long)cp_count(v->str, v->str_len) < n->min_length) {
|
|
554
|
+
verr(vc, "string is shorter than minLength");
|
|
555
|
+
}
|
|
556
|
+
if (n->has_max_length &&
|
|
557
|
+
(long)cp_count(v->str, v->str_len) > n->max_length) {
|
|
558
|
+
verr(vc, "string is longer than maxLength");
|
|
559
|
+
}
|
|
560
|
+
if (n->pattern != NULL &&
|
|
561
|
+
!sch_regex_is_match(n->pattern, v->str, v->str_len)) {
|
|
562
|
+
verr(vc, "string does not match the pattern");
|
|
563
|
+
}
|
|
564
|
+
if (n->format != FMT_NONE && !format_ok(vc, n->format, v)) {
|
|
565
|
+
verr(vc, "string does not match the format");
|
|
566
|
+
}
|
|
567
|
+
check_content(vc, n, v);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/* object */
|
|
571
|
+
if (v->kind == SV_TABLE) {
|
|
572
|
+
if (n->has_min_props && (long)v->n_members < n->min_props) {
|
|
573
|
+
verr(vc, "fewer properties than minProperties");
|
|
574
|
+
}
|
|
575
|
+
if (n->has_max_props && (long)v->n_members > n->max_props) {
|
|
576
|
+
verr(vc, "more properties than maxProperties");
|
|
577
|
+
}
|
|
578
|
+
for (size_t r = 0; r < n->n_req; r++) {
|
|
579
|
+
bool found = false;
|
|
580
|
+
for (size_t m = 0; m < v->n_members && !found; m++) {
|
|
581
|
+
if (strcmp(v->members[m].key, n->required[r]) == 0) found = true;
|
|
582
|
+
}
|
|
583
|
+
if (!found) {
|
|
584
|
+
verr(vc, "missing required property '%s'", n->required[r]);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
for (size_t m = 0; m < v->n_members; m++) {
|
|
588
|
+
const char *key = v->members[m].key;
|
|
589
|
+
sch_svalue *mv = v->members[m].val;
|
|
590
|
+
bool matched_known = false;
|
|
591
|
+
if (n->n_props > 0) {
|
|
592
|
+
for (size_t p = 0; p < n->n_props; p++) {
|
|
593
|
+
if (strcmp(n->props[p].name, key) == 0) {
|
|
594
|
+
matched_known = true;
|
|
595
|
+
spush_named(vc, "properties", key);
|
|
596
|
+
path_push(vc, key);
|
|
597
|
+
val(vc, n->props[p].schema, mv);
|
|
598
|
+
path_pop(vc);
|
|
599
|
+
spop(vc);
|
|
600
|
+
break;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
for (size_t p = 0; p < n->n_pprops; p++) {
|
|
605
|
+
if (sch_regex_is_match(n->pprops[p].re, key, strlen(key))) {
|
|
606
|
+
matched_known = true;
|
|
607
|
+
spush(vc, "patternProperties");
|
|
608
|
+
path_push(vc, key);
|
|
609
|
+
val(vc, n->pprops[p].schema, mv);
|
|
610
|
+
path_pop(vc);
|
|
611
|
+
spop(vc);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if (!matched_known && n->additional != NULL) {
|
|
615
|
+
spush(vc, "additionalProperties");
|
|
616
|
+
path_push(vc, key);
|
|
617
|
+
if (n->additional->is_false) {
|
|
618
|
+
verr(vc, "unknown property '%s' (additionalProperties is false)",
|
|
619
|
+
key);
|
|
620
|
+
} else {
|
|
621
|
+
val(vc, n->additional, mv);
|
|
622
|
+
}
|
|
623
|
+
path_pop(vc);
|
|
624
|
+
spop(vc);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
if (n->prop_names != NULL) {
|
|
628
|
+
for (size_t m = 0; m < v->n_members; m++) {
|
|
629
|
+
sch_svalue key_sv;
|
|
630
|
+
memset(&key_sv, 0, sizeof key_sv);
|
|
631
|
+
key_sv.kind = SV_STRING;
|
|
632
|
+
key_sv.str = v->members[m].key;
|
|
633
|
+
key_sv.str_len = strlen(v->members[m].key);
|
|
634
|
+
spush(vc, "propertyNames");
|
|
635
|
+
if (!passes(vc, n->prop_names, &key_sv)) {
|
|
636
|
+
verr(vc, "property name '%s' fails propertyNames",
|
|
637
|
+
v->members[m].key);
|
|
638
|
+
}
|
|
639
|
+
spop(vc);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
for (size_t d = 0; d < n->n_dep_req; d++) {
|
|
643
|
+
bool present = false;
|
|
644
|
+
for (size_t m = 0; m < v->n_members && !present; m++) {
|
|
645
|
+
if (strcmp(v->members[m].key, n->dep_req[d].key) == 0) present = true;
|
|
646
|
+
}
|
|
647
|
+
if (!present) continue;
|
|
648
|
+
sch_node *fake = n->dep_req[d].schema;
|
|
649
|
+
for (size_t r = 0; r < fake->n_req; r++) {
|
|
650
|
+
bool found = false;
|
|
651
|
+
for (size_t m = 0; m < v->n_members && !found; m++) {
|
|
652
|
+
if (strcmp(v->members[m].key, fake->required[r]) == 0) found = true;
|
|
653
|
+
}
|
|
654
|
+
if (!found) {
|
|
655
|
+
verr(vc, "'%s' requires '%s' (dependentRequired)",
|
|
656
|
+
n->dep_req[d].key, fake->required[r]);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
for (size_t d = 0; d < n->n_dep_schemas; d++) {
|
|
661
|
+
bool present = false;
|
|
662
|
+
for (size_t m = 0; m < v->n_members && !present; m++) {
|
|
663
|
+
if (strcmp(v->members[m].key, n->dep_schemas[d].key) == 0) {
|
|
664
|
+
present = true;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
if (!present) continue;
|
|
668
|
+
if (!passes(vc, n->dep_schemas[d].schema, v)) {
|
|
669
|
+
verr(vc, "the '%s' dependency schema does not hold",
|
|
670
|
+
n->dep_schemas[d].key);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/* array */
|
|
676
|
+
if (v->kind == SV_ARRAY) {
|
|
677
|
+
if (n->has_min_items && (long)v->n_items < n->min_items) {
|
|
678
|
+
verr(vc, "fewer items than minItems");
|
|
679
|
+
}
|
|
680
|
+
if (n->has_max_items && (long)v->n_items > n->max_items) {
|
|
681
|
+
verr(vc, "more items than maxItems");
|
|
682
|
+
}
|
|
683
|
+
if (n->unique_items && n->has_unique) {
|
|
684
|
+
for (size_t i = 0; i < v->n_items; i++) {
|
|
685
|
+
for (size_t k = i + 1; k < v->n_items; k++) {
|
|
686
|
+
if (sv_equal(v->items[i], v->items[k])) {
|
|
687
|
+
verr(vc, "array items are not unique (items %zu and %zu)",
|
|
688
|
+
i, k);
|
|
689
|
+
i = v->n_items; /* one report is enough */
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
if (n->n_prefix > 0 || (n->items_set && n->items != NULL)) {
|
|
696
|
+
for (size_t i = 0; i < v->n_items; i++) {
|
|
697
|
+
if (i < n->n_prefix) {
|
|
698
|
+
spush(vc, "prefixItems");
|
|
699
|
+
path_push_idx(vc, (long)i);
|
|
700
|
+
val(vc, n->prefix[i], v->items[i]);
|
|
701
|
+
path_pop(vc);
|
|
702
|
+
spop(vc);
|
|
703
|
+
} else if (n->items != NULL) {
|
|
704
|
+
spush(vc, "items");
|
|
705
|
+
path_push_idx(vc, (long)i);
|
|
706
|
+
val(vc, n->items, v->items[i]);
|
|
707
|
+
path_pop(vc);
|
|
708
|
+
spop(vc);
|
|
709
|
+
} else if (n->add_items != NULL) {
|
|
710
|
+
spush(vc, "additionalItems");
|
|
711
|
+
path_push_idx(vc, (long)i);
|
|
712
|
+
if (n->add_items->is_false) {
|
|
713
|
+
verr(vc, "array has items beyond the tuple "
|
|
714
|
+
"(additionalItems is false)");
|
|
715
|
+
} else {
|
|
716
|
+
val(vc, n->add_items, v->items[i]);
|
|
717
|
+
}
|
|
718
|
+
path_pop(vc);
|
|
719
|
+
spop(vc);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
} else if (n->add_items != NULL) {
|
|
723
|
+
/* additionalItems with no tuple: every position */
|
|
724
|
+
for (size_t i = 0; i < v->n_items; i++) {
|
|
725
|
+
spush(vc, "additionalItems");
|
|
726
|
+
path_push_idx(vc, (long)i);
|
|
727
|
+
if (n->add_items->is_false) {
|
|
728
|
+
verr(vc, "array has items beyond the tuple "
|
|
729
|
+
"(additionalItems is false)");
|
|
730
|
+
} else {
|
|
731
|
+
val(vc, n->add_items, v->items[i]);
|
|
732
|
+
}
|
|
733
|
+
path_pop(vc);
|
|
734
|
+
spop(vc);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
if (n->has_contains && n->contains != NULL) {
|
|
738
|
+
long hits = 0;
|
|
739
|
+
for (size_t i = 0; i < v->n_items; i++) {
|
|
740
|
+
if (passes(vc, n->contains, v->items[i])) hits++;
|
|
741
|
+
}
|
|
742
|
+
long min = n->has_min_contains ? n->min_contains : 1;
|
|
743
|
+
long max = n->has_max_contains ? n->max_contains : (long)v->n_items;
|
|
744
|
+
if (hits < min) verr(vc, "fewer matching items than minContains");
|
|
745
|
+
if (hits > max) verr(vc, "more matching items than maxContains");
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/* applicators */
|
|
750
|
+
if (n->n_all > 0) {
|
|
751
|
+
for (size_t i = 0; i < n->n_all; i++) {
|
|
752
|
+
spush(vc, "allOf");
|
|
753
|
+
val(vc, n->all_of[i], v);
|
|
754
|
+
spop(vc);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
if (n->n_any > 0) {
|
|
758
|
+
bool ok = false;
|
|
759
|
+
for (size_t i = 0; i < n->n_any && !ok; i++) {
|
|
760
|
+
if (passes(vc, n->any_of[i], v)) ok = true;
|
|
761
|
+
}
|
|
762
|
+
if (!ok) verr(vc, "does not match any of the %zu anyOf schemas",
|
|
763
|
+
n->n_any);
|
|
764
|
+
}
|
|
765
|
+
if (n->n_one > 0) {
|
|
766
|
+
int hits = 0;
|
|
767
|
+
for (size_t i = 0; i < n->n_one; i++) {
|
|
768
|
+
if (passes(vc, n->one_of[i], v)) hits++;
|
|
769
|
+
}
|
|
770
|
+
if (hits != 1) {
|
|
771
|
+
verr(vc, "matches %d of the %zu oneOf schemas (exactly one required)",
|
|
772
|
+
hits, n->n_one);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
if (n->_not != NULL) {
|
|
776
|
+
if (passes(vc, n->_not, v)) {
|
|
777
|
+
verr(vc, "must not match the not-schema");
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
if (n->if_s != NULL) {
|
|
781
|
+
bool cond = passes(vc, n->if_s, v);
|
|
782
|
+
if (cond && n->then_s != NULL) {
|
|
783
|
+
spush(vc, "then");
|
|
784
|
+
val(vc, n->then_s, v);
|
|
785
|
+
spop(vc);
|
|
786
|
+
} else if (!cond && n->else_s != NULL) {
|
|
787
|
+
spush(vc, "else");
|
|
788
|
+
val(vc, n->else_s, v);
|
|
789
|
+
spop(vc);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/* -------------------------------------------------- DOM conversion -- */
|
|
795
|
+
|
|
796
|
+
static sch_svalue *node_to_sv(teptris_arena *scratch, const teptris_node *node);
|
|
797
|
+
|
|
798
|
+
static sch_svalue *sv_new(teptris_arena *a, int kind) {
|
|
799
|
+
sch_svalue *v = teptris_arena_alloc(a, sizeof(sch_svalue));
|
|
800
|
+
*v = (sch_svalue){ .kind = kind };
|
|
801
|
+
return v;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
static sch_svalue *node_to_sv(teptris_arena *scratch, const teptris_node *node) {
|
|
805
|
+
int kind = teptris_node_kind(node);
|
|
806
|
+
switch (kind) {
|
|
807
|
+
case TEPTRIS_STRING: {
|
|
808
|
+
sch_svalue *v = sv_new(scratch, SV_STRING);
|
|
809
|
+
teptris_view view;
|
|
810
|
+
if (teptris_node_string(node, &view) == TEPTRIS_OK) {
|
|
811
|
+
v->str = view.ptr;
|
|
812
|
+
v->str_len = view.len;
|
|
813
|
+
}
|
|
814
|
+
return v;
|
|
815
|
+
}
|
|
816
|
+
case TEPTRIS_INTEGER: {
|
|
817
|
+
sch_svalue *v = sv_new(scratch, SV_INT);
|
|
818
|
+
teptris_node_integer(node, &v->i);
|
|
819
|
+
return v;
|
|
820
|
+
}
|
|
821
|
+
case TEPTRIS_FLOAT: {
|
|
822
|
+
sch_svalue *v = sv_new(scratch, SV_FLOAT);
|
|
823
|
+
teptris_node_float(node, &v->f);
|
|
824
|
+
return v;
|
|
825
|
+
}
|
|
826
|
+
case TEPTRIS_BOOLEAN: {
|
|
827
|
+
sch_svalue *v = sv_new(scratch, SV_BOOL);
|
|
828
|
+
teptris_node_boolean(node, &v->b);
|
|
829
|
+
return v;
|
|
830
|
+
}
|
|
831
|
+
case TEPTRIS_DATETIME_OFFSET:
|
|
832
|
+
case TEPTRIS_DATETIME_LOCAL: {
|
|
833
|
+
sch_svalue *v = sv_new(scratch, SV_DATETIME);
|
|
834
|
+
teptris_datetime dt;
|
|
835
|
+
teptris_node_datetime(node, &dt);
|
|
836
|
+
/* the RFC 3339 text: equality + format checks run on the
|
|
837
|
+
* canonical text the emitter would produce */
|
|
838
|
+
char *buf = teptris_arena_alloc(scratch, 64);
|
|
839
|
+
int n = snprintf(buf, 64, "%04d-%02d-%02dT%02d:%02d:%02d", dt.year,
|
|
840
|
+
dt.month, dt.day, dt.hour, dt.minute, dt.second);
|
|
841
|
+
if (dt.nanosecond > 0) {
|
|
842
|
+
n += snprintf(buf + n, 20, ".%09u", dt.nanosecond);
|
|
843
|
+
}
|
|
844
|
+
if (kind == TEPTRIS_DATETIME_OFFSET) {
|
|
845
|
+
if (dt.offset_seconds == 0) {
|
|
846
|
+
n += snprintf(buf + n, 8, "Z");
|
|
847
|
+
} else {
|
|
848
|
+
int sign = dt.offset_seconds < 0 ? -1 : 1;
|
|
849
|
+
int off = dt.offset_seconds * sign;
|
|
850
|
+
snprintf(buf + n, 12, "%c%02d:%02d", sign < 0 ? '-' : '+',
|
|
851
|
+
off / 3600, (off % 3600) / 60);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
v->str = buf;
|
|
855
|
+
v->str_len = (size_t)n;
|
|
856
|
+
return v;
|
|
857
|
+
}
|
|
858
|
+
case TEPTRIS_DATE_LOCAL: {
|
|
859
|
+
sch_svalue *v = sv_new(scratch, SV_DATE);
|
|
860
|
+
teptris_datetime dt;
|
|
861
|
+
teptris_node_datetime(node, &dt);
|
|
862
|
+
char *buf = teptris_arena_alloc(scratch, 16);
|
|
863
|
+
snprintf(buf, 16, "%04d-%02d-%02d", dt.year, dt.month, dt.day);
|
|
864
|
+
v->str = buf;
|
|
865
|
+
v->str_len = 10;
|
|
866
|
+
return v;
|
|
867
|
+
}
|
|
868
|
+
case TEPTRIS_TIME_LOCAL: {
|
|
869
|
+
sch_svalue *v = sv_new(scratch, SV_TIME);
|
|
870
|
+
teptris_datetime dt;
|
|
871
|
+
teptris_node_datetime(node, &dt);
|
|
872
|
+
char *buf = teptris_arena_alloc(scratch, 24);
|
|
873
|
+
int n = snprintf(buf, 16, "%02d:%02d:%02d", dt.hour, dt.minute, dt.second);
|
|
874
|
+
if (dt.nanosecond > 0) {
|
|
875
|
+
n += snprintf(buf + n, 20, ".%09u", dt.nanosecond);
|
|
876
|
+
}
|
|
877
|
+
v->str = buf;
|
|
878
|
+
v->str_len = (size_t)n;
|
|
879
|
+
return v;
|
|
880
|
+
}
|
|
881
|
+
case TEPTRIS_ARRAY: {
|
|
882
|
+
sch_svalue *v = sv_new(scratch, SV_ARRAY);
|
|
883
|
+
size_t len = teptris_node_array_length(node);
|
|
884
|
+
v->items = teptris_arena_alloc(scratch, (len ? len : 1) * sizeof(sch_svalue *));
|
|
885
|
+
for (size_t i = 0; i < len; i++) {
|
|
886
|
+
v->items[i] = node_to_sv(scratch, teptris_node_array_at(node, i));
|
|
887
|
+
}
|
|
888
|
+
v->n_items = len;
|
|
889
|
+
return v;
|
|
890
|
+
}
|
|
891
|
+
case TEPTRIS_TABLE: {
|
|
892
|
+
sch_svalue *v = sv_new(scratch, SV_TABLE);
|
|
893
|
+
size_t len = teptris_node_table_length(node);
|
|
894
|
+
v->members = teptris_arena_alloc(scratch, (len ? len : 1) * sizeof(sv_member));
|
|
895
|
+
for (size_t i = 0; i < len; i++) {
|
|
896
|
+
teptris_view key;
|
|
897
|
+
const teptris_node *child =
|
|
898
|
+
teptris_node_table_at(node, i, &key);
|
|
899
|
+
char *kbuf = teptris_arena_alloc(scratch, key.len + 1);
|
|
900
|
+
memcpy(kbuf, key.ptr, key.len);
|
|
901
|
+
kbuf[key.len] = '\0';
|
|
902
|
+
v->members[i].key = kbuf;
|
|
903
|
+
v->members[i].val = node_to_sv(scratch, child);
|
|
904
|
+
}
|
|
905
|
+
v->n_members = len;
|
|
906
|
+
return v;
|
|
907
|
+
}
|
|
908
|
+
default: {
|
|
909
|
+
sch_svalue *v = sv_new(scratch, SV_NULL);
|
|
910
|
+
return v;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/* jv -> sv for schema-side enum/const values */
|
|
916
|
+
static sch_svalue *jv_to_sv(vctx *vc, jv *v) {
|
|
917
|
+
switch (jv_kind_of(v)) {
|
|
918
|
+
case JV_NULL: return sv_new(vc->scratch, SV_NULL);
|
|
919
|
+
case JV_BOOL: {
|
|
920
|
+
sch_svalue *sv = sv_new(vc->scratch, SV_BOOL);
|
|
921
|
+
sv->b = jv_bool(v);
|
|
922
|
+
return sv;
|
|
923
|
+
}
|
|
924
|
+
case JV_INT: {
|
|
925
|
+
sch_svalue *sv = sv_new(vc->scratch, SV_INT);
|
|
926
|
+
sv->i = jv_int(v);
|
|
927
|
+
return sv;
|
|
928
|
+
}
|
|
929
|
+
case JV_NUM: {
|
|
930
|
+
sch_svalue *sv = sv_new(vc->scratch, SV_FLOAT);
|
|
931
|
+
sv->f = jv_num(v);
|
|
932
|
+
return sv;
|
|
933
|
+
}
|
|
934
|
+
case JV_STRING: {
|
|
935
|
+
sch_svalue *sv = sv_new(vc->scratch, SV_STRING);
|
|
936
|
+
size_t len = jv_str_len(v);
|
|
937
|
+
char *buf = teptris_arena_alloc(vc->scratch, len + 1);
|
|
938
|
+
memcpy(buf, jv_str(v), len);
|
|
939
|
+
buf[len] = '\0';
|
|
940
|
+
sv->str = buf;
|
|
941
|
+
sv->str_len = len;
|
|
942
|
+
return sv;
|
|
943
|
+
}
|
|
944
|
+
case JV_ARRAY: {
|
|
945
|
+
sch_svalue *sv = sv_new(vc->scratch, SV_ARRAY);
|
|
946
|
+
size_t len = jv_item_count(v);
|
|
947
|
+
sv->items = teptris_arena_alloc(vc->scratch, (len ? len : 1) * sizeof(sch_svalue *));
|
|
948
|
+
for (size_t i = 0; i < len; i++) {
|
|
949
|
+
sv->items[i] = jv_to_sv(vc, jv_item(v, i));
|
|
950
|
+
}
|
|
951
|
+
sv->n_items = len;
|
|
952
|
+
return sv;
|
|
953
|
+
}
|
|
954
|
+
case JV_OBJECT: {
|
|
955
|
+
sch_svalue *sv = sv_new(vc->scratch, SV_TABLE);
|
|
956
|
+
size_t len = jv_member_count(v);
|
|
957
|
+
sv->members = teptris_arena_alloc(vc->scratch, (len ? len : 1) * sizeof(sv_member));
|
|
958
|
+
for (size_t i = 0; i < len; i++) {
|
|
959
|
+
size_t klen = strlen(jv_member_key(v, i));
|
|
960
|
+
char *kbuf = teptris_arena_alloc(vc->scratch, klen + 1);
|
|
961
|
+
memcpy(kbuf, jv_member_key(v, i), klen + 1);
|
|
962
|
+
sv->members[i].key = kbuf;
|
|
963
|
+
sv->members[i].val = jv_to_sv(vc, jv_member_val(v, i));
|
|
964
|
+
}
|
|
965
|
+
sv->n_members = len;
|
|
966
|
+
return sv;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
return sv_new(vc->scratch, SV_NULL);
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
/* ---------------------------------------------------------- public -- */
|
|
973
|
+
|
|
974
|
+
TEPTRIS_API teptris_status teptris_schema_validate_doc(
|
|
975
|
+
const struct teptris_schema *sch, const teptris_document *doc, char **buf,
|
|
976
|
+
size_t *out_len) {
|
|
977
|
+
if (sch == NULL || doc == NULL || buf == NULL || out_len == NULL) {
|
|
978
|
+
return TEPTRIS_ERR_ARG;
|
|
979
|
+
}
|
|
980
|
+
*buf = NULL;
|
|
981
|
+
*out_len = 0;
|
|
982
|
+
|
|
983
|
+
teptris_arena scratch;
|
|
984
|
+
teptris_arena_init(&scratch);
|
|
985
|
+
const teptris_node *root = teptris_document_root(doc);
|
|
986
|
+
sch_svalue *rv = node_to_sv(&scratch, root);
|
|
987
|
+
|
|
988
|
+
vctx vc;
|
|
989
|
+
memset(&vc, 0, sizeof(vc));
|
|
990
|
+
vc.scratch = &scratch;
|
|
991
|
+
|
|
992
|
+
val(&vc, sch->root, rv);
|
|
993
|
+
|
|
994
|
+
teptris_status st = TEPTRIS_OK;
|
|
995
|
+
/* the envelope is always one JSON array: wrap the comma-joined
|
|
996
|
+
* error objects (or emit the empty array) */
|
|
997
|
+
size_t body_n = vc.n;
|
|
998
|
+
size_t out_n = body_n > 0 ? body_n + 2 : 2;
|
|
999
|
+
char *out = malloc(out_n + 1);
|
|
1000
|
+
if (out == NULL) {
|
|
1001
|
+
st = TEPTRIS_ERR_ALLOC;
|
|
1002
|
+
} else {
|
|
1003
|
+
if (body_n > 0) {
|
|
1004
|
+
out[0] = '[';
|
|
1005
|
+
memcpy(out + 1, vc.buf, body_n);
|
|
1006
|
+
out[body_n + 1] = ']';
|
|
1007
|
+
out[out_n] = '\0';
|
|
1008
|
+
} else {
|
|
1009
|
+
memcpy(out, "[]", 3);
|
|
1010
|
+
}
|
|
1011
|
+
*buf = out;
|
|
1012
|
+
*out_len = out_n;
|
|
1013
|
+
}
|
|
1014
|
+
if (vc.buf != NULL) free(vc.buf);
|
|
1015
|
+
teptris_arena_destroy(&scratch);
|
|
1016
|
+
return st;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
TEPTRIS_API teptris_status teptris_schema_validate(const struct teptris_schema *sch,
|
|
1020
|
+
const char *toml, size_t len,
|
|
1021
|
+
char **buf, size_t *out_len) {
|
|
1022
|
+
if (sch == NULL || toml == NULL || buf == NULL || out_len == NULL) {
|
|
1023
|
+
return TEPTRIS_ERR_ARG;
|
|
1024
|
+
}
|
|
1025
|
+
teptris_document *doc = NULL;
|
|
1026
|
+
teptris_status st = teptris_parse(toml, len, NULL, &doc);
|
|
1027
|
+
if (st != TEPTRIS_OK) {
|
|
1028
|
+
if (doc != NULL) teptris_document_free(doc);
|
|
1029
|
+
return st;
|
|
1030
|
+
}
|
|
1031
|
+
st = teptris_schema_validate_doc(sch, doc, buf, out_len);
|
|
1032
|
+
teptris_document_free(doc);
|
|
1033
|
+
return st;
|
|
1034
|
+
}
|