code wiki / _hdl_build / nx_apistack_schema.nx
nx_apistack_schema.nx source
↩ module page · 45 lines · 2128 B
1// nx_apistack_schema.nx -- CAP-API-SCHEMA: machine-readable request validation for /api (OpenAPI/JSON-Schema role).
2// Schema is DATA (field<TAB>required<TAB>type rows), so a route's contract is a config not code; sc_validate checks
3// every required field is present in the form body at a real token boundary (not a substring). Deterministic
4// accept/reject, self-hosted spec = the sovereign exceed over a bolt-on validator. license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "nx_site_lock_lib.nx"
7
8// is `field` present in the form body at a token boundary (start or after '&'), followed by '='?
9func sc_field_present(body: *u8, body_n: i64, field: *u8, field_n: i64) -> i64 {
10 var i: i64 = 0
11 while i < body_n {
12 var at_start: i64 = 0
13 if i == 0 { at_start = 1 } else { if body[i - 1] == (38 as u8) { at_start = 1 } }
14 if at_start == 1 {
15 if i + field_n < body_n {
16 var m: i64 = 1; var j: i64 = 0
17 while j < field_n { if body[i + j] != field[j] { m = 0; j = field_n } else { j = j + 1 } }
18 if m == 1 { if body[i + field_n] == (61 as u8) { return 1 } } // '='
19 }
20 }
21 i = i + 1
22 }
23 return 0
24}
25// validate the body against the schema: 1 iff every REQUIRED field is present. 0 = a required field is missing.
26func sc_validate(schema: *u8, sc_n: i64, body: *u8, body_n: i64) -> i64 {
27 let fs: *i64 = sys_mmap(8); let fe: *i64 = sys_mmap(8)
28 var ls: i64 = 0
29 while ls < sc_n {
30 let le: i64 = slk_line_end(schema, sc_n, ls)
31 if le > ls { if schema[ls] != (35 as u8) {
32 if slk_field(schema, ls, le, 1, fs, fe) == 1 {
33 let req: i64 = slk_atoi(schema, fs[0], fe[0])
34 if req == 1 {
35 let fs0: *i64 = sys_mmap(8); let fe0: *i64 = sys_mmap(8)
36 if slk_field(schema, ls, le, 0, fs0, fe0) == 1 {
37 if sc_field_present(body, body_n, slk_at(schema, fs0[0]), fe0[0] - fs0[0]) == 0 { return 0 }
38 }
39 }
40 }
41 } }
42 ls = le + 1
43 }
44 return 1
45}