From b660f2e74159cec599d84344898e8b030602ea75 Mon Sep 17 00:00:00 2001 From: Andrei Kirkouski Date: Thu, 1 Oct 2026 10:41:02 +0200 Subject: [PATCH 01/63] test(forms): pin the runtime behavior the V2 field rules rely on Probe tests for number decoding and arithmetic, null in decoded answers, time.parse(DATE_ONLY) strictness and text.regexp dialect and bounds. Each asserts what runtime 0.3.43a does today, so a runtime upgrade that changes one fails a named test. --- test/forms/_index.yaml | 16 ++ test/forms/runtime_probe_test.lua | 260 ++++++++++++++++++++++++++++++ 2 files changed, 276 insertions(+) create mode 100644 test/forms/runtime_probe_test.lua diff --git a/test/forms/_index.yaml b/test/forms/_index.yaml index 79874c9..3f7b09f 100644 --- a/test/forms/_index.yaml +++ b/test/forms/_index.yaml @@ -20,6 +20,22 @@ entries: test: wippy.test:test method: run + - name: runtime_probe_test + kind: function.lua + meta: + name: Runtime facts behind the V2 field rules + type: test + suite: session + comment: Number decoding and arithmetic, null handling, date parsing and regexp behavior of the runtime that the V2 standard field rules rely on, asserted as observed so a runtime change shows up + source: file://runtime_probe_test.lua + modules: + - json + - time + - text + imports: + test: wippy.test:test + method: run + - name: form_index_test kind: function.lua meta: diff --git a/test/forms/runtime_probe_test.lua b/test/forms/runtime_probe_test.lua new file mode 100644 index 0000000..015d3d8 --- /dev/null +++ b/test/forms/runtime_probe_test.lua @@ -0,0 +1,260 @@ +local test = require("test") +local json = require("json") +local time = require("time") +local text = require("text") + +-- Runtime facts the V2 standard field rules (src/forms/field_values.lua, +-- form_pattern.lua) are built on, checked against the Wippy runtime these +-- tests run on. Each case asserts what the runtime does today: when a runtime +-- upgrade changes one, the failure points at the rule that has to be looked +-- at again. The observed values are written up in +-- .local/work/session-forms/probes/2026-10-01-v2-runtime-probes.md. +-- +-- Lua source in these tests writes raw bytes as decimal escapes ("\240"): +-- this runtime has no "\xHH" or "\u{...}" escape and silently drops the +-- backslash. + +local function compile(pattern: string): (any, any) + return text.regexp.compile(pattern) +end + +-- Whether `pattern` matches the whole of `subject` (patterns here carry their +-- own anchors). Fails the test when the pattern does not compile. +local function matches(pattern: string, subject: string): boolean + local re, err = compile(pattern) + test.is_nil(err, "compile " .. pattern) + local result, match_err = (re :: any):match_string(subject) + test.is_nil(match_err) + return result == true +end + +local function define_tests() + describe("P0 Lua source escapes", function() + it("has decimal byte escapes only", function() + test.eq(#"\240", 1) + test.eq(string.byte("\240", 1), 240) + test.eq(#"\xF0", 3, "\\xF0 is read as the three characters xF0") + test.eq(#"\u{E9}", 5, "\\u{E9} is read as the five characters u{E9}") + end) + end) + + describe("P1 numbers", function() + it("decodes integers exactly and everything else as a float", function() + local cases = { + { "3", 3, "integer" }, + { "3.0", 3, "float" }, + { "1e2", 100, "float" }, + { "0.1", 0.1, "float" }, + { "-0", 0, "integer" }, + { "1e21", 1e21, "float" }, + { "1e-7", 1e-7, "float" }, + { "123456789012345678", 123456789012345678, "integer" }, + { "12345678901234567890", 1.2345678901234567e19, "float" }, + } + for _, case in ipairs(cases) do + local value, err = json.decode(case[1]) + test.is_nil(err, case[1]) + test.eq(value, case[2], case[1]) + test.eq(math.type(value), case[3], case[1]) + end + -- "-0.0" keeps its sign as a float; "-0" has none. + local negative_zero = json.decode("-0.0") + test.eq(math.type(negative_zero), "float") + test.is_true(1 / negative_zero < -1e308, "1 / -0.0 is negative infinity") + -- math.huge is the largest finite float here, not infinity, so the + -- number rules test magnitude with a plain comparison. + test.eq(math.huge, 1.7976931348623157e308) + end) + + it("fails the whole decode on a number outside the float range", function() + for _, source in ipairs({ "1e400", "-1e400" }) do + local value, err = json.decode(source) + test.is_nil(value, source) + test.contains(tostring(err), "out of range") + local object, object_err = json.decode('{"answers":{"n":' .. source .. '}}') + test.is_nil(object, "an overflowing answer fails the whole body") + test.not_nil(object_err) + end + end) + + it("keeps integers exact in sums and differences but not in comparison or modulo", function() + local big = json.decode("9007199254740993") -- 2^53 + 1 + test.eq(math.type(big), "integer") + test.eq(big - 9007199254740992, 1, "subtraction is exact") + test.eq(big == 9007199254740992, true, "== goes through float64") + local wide = json.decode("123456789012345678") + test.eq(wide - 123456789012345000, 678, "subtraction is exact") + test.eq(wide % 1000, 680, "% goes through float64: the exact remainder is 678") + test.eq(math.type(3 % 2), "float") + test.eq(math.tointeger(1e18), 1000000000000000000) + test.eq(math.type(math.tointeger(3.0)), "integer") + test.eq(math.type(math.floor(json.decode("3.0"))), "float") + end) + + it("encodes numbers in a position-dependent spelling", function() + test.eq(json.encode(3.0), "3") + test.eq(json.encode(0.1), "0.1") + test.eq(json.encode(-0.0), "-0") + test.eq(json.encode(1e-7), "1e-7") + test.eq(json.encode(1e21), "1e+21") + test.eq(json.encode({ a = 1e-7 }), '{"a":0.0000001}') + test.eq(json.encode({ a = 1e21 }), '{"a":1000000000000000000000}') + test.eq(json.encode({ a = 3.0 }), '{"a":3}') + end) + + it("formats and parses decimal text", function() + test.eq(string.format("%.1e", 0.1), "1.0e-01") + test.eq(string.format("%.16e", 0.1), "1.0000000000000001e-01") + test.eq(string.format("%.2e", 123456.0), "1.23e+05") + test.eq(string.format("%d", 3.0), "3") + test.contains(string.format("%.3e", json.decode("5")), "%!e", "%e does not take an integer value") + test.eq(tonumber("1.5e+02"), 150) + test.eq(tonumber("1.0e-01"), 0.1) + test.is_nil(tonumber("1e2"), "an exponent needs a mantissa with a point") + test.eq(tostring(0.30000000000000004), "0.30000000000000004") + test.eq(tostring(1e-7), "1e-07") + test.eq(tostring(123456789.125), "1.23456789125e+08") + end) + end) + + describe("P2 null in decoded answers", function() + it("drops a null object member and leaves a hole in an array", function() + local object, err = json.decode('{"a":null,"b":1}') + test.is_nil(err) + test.is_nil(object.a) + test.eq(object.b, 1) + local keys = 0 + for _ in pairs(object) do keys = keys + 1 end + test.eq(keys, 1) + + local list = json.decode("[1,null,2]") + local count = 0 + for _ in pairs(list) do count = count + 1 end + test.eq(count, 2) + test.eq(#list, 3, "the length counts the hole, so it is not a plain array") + + local nothing, nothing_err = json.decode("null") + test.is_nil(nothing) + test.is_nil(nothing_err) + + local nested = json.decode('{"answers":{"x":null}}') + test.is_nil(next(nested.answers), "an answer sent as null is an answer not sent") + end) + end) + + describe("P3 time.parse(time.DATE_ONLY)", function() + it("accepts exactly the real calendar days written as YYYY-MM-DD", function() + test.eq(time.DATE_ONLY, "2006-01-02") + local accepted = { + "2024-02-29", "2000-02-29", "0001-01-01", "9999-12-31", "2023-12-31", + "0000-01-01", -- year 0 parses: the form rules refuse it themselves + } + for _, value in ipairs(accepted) do + local parsed, err = time.parse(time.DATE_ONLY, value) + test.is_nil(err, value) + test.not_nil(parsed, value) + end + local parsed = time.parse(time.DATE_ONLY, "2024-02-29") + test.eq(parsed:year(), 2024) + test.eq(parsed:month(), 2) + test.eq(parsed:day(), 29) + end) + + it("rejects wrong calendar days and every other spelling", function() + local rejected = { + "2023-02-30", "2023-02-29", "1900-02-29", "2100-02-29", "2023-04-31", "2023-01-32", + "2023-13-01", "2023-00-10", "2023-2-3", "2023-1-01", "2023-01-1", + " 2023-01-01", "2023-01-01 ", "2023-01-01x", "2023-01-01T00:00:00Z", "+2023-01-01", + "10000-01-01", "23-01-01", "2023/01/01", "", + "2023-01-01", -- full-width digits + } + for _, value in ipairs(rejected) do + local parsed, err = time.parse(time.DATE_ONLY, value) + test.is_nil(parsed, "[" .. value .. "]") + test.not_nil(err, "[" .. value .. "]") + end + local _, calendar_err = time.parse(time.DATE_ONLY, "2023-02-30") + test.contains(tostring(calendar_err), "day out of range") + end) + end) + + describe("P4 text.regexp", function() + it("refuses what the form rules also refuse, with an INVALID error", function() + local refused = { + "(?=a)", "(?!a)", "(a)\\1", "a{1001}", "a{0,2000}", "a{3,2}", "a**", "a{2}{3}", "(", "[", "*a", + "a\255", -- invalid UTF-8 in the pattern + } + for _, pattern in ipairs(refused) do + local re, err = compile(pattern) + test.is_nil(re, pattern) + test.not_nil(err, pattern) + test.eq(err:kind(), errors.INVALID, pattern) + end + local _, lookahead = compile("(?=a)") + test.contains(tostring(lookahead), "invalid or unsupported Perl syntax") + test.not_nil(compile("a{1000}"), "a repeat count of 1000 is the bound") + end) + + it("accepts what the form rules must refuse themselves", function() + local accepted = { + "(?i)abc", "(?i:abc)", "(?Pa)", "(?a)", "\\s", "\\S", "\\b", "\\B", "\\pL", "\\p{L}", + "[[:alpha:]]", "\\x41", "\\x{41}", "\\Aab\\z", "a{,3}", "a{}", "}", "]", "\\-", "\\_", "\\@", + "^*", "[\\w-x]", "(?:)", "a|", + } + for _, pattern in ipairs(accepted) do + local re, err = compile(pattern) + test.is_nil(err, pattern) + test.not_nil(re, pattern) + end + end) + + it("matches the whole text only, ASCII classes, one character per code point", function() + test.is_true(matches("^(?:a+)$", "aaa")) + test.is_false(matches("^(?:a+)$", "aaa\n"), "$ is the end of the text, not before a final newline") + test.is_false(matches("(?:a+)$", "aaa\n")) + test.is_true(matches("^(?:\\d+)$", "123")) + test.is_false(matches("^(?:\\d+)$", "\217\161\217\162"), "\\d is ASCII only (Arabic-Indic digits)") + test.is_true(matches("^(?:\\w+)$", "abc_1")) + test.is_false(matches("^(?:\\w+)$", "é"), "\\w is ASCII only") + test.is_false(matches("^(?:\\s)$", "\194\160"), "\\s does not match a no-break space (JavaScript's does)") + test.is_true(matches("^(?:\\pL+)$", "é")) + test.is_true(matches("^(?:.)$", "\240\159\152\128"), ". is one code point, not one UTF-8 byte") + test.is_false(matches("^(?:..)$", "\240\159\152\128")) + test.is_true(matches("^(?:[\240\159\152\128])$", "\240\159\152\128")) + test.is_true(matches("^(?:.)$", "\r"), ". matches CR (JavaScript's does not)") + test.is_true(matches("^(?:.)$", "\226\128\168"), ". matches U+2028 (JavaScript's does not)") + test.is_false(matches("^(?:.)$", "\n")) + test.is_true(matches("(?i)^(?:abc)$", "ABC")) + test.is_true(matches("^(?:a{2,3})$", "aa")) + test.is_true(matches("^(?:a+?)$", "aaa")) + test.is_true(matches("^(?:(a{2}){3})$", "aaaaaa")) + end) + + it("reads invalid UTF-8 in a subject as a replacement character", function() + test.is_true(matches("^(?:.+)$", "ab\255cd")) + test.is_false(matches("^(?:[a-z]+)$", "ab\255cd")) + end) + + it("runs in linear time on patterns that make a backtracking engine stall", function() + local started = time.now() + test.is_false(matches("^(?:(a+)+b)$", string.rep("a", 2000))) + test.is_false(matches("^(?:(x+x+)+y)$", string.rep("x", 2000))) + test.is_false(matches("^(?:(a|b|ab)+c)$", string.rep("ab", 1000))) + local long = string.rep("ab ", 666) + local re = compile("^(?:[a-z ]*)$") + for _ = 1, 100 do + test.is_true(((re :: any):match_string(long))) + end + local elapsed = time.now():sub(started):milliseconds() + test.lt(elapsed, 2000, "a 2000-character value matches in far under a second") + end) + + it("compiles a pattern wrapped for a full match", function() + test.is_true(matches("^(?:a|b)$", "b")) + test.is_false(matches("^(?:a|b)$", "ab")) + test.is_true(matches("^(?:^a$)$", "a"), "an author's own anchors are harmless inside the wrapper") + end) + end) +end + +return test.run_cases(define_tests) From 4d2dd8b43086ce465fb97b0c6d354c4a1d92b7cb Mon Sep 17 00:00:00 2001 From: Andrei Kirkouski Date: Thu, 1 Oct 2026 10:42:48 +0200 Subject: [PATCH 02/63] refactor(forms): move the shared text rules into form_text The UTF-8 decoder, code point count, JavaScript trim set, blank and control character tests and the plain-text definition key checks move out of definition.lua so the standard field values can use them without a dependency cycle. Behavior is unchanged; the existing suite is the regression net. --- src/forms/_index.yaml | 8 ++ src/forms/definition.lua | 209 ++-------------------------------- src/forms/form_text.lua | 238 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 255 insertions(+), 200 deletions(-) create mode 100644 src/forms/form_text.lua diff --git a/src/forms/_index.yaml b/src/forms/_index.yaml index 8205477..d7303fc 100644 --- a/src/forms/_index.yaml +++ b/src/forms/_index.yaml @@ -23,6 +23,13 @@ entries: comment: Accessors for the form record in artifact meta (meta.session_form); imports nothing, so persistence can use it source: file://form_meta.lua + # wippy.session.forms:form_text + - name: form_text + kind: library.lua + meta: + comment: UTF-8 decoding, code point counts, the JavaScript trim set, blank and control character tests and plain-text definition key checks, shared by the definition and the standard field values + source: file://form_text.lua + # wippy.session.forms:definition - name: definition kind: library.lua @@ -33,6 +40,7 @@ entries: - json imports: ordered_json: wippy.session.forms:ordered_json + form_text: wippy.session.forms:form_text # wippy.session.forms:form_artifact - name: form_artifact diff --git a/src/forms/definition.lua b/src/forms/definition.lua index 6a9022f..bf8df2c 100644 --- a/src/forms/definition.lua +++ b/src/forms/definition.lua @@ -74,206 +74,15 @@ local function is_object(value: any): boolean return true end --- Decodes the UTF-8 character at byte `index` of `text`. Returns its code --- point and the index of the next character, or nil when the bytes there are --- not valid UTF-8: a malformed or truncated sequence, an overlong form, a --- surrogate or a value past U+10FFFF. The runtime (Lua 5.3, Wippy --- modification) has no utf8 library, global or required, so this is the one --- decoder every text rule below uses. -local function decode_at(text: string, index: integer): (integer?, integer) - local byte = string.byte(text, index) - local extra, minimum, value - if byte < 0x80 then - return byte, index + 1 - elseif byte >= 0xC2 and byte <= 0xDF then - extra, minimum, value = 1, 0x80, byte - 0xC0 - elseif byte >= 0xE0 and byte <= 0xEF then - extra, minimum, value = 2, 0x800, byte - 0xE0 - elseif byte >= 0xF0 and byte <= 0xF4 then - extra, minimum, value = 3, 0x10000, byte - 0xF0 - else - return nil, index - end - if index + extra > #text then return nil, index end - for offset = 1, extra do - local continuation = string.byte(text, index + offset) - if continuation < 0x80 or continuation > 0xBF then return nil, index end - value = value * 64 + (continuation - 0x80) - end - if value < minimum or value > 0x10FFFF or (value >= 0xD800 and value <= 0xDFFF) then - return nil, index - end - return value, index + extra + 1 -end - --- Number of Unicode code points in `text`, or nil when it is not valid --- UTF-8. Limits count characters as users see them, as json.validate's --- maxLength does. -local function char_length(text: string): number? - local count = 0 - local index = 1 - local size = #text - while index <= size do - local value - value, index = decode_at(text, index) - if not value then return nil end - count = count + 1 - end - return count -end - --- The characters JavaScript's String.prototype.trim removes: ECMAScript --- WhiteSpace (tab, vertical tab, form feed, U+FEFF and every Unicode space --- separator) and LineTerminator (LF, CR, U+2028, U+2029). The form component --- trims Other text and notes with it, so the server trims them, and decides --- they are blank, with exactly this set. U+200B ZERO WIDTH SPACE is not in it. -local WHITESPACE = { - [0x0009] = true, [0x000A] = true, [0x000B] = true, [0x000C] = true, [0x000D] = true, - [0x0020] = true, [0x00A0] = true, [0x1680] = true, - [0x2000] = true, [0x2001] = true, [0x2002] = true, [0x2003] = true, [0x2004] = true, - [0x2005] = true, [0x2006] = true, [0x2007] = true, [0x2008] = true, [0x2009] = true, - [0x200A] = true, [0x2028] = true, [0x2029] = true, [0x202F] = true, [0x205F] = true, - [0x3000] = true, [0xFEFF] = true, -} - --- Invisible format characters. An agent-authored title, label or message --- made up only of these and WHITESPACE shows nothing, so it is blank too. -local ZERO_WIDTH = { [0x200B] = true, [0x200C] = true, [0x200D] = true, [0x2060] = true } - --- True when `text` (valid UTF-8) shows nothing: empty, or every character --- in WHITESPACE or ZERO_WIDTH. For agent-authored definition text; answer --- text follows JavaScript trim instead (trim). -local function is_blank(text: string): boolean - local index = 1 - local size = #text - while index <= size do - local value - value, index = decode_at(text, index) - if not value or not (WHITESPACE[value] or ZERO_WIDTH[value]) then - return false - end - end - return true -end - -local function add_error(errors: {any}, path: string, code: string, message: string) - table.insert(errors, { path = path, code = code, message = message }) -end - --- True when `text` (valid UTF-8) has a control character: ASCII C0 (and --- DEL), other than tab and newline where `multiline`; C1 (U+0080-U+009F); --- the bidi override/isolate controls (U+202A-U+202E, U+2066-U+2069); or the --- line/paragraph separators U+2028/U+2029. Question text is shown as-is, so --- these are refused rather than silently changed or left to confuse a --- renderer that treats them as directional or line-breaking control. -local function has_control_characters(text: string, multiline: boolean): boolean - local index = 1 - local size = #text - while index <= size do - local value - value, index = decode_at(text, index) - if not value then return true end - if value < 0x80 then - local is_c0 = value < 32 or value == 127 - if is_c0 and multiline and (value == 9 or value == 10) then - is_c0 = false - end - if is_c0 then return true end - elseif (value >= 0x80 and value <= 0x9F) - or (value >= 0x202A and value <= 0x202E) - or (value >= 0x2066 and value <= 0x2069) - or value == 0x2028 or value == 0x2029 then - return true - end - end - return false -end - --- `text` without the WHITESPACE characters at either end, as JavaScript's --- String.prototype.trim gives it. One forward pass, so it stays linear on a --- long run of spaces, where a "^%s*(.-)%s*$" pattern backtracks --- quadratically. Text that is not valid UTF-8 is returned as it is, for the --- caller's UTF-8 check to refuse. -local function trim(text: string): string - local index = 1 - local size = #text - local first, last = nil, 0 - while index <= size do - local value, next_index = decode_at(text, index) - if not value then return text end - if not WHITESPACE[value] then - first = first or index - last = next_index - 1 - end - index = next_index - end - if not first then return "" end - return string.sub(text, first, last) -end - --- Checks an optional or required plain-text value. Returns the value to keep --- (nil for an omitted optional value) and whether it was valid. opts.strict --- applies the rules for new definitions (control characters); stored --- definitions are read without them so a later tightening never makes an --- existing form unreadable. -local function check_text(errors: {any}, path: string, value: any, - opts: {required: boolean, min: number?, max: number, strict: boolean?, multiline: boolean?}) - if value == nil then - if opts.required then - add_error(errors, path, "required", "A value is required.") - return nil, false - end - return nil, true - end - if type(value) ~= "string" then - add_error(errors, path, "invalid_type", "Must be a string.") - return nil, false - end - local length = char_length(value) - if not length then - add_error(errors, path, "invalid_text", "Must be valid UTF-8 text.") - return nil, false - end - if opts.required and is_blank(value) then - add_error(errors, path, "blank", "Must contain visible text.") - return nil, false - end - if opts.strict and has_control_characters(value, opts.multiline == true) then - add_error(errors, path, "control_characters", opts.multiline - and "Must not contain control characters other than tabs and line breaks." - or "Must not contain control characters or line breaks.") - return nil, false - end - if length > opts.max then - add_error(errors, path, "too_long", string.format("Must be at most %d characters.", opts.max)) - return nil, false - end - if not opts.required and length == 0 then - return nil, true - end - return value, true -end - -local function check_boolean(errors: {any}, path: string, value: any): boolean - if value == nil then return false end - if type(value) ~= "boolean" then - add_error(errors, path, "invalid_type", "Must be true or false.") - return false - end - return value -end - -local function check_unknown_keys(errors: {any}, path: string, value: table, allowed: {[string]: boolean}) - local unknown = {} - for key in pairs(value) do - if not allowed[key] then table.insert(unknown, tostring(key)) end - end - table.sort(unknown) - for _, key in ipairs(unknown) do - add_error(errors, path == "" and key or (path .. "." .. key), "unknown_key", - "Unknown key \"" .. key .. "\".") - end -end +local decode_at = form_text.decode_at +local char_length = form_text.char_length +local is_blank = form_text.is_blank +local has_control_characters = form_text.has_control_characters +local trim = form_text.trim +local add_error = form_text.add_error +local check_text = form_text.check_text +local check_boolean = form_text.check_boolean +local check_unknown_keys = form_text.check_unknown_keys --------------------------------------------------------------------------- -- Definition diff --git a/src/forms/form_text.lua b/src/forms/form_text.lua new file mode 100644 index 0000000..be50844 --- /dev/null +++ b/src/forms/form_text.lua @@ -0,0 +1,238 @@ +-- Text rules shared by the form definition (definition.lua) and the standard +-- field values (field_values.lua): UTF-8 decoding and code point counts, the +-- JavaScript trim set, blank and control character tests, and the checks for +-- plain-text definition keys. Pure functions, no I/O. +-- +-- Rejections are { path, code, message } items appended to an errors list, as +-- definition.normalize returns them. Array positions in paths are 0-based, as +-- in the JSON the agent wrote. + +local form_text = {} + +-- Decodes the UTF-8 character at byte `index` of `text`. Returns its code +-- point and the index of the next character, or nil when the bytes there are +-- not valid UTF-8: a malformed or truncated sequence, an overlong form, a +-- surrogate or a value past U+10FFFF. The runtime (Lua 5.3, Wippy +-- modification) has no utf8 library, global or required, so this is the one +-- decoder every text rule uses. +local function decode_at(text: string, index: integer): (integer?, integer) + local byte = string.byte(text, index) + local extra, minimum, value + if byte < 0x80 then + return byte, index + 1 + elseif byte >= 0xC2 and byte <= 0xDF then + extra, minimum, value = 1, 0x80, byte - 0xC0 + elseif byte >= 0xE0 and byte <= 0xEF then + extra, minimum, value = 2, 0x800, byte - 0xE0 + elseif byte >= 0xF0 and byte <= 0xF4 then + extra, minimum, value = 3, 0x10000, byte - 0xF0 + else + return nil, index + end + if index + extra > #text then return nil, index end + for offset = 1, extra do + local continuation = string.byte(text, index + offset) + if continuation < 0x80 or continuation > 0xBF then return nil, index end + value = value * 64 + (continuation - 0x80) + end + if value < minimum or value > 0x10FFFF or (value >= 0xD800 and value <= 0xDFFF) then + return nil, index + end + return value, index + extra + 1 +end + +-- Number of Unicode code points in `text`, or nil when it is not valid +-- UTF-8. Limits count characters as users see them, as json.validate's +-- maxLength does. +local function char_length(text: string): number? + local count = 0 + local index = 1 + local size = #text + while index <= size do + local value + value, index = decode_at(text, index) + if not value then return nil end + count = count + 1 + end + return count +end + +-- The characters JavaScript's String.prototype.trim removes: ECMAScript +-- WhiteSpace (tab, vertical tab, form feed, U+FEFF and every Unicode space +-- separator) and LineTerminator (LF, CR, U+2028, U+2029). The form component +-- trims Other text and notes with it, so the server trims them, and decides +-- they are blank, with exactly this set. U+200B ZERO WIDTH SPACE is not in it. +local WHITESPACE = { + [0x0009] = true, [0x000A] = true, [0x000B] = true, [0x000C] = true, [0x000D] = true, + [0x0020] = true, [0x00A0] = true, [0x1680] = true, + [0x2000] = true, [0x2001] = true, [0x2002] = true, [0x2003] = true, [0x2004] = true, + [0x2005] = true, [0x2006] = true, [0x2007] = true, [0x2008] = true, [0x2009] = true, + [0x200A] = true, [0x2028] = true, [0x2029] = true, [0x202F] = true, [0x205F] = true, + [0x3000] = true, [0xFEFF] = true, +} + +-- Invisible format characters. An agent-authored title, label or message +-- made up only of these and WHITESPACE shows nothing, so it is blank too. +local ZERO_WIDTH = { [0x200B] = true, [0x200C] = true, [0x200D] = true, [0x2060] = true } + +-- True when `text` (valid UTF-8) shows nothing: empty, or every character +-- in WHITESPACE or ZERO_WIDTH. For agent-authored definition text; answer +-- text follows JavaScript trim instead (trim, is_whitespace_only). +local function is_blank(text: string): boolean + local index = 1 + local size = #text + while index <= size do + local value + value, index = decode_at(text, index) + if not value or not (WHITESPACE[value] or ZERO_WIDTH[value]) then + return false + end + end + return true +end + +-- True when `text` (valid UTF-8) is empty or only JavaScript trim +-- whitespace: what a user's answer text counts as blank. Unlike is_blank, +-- zero-width characters are content here. +local function is_whitespace_only(text: string): boolean + local index = 1 + local size = #text + while index <= size do + local value + value, index = decode_at(text, index) + if not value or not WHITESPACE[value] then return false end + end + return true +end + +-- True when `text` (valid UTF-8) has a control character: ASCII C0 (and +-- DEL), other than tab and newline where `multiline`; C1 (U+0080-U+009F); +-- the bidi override/isolate controls (U+202A-U+202E, U+2066-U+2069); or the +-- line/paragraph separators U+2028/U+2029. Question text is shown as-is, so +-- these are refused rather than silently changed or left to confuse a +-- renderer that treats them as directional or line-breaking control. +local function has_control_characters(text: string, multiline: boolean): boolean + local index = 1 + local size = #text + while index <= size do + local value + value, index = decode_at(text, index) + if not value then return true end + if value < 0x80 then + local is_c0 = value < 32 or value == 127 + if is_c0 and multiline and (value == 9 or value == 10) then + is_c0 = false + end + if is_c0 then return true end + elseif (value >= 0x80 and value <= 0x9F) + or (value >= 0x202A and value <= 0x202E) + or (value >= 0x2066 and value <= 0x2069) + or value == 0x2028 or value == 0x2029 then + return true + end + end + return false +end + +-- `text` without the WHITESPACE characters at either end, as JavaScript's +-- String.prototype.trim gives it. One forward pass, so it stays linear on a +-- long run of spaces, where a "^%s*(.-)%s*$" pattern backtracks +-- quadratically. Text that is not valid UTF-8 is returned as it is, for the +-- caller's UTF-8 check to refuse. +local function trim(text: string): string + local index = 1 + local size = #text + local first, last = nil, 0 + while index <= size do + local value, next_index = decode_at(text, index) + if not value then return text end + if not WHITESPACE[value] then + first = first or index + last = next_index - 1 + end + index = next_index + end + if not first then return "" end + return string.sub(text, first, last) +end + +local function add_error(errors: {any}, path: string, code: string, message: string) + table.insert(errors, { path = path, code = code, message = message }) +end + +-- Checks an optional or required plain-text definition value. Returns the +-- value to keep (nil for an omitted optional value) and whether it was valid. +-- opts.strict applies the rules for new definitions (control characters); +-- stored definitions are read without them so a later tightening never makes +-- an existing form unreadable. +local function check_text(errors: {any}, path: string, value: any, + opts: {required: boolean, min: number?, max: number, strict: boolean?, multiline: boolean?}) + if value == nil then + if opts.required then + add_error(errors, path, "required", "A value is required.") + return nil, false + end + return nil, true + end + if type(value) ~= "string" then + add_error(errors, path, "invalid_type", "Must be a string.") + return nil, false + end + local length = char_length(value) + if not length then + add_error(errors, path, "invalid_text", "Must be valid UTF-8 text.") + return nil, false + end + if opts.required and is_blank(value) then + add_error(errors, path, "blank", "Must contain visible text.") + return nil, false + end + if opts.strict and has_control_characters(value, opts.multiline == true) then + add_error(errors, path, "control_characters", opts.multiline + and "Must not contain control characters other than tabs and line breaks." + or "Must not contain control characters or line breaks.") + return nil, false + end + if length > opts.max then + add_error(errors, path, "too_long", string.format("Must be at most %d characters.", opts.max)) + return nil, false + end + if not opts.required and length == 0 then + return nil, true + end + return value, true +end + +local function check_boolean(errors: {any}, path: string, value: any): boolean + if value == nil then return false end + if type(value) ~= "boolean" then + add_error(errors, path, "invalid_type", "Must be true or false.") + return false + end + return value +end + +local function check_unknown_keys(errors: {any}, path: string, value: table, allowed: {[string]: boolean}) + local unknown = {} + for key in pairs(value) do + if not allowed[key] then table.insert(unknown, tostring(key)) end + end + table.sort(unknown) + for _, key in ipairs(unknown) do + add_error(errors, path == "" and key or (path .. "." .. key), "unknown_key", + "Unknown key \"" .. key .. "\".") + end +end + +form_text.decode_at = decode_at +form_text.char_length = char_length +form_text.is_blank = is_blank +form_text.is_whitespace_only = is_whitespace_only +form_text.has_control_characters = has_control_characters +form_text.trim = trim +form_text.add_error = add_error +form_text.check_text = check_text +form_text.check_boolean = check_boolean +form_text.check_unknown_keys = check_unknown_keys + +return form_text From cbd6df0f08d8e10d32b29a2f9d986aac065ef093 Mon Sep 17 00:00:00 2001 From: Andrei Kirkouski Date: Thu, 1 Oct 2026 11:04:47 +0200 Subject: [PATCH 03/63] feat(forms): V2 standard fields: text, textarea, number, email, date, checkbox A form definition may now hold text, textarea, number, email, date and checkbox fields next to choices. field_values.lua owns the six types: per-type allowed keys, ranges, steps, a bounded regular expression subset (form_pattern.lua, compiled as a full match with text.regexp), defaults validated by the answer checker, the answer checks with stable codes, and the canonical text of a stored value. Numbers are checked on their shortest decimal digits with digit-string arithmetic: this runtime compares and takes remainders of integers through 64-bit floats, which is not exact above 2^53. Dates use time.parse(DATE_ONLY) behind an ASCII shape check. Text is stored as typed; an address is trimmed. The text, textarea and email limits of one form together are capped at 16,000 characters so every valid submission fits the 64 KiB body. The form version follows the definition: version 2 with any standard field, version 1 otherwise, and a choice-only form is unchanged byte for byte. The write, parse, GET, the POST envelope and the answer record all use it, and a stored version lower than the definition needs is refused. The answer record keeps {question, answer} with the answer as a JSON literal, and read_form and questions_json describe the new types and their limits. The ask_user schema is one flat object with the seven types. A shared JSON fixture of 481 cases runs through the definition and the answer validator; the form component is tested against the same file. --- src/forms/_index.yaml | 117 ++++- src/forms/answer_text.lua | 55 +- src/forms/definition.lua | 90 +++- src/forms/field_values.lua | 671 ++++++++++++++++++++++++ src/forms/form_artifact.lua | 17 +- src/forms/form_pattern.lua | 305 +++++++++++ src/forms/state_service.lua | 17 +- test/forms/_index.yaml | 40 ++ test/forms/definition_test.lua | 209 +++++++- test/forms/field_values_test.lua | 214 ++++++++ test/forms/fixtures/v2_conformance.json | 483 +++++++++++++++++ test/forms/state_service_test.lua | 331 ++++++++++++ test/forms/v2_conformance_test.lua | 164 ++++++ test/process/forms_test.lua | 74 ++- 14 files changed, 2712 insertions(+), 75 deletions(-) create mode 100644 src/forms/field_values.lua create mode 100644 src/forms/form_pattern.lua create mode 100644 test/forms/field_values_test.lua create mode 100644 test/forms/fixtures/v2_conformance.json create mode 100644 test/forms/v2_conformance_test.lua diff --git a/src/forms/_index.yaml b/src/forms/_index.yaml index d7303fc..053f471 100644 --- a/src/forms/_index.yaml +++ b/src/forms/_index.yaml @@ -1,9 +1,9 @@ version: "1.0" namespace: wippy.session.forms -# Session forms (V1: choice questions). The ask_user trait lets an agent ask -# the user a structured question; the answer comes back as an ordinary user -# message. Storage, turn handling and the state API live in the session +# Session forms (V1: choice questions; V2: text, textarea, number, email, date +# and checkbox fields). The ask_user trait lets an agent ask the user a +# structured question; the answer comes back as an ordinary user message. Storage, turn handling and the state API live in the session # module: see src/process/control_handlers.lua (creation), src/api (state # endpoints) and src/process/message_handlers.lua (answer admission). entries: @@ -30,17 +30,43 @@ entries: comment: UTF-8 decoding, code point counts, the JavaScript trim set, blank and control character tests and plain-text definition key checks, shared by the definition and the standard field values source: file://form_text.lua + # wippy.session.forms:form_pattern + - name: form_pattern + kind: library.lua + meta: + comment: The bounded regular expression subset a text field may carry (the same meaning in RE2 and in JavaScript with the u flag), its check and its full-match compile + source: file://form_pattern.lua + modules: + - text + imports: + form_text: wippy.session.forms:form_text + + # wippy.session.forms:field_values + - name: field_values + kind: library.lua + meta: + comment: Standard field types (text, textarea, number, email, date, checkbox) - definition keys, answer checks with stable codes, canonical answer text and the question descriptions for an agent + source: file://field_values.lua + modules: + - json + - text + - time + imports: + form_text: wippy.session.forms:form_text + form_pattern: wippy.session.forms:form_pattern + # wippy.session.forms:definition - name: definition kind: library.lua meta: - comment: Form definition normalizer and per-field answer validator (V1 choice fields) with stable error codes + comment: Form definition normalizer and per-field answer validator (choice fields here, standard fields through field_values) with stable error codes and the form version source: file://definition.lua modules: - json imports: ordered_json: wippy.session.forms:ordered_json form_text: wippy.session.forms:form_text + field_values: wippy.session.forms:field_values # wippy.session.forms:form_artifact - name: form_artifact @@ -62,6 +88,7 @@ entries: source: file://answer_text.lua imports: definition: wippy.session.forms:definition + field_values: wippy.session.forms:field_values ordered_json: wippy.session.forms:ordered_json # wippy.session.forms:state_service @@ -99,8 +126,10 @@ entries: comment: Validates a form definition and asks the session to show it to the user llm_alias: ask_user llm_description: | - Show the user a short form of choice questions and end your turn. Use it only when - you need the user to decide between concrete options before you can continue. + Show the user a short form and end your turn. Use it when you need the user's + input before you can continue: a choice between concrete options (type "choice"), + or details such as free text ("text", "textarea"), a "number", an "email" address, + a "date" or a "checkbox". The user's answers arrive later as a user message whose element carries a JSON record. Ask at most one form per turn. Keep the whole definition under about 32 KiB. That is measured after normalization, @@ -141,8 +170,8 @@ entries: }, "type": { "type": "string", - "enum": ["choice"], - "description": "Always \"choice\"." + "enum": ["choice", "text", "textarea", "number", "email", "date", "checkbox"], + "description": "\"choice\": pick from options (needs options). \"text\": one line of text. \"textarea\": several lines of text. \"number\": a number. \"email\": an email address. \"date\": a calendar date, YYYY-MM-DD. \"checkbox\": a yes/no box. Each type takes only its own keys below; any other key is an error." }, "label": { "type": "string", @@ -154,11 +183,11 @@ entries: }, "required": { "type": "boolean", - "description": "Whether an answer is required. Default false." + "description": "Whether an answer is required. Default false. A required checkbox must be checked, so use it for consent." }, "options": { "type": "array", - "description": "1-20 distinct choices, each at most 240 characters of text. A string is both the value and the label; use {\"value\", \"label\", \"description\"} when the stored value should differ from the text shown (value and label at most 240 characters each, description at most 1000).", + "description": "choice only, required there: 1-20 distinct choices, each at most 240 characters of text. A string is both the value and the label; use {\"value\", \"label\", \"description\"} when the stored value should differ from the text shown (value and label at most 240 characters each, description at most 1000).", "items": { "anyOf": [ { "type": "string" }, @@ -177,14 +206,50 @@ entries: }, "multiple": { "type": "boolean", - "description": "Allow selecting several options. Default false." + "description": "choice only: allow selecting several options. Default false." }, "allow_other": { "type": "boolean", - "description": "Offer an \"Other\" free-text answer. Default false." + "description": "choice only: offer an \"Other\" free-text answer. Default false." + }, + "min_length": { + "type": "integer", + "description": "text, textarea: fewest characters. Default 0." + }, + "max_length": { + "type": "integer", + "description": "text: most characters, default 500, at most 2000. textarea: default and at most 8000. The text, textarea and email questions of one form hold at most 16000 characters together, counting the most each allows (an email counts 254), so lower max_length where less is enough." + }, + "pattern": { + "type": "string", + "description": "text only: the whole answer must match this regular expression, at most 200 characters. Allowed: literal characters, . [a-z] [^0-9] \\d \\w ( ) (?: ) | * + ? {n} {n,m}, and a backslash before a punctuation character. Not allowed: lookahead or lookbehind, backreferences, named groups, flags, \\s \\b \\p, repeats of repeats such as (a+)+." + }, + "placeholder": { + "type": "string", + "description": "text, textarea: a hint shown in the empty box, at most 200 characters." + }, + "min": { + "anyOf": [{ "type": "number" }, { "type": "string" }], + "description": "number: the smallest allowed value, a number (at most 6 decimals, between -1000000000000 and 1000000000000). date: the earliest date, a string YYYY-MM-DD, inclusive." + }, + "max": { + "anyOf": [{ "type": "number" }, { "type": "string" }], + "description": "number: the largest allowed value (same limits as min). date: the latest date, a string YYYY-MM-DD, inclusive." + }, + "step": { + "type": "number", + "description": "number only: a positive step of at most 6 decimals. The allowed values are min (or 0 without min) plus whole multiples of the step." + }, + "integer": { + "type": "boolean", + "description": "number only: accept whole numbers only. Default false." + }, + "default": { + "anyOf": [{ "type": "string" }, { "type": "number" }, { "type": "boolean" }], + "description": "Not for choice. A value the form starts with: text for text, textarea, email and date, a number for number, only false for checkbox (a box never starts checked). It must itself be a valid answer. The user still has to submit it." } }, - "required": ["id", "type", "label", "options"] + "required": ["id", "type", "label"] } } }, @@ -206,16 +271,17 @@ entries: meta: type: agent.trait title: Ask the user with a form - comment: Lets an agent ask the user structured choice questions through a form in the chat + comment: Lets an agent ask the user structured questions (choices, text, numbers, email addresses, dates and checkboxes) through a form in the chat tags: - forms - session prompt: | - You can ask the user structured questions with the ask_user tool. It shows a form of - choice questions in the chat. + You can ask the user structured questions with the ask_user tool. It shows a form in + the chat. A question is a choice between options, or one of the standard fields: text + (one line), textarea (several lines), number, email, date or checkbox. - - Use it only when you need a decision from the user between concrete options before - you can continue. Ask plain questions in text when free conversation works better. + - Use it when you need a decision or specific details from the user before you can + continue. Ask plain questions in text when free conversation works better. - Calling ask_user ends your turn. Do not write a follow-up asking the user to fill in the form; the form is already visible to them. - Ask at most one form per turn, with all the questions you need in it. @@ -225,9 +291,15 @@ entries: element whose record attribute holds a JSON record. The user sees only the sentence; you get the record. The record is complete on its own: for each question id it has the question text and the answer, which is {"value", "label"} for a chosen option, {"other"} for the - user's own words, a list of those when several answers were allowed, or null when - the question was left unanswered. Use it; do not ask the same question again. + user's own words, a list of those when several answers were allowed, the text, the + number or true/false for a standard field, or null when the question was left + unanswered. Use it; do not ask the same question again. Never write a element yourself. + - A default only prefills a field: it is not the user's answer until they submit it. + A checkbox never starts checked; use a required checkbox for consent. + - Keep the form to the questions you need. A text, textarea or email field holds at + most what its max_length allows, and the fields of one form together at most 16000 + characters. - A chosen option can carry a "note": the user's own words about that choice. Follow it. - A record with "late": true was sent after the conversation moved on. Treat it as context only: confirm with the user before acting on the earlier request. @@ -251,8 +323,9 @@ entries: llm_alias: read_form llm_description: | Read one form of this session by its form_id. An answered form returns its answer - record: each question with the answer's value, label and any note. An unanswered - form returns its questions with their options. Use it when an earlier form or answer + record: each question with its answer (a choice's value, label and any note, or the + text, number or true/false of a standard field). An unanswered form returns its + questions with their type, options and limits. Use it when an earlier form or answer is no longer in the conversation. A result with "status":"answer_pending" means the user already answered, but the answer has not arrived as a user message yet. Do not act on it or guess its content diff --git a/src/forms/answer_text.lua b/src/forms/answer_text.lua index 59f0cb9..1778760 100644 --- a/src/forms/answer_text.lua +++ b/src/forms/answer_text.lua @@ -1,4 +1,5 @@ local definition = require("definition") +local field_values = require("field_values") local ordered_json = require("ordered_json") -- Builds the text of the user message that carries an accepted form answer @@ -21,8 +22,11 @@ local ordered_json = require("ordered_json") -- The JSON is self-contained, so the agent needs nothing else to act on it: -- is {"value","label"} for a chosen option (plus "note" when the -- user added one), {"other"} for the user's own answer, a list of those for a --- several-answer question, or null for a question left unanswered. A late --- answer adds "late_notice" telling the agent to ask for fresh confirmation. +-- several-answer question, the text, number or true/false the user gave for a +-- standard field (text, textarea, email, date, number, checkbox), or null for +-- a question left unanswered. A late answer adds "late_notice" telling the +-- agent to ask for fresh confirmation. "form_version" is 1 for a form of +-- choices only and 2 for a form with a standard field. -- The stored submission keeps only values (definition.encode_answers); this -- record is derived from it and the immutable definition. read_form returns -- the same record. @@ -100,7 +104,7 @@ end local function option_labels(field: any): {[string]: string} local labels: {[string]: string} = {} - for _, option in ipairs(field.options) do labels[option.value] = option.label end + for _, option in ipairs(field.options or {}) do labels[option.value] = option.label end return labels end @@ -125,15 +129,19 @@ function answer_text.record_json(params: any): string local def = params.definition local questions = {} for _, field in ipairs(def.fields) do - local labels = option_labels(field) local value = params.answers and params.answers[field.id] local answer = "null" - if value ~= nil and definition.is_array(value) then - local items = {} - for _, item in ipairs(value) do table.insert(items, record_item(labels, item)) end - answer = ordered_json.array(items) + if value ~= nil and field.type ~= "choice" then + answer = field_values.literal(value) elseif value ~= nil then - answer = record_item(labels, value) + local labels = option_labels(field) + if definition.is_array(value) then + local items = {} + for _, item in ipairs(value) do table.insert(items, record_item(labels, item)) end + answer = ordered_json.array(items) + else + answer = record_item(labels, value) + end end table.insert(questions, { field.id, raw = ordered_json.object({ { "question", field.label }, { "answer", raw = answer }, @@ -141,7 +149,7 @@ function answer_text.record_json(params: any): string end local entries = { { "form_id", tostring(params.artifact_id) }, - { "form_version", definition.FORM_VERSION }, + { "form_version", definition.version_of(def) }, { "title", def.title }, { "late", params.late == true }, } @@ -153,19 +161,26 @@ end -- The questions of an unanswered form with their options, so an agent that -- lost the original tool call can still restate them. function answer_text.questions_json(def: any): string + -- A form with a standard field names the type of every question; a form + -- of choices only is described as it always was. + local typed = definition.version_of(def) == definition.VERSIONS.FIELDS local questions = {} for _, field in ipairs(def.fields) do - local options = {} - for _, option in ipairs(field.options) do - table.insert(options, ordered_json.object({ { "value", option.value }, { "label", option.label } })) + local entries: {any} = { { "question", field.label } } + if typed then table.insert(entries, { "type", field.type }) end + if field.type == "choice" then + local options = {} + for _, option in ipairs(field.options) do + table.insert(options, ordered_json.object({ { "value", option.value }, { "label", option.label } })) + end + table.insert(entries, { "multiple", field.multiple == true }) + table.insert(entries, { "required", field.required == true }) + table.insert(entries, { "allow_other", field.allow_other == true }) + table.insert(entries, { "options", raw = ordered_json.array(options) }) + else + for _, entry in ipairs(field_values.question_entries(field)) do table.insert(entries, entry) end end - table.insert(questions, { field.id, raw = ordered_json.object({ - { "question", field.label }, - { "multiple", field.multiple == true }, - { "required", field.required == true }, - { "allow_other", field.allow_other == true }, - { "options", raw = ordered_json.array(options) }, - }) }) + table.insert(questions, { field.id, raw = ordered_json.object(entries) }) end return ordered_json.object(questions) end diff --git a/src/forms/definition.lua b/src/forms/definition.lua index bf8df2c..357edae 100644 --- a/src/forms/definition.lua +++ b/src/forms/definition.lua @@ -1,8 +1,12 @@ local json = require("json") local ordered_json = require("ordered_json") +local form_text = require("form_text") +local field_values = require("field_values") --- Session forms V1 contract: the definition an agent authors (choice fields --- only) and the answers a user submits for it. +-- Session forms contract: the definition an agent authors and the answers a +-- user submits for it. A field is a choice (defined here) or one of the +-- standard types text, textarea, number, email, date and checkbox (defined +-- in field_values.lua). -- -- Pure functions, no I/O. Every rejection carries a stable `code`, a `path` -- (definition) or field id (answers) and a plain `message`. Array positions @@ -15,7 +19,18 @@ local ordered_json = require("ordered_json") local definition = {} -definition.FORM_VERSION = 1 +-- The version of a form follows its definition: a form of choice fields only +-- is version 1, exactly as it was before standard fields existed, and a form +-- with any other field is version 2. A reader that only knows version 1 shows +-- a notice for a version 2 form instead of rendering it wrongly. +definition.VERSIONS = { CHOICES = 1, FIELDS = 2 } + +function definition.version_of(def: any): integer + for _, field in ipairs(def.fields) do + if field.type ~= "choice" then return definition.VERSIONS.FIELDS end + end + return definition.VERSIONS.CHOICES +end definition.LIMITS = { TITLE = 160, @@ -74,7 +89,6 @@ local function is_object(value: any): boolean return true end -local decode_at = form_text.decode_at local char_length = form_text.char_length local is_blank = form_text.is_blank local has_control_characters = form_text.has_control_characters @@ -117,7 +131,9 @@ local function normalize_field(errors: {any}, path: string, raw: any, seen_ids: return nil end local before = #errors - check_unknown_keys(errors, path, raw, FIELD_KEYS) + -- The keys a field may carry depend on its type. + local standard = field_values.is_standard(raw.type) + check_unknown_keys(errors, path, raw, standard and field_values.KEYS[raw.type] or FIELD_KEYS) local id = raw.id if id == nil then @@ -135,8 +151,9 @@ local function normalize_field(errors: {any}, path: string, raw: any, seen_ids: if raw.type == nil then add_error(errors, path .. ".type", "required", "A field type is required.") - elseif raw.type ~= "choice" then - add_error(errors, path .. ".type", "unsupported_type", "Only the \"choice\" type is supported.") + elseif raw.type ~= "choice" and not standard then + add_error(errors, path .. ".type", "unsupported_type", + "Supported types: " .. field_values.TYPE_NAMES .. ".") end local label = check_text(errors, path .. ".label", raw.label, @@ -144,6 +161,15 @@ local function normalize_field(errors: {any}, path: string, raw: any, seen_ids: local description = check_text(errors, path .. ".description", raw.description, { required = false, max = definition.LIMITS.DESCRIPTION, strict = strict, multiline = true }) local required = check_boolean(errors, path .. ".required", raw.required) + + if standard then + local field = field_values.normalize(errors, path, raw, { + id = id, type = raw.type, label = label, description = description, required = required, + }, strict) + if #errors > before then return nil end + return field + end + local multiple = check_boolean(errors, path .. ".multiple", raw.multiple) local allow_other = check_boolean(errors, path .. ".allow_other", raw.allow_other) @@ -236,6 +262,14 @@ function definition.normalize(raw: any, opts: {stored: boolean?}?): (any, {any}? local field = normalize_field(errors, string.format("fields[%d]", index - 1), raw_field, seen_ids, strict) if field then table.insert(fields, field) end end + -- Every answer of the form must fit the request body cap: see + -- field_values.LIMITS.TEXT_BUDGET. Not applied to a stored form. + local budget = field_values.text_budget(fields) + if strict and budget > field_values.LIMITS.TEXT_BUDGET then + add_error(errors, "fields", "text_budget_exceeded", string.format( + "The text, textarea and email fields can hold %d characters in all, over the %d allowed. " + .. "Lower max_length on some of them, or use fewer.", budget, field_values.LIMITS.TEXT_BUDGET)) + end end if #errors > 0 then return nil, errors end @@ -443,7 +477,10 @@ end -- field_errors - { [field_id] = { { code, message } } } (first -- violation per field), or nil when valid -- form_errors - list of { code, message, field? } for problems that --- belong to no defined field, or nil when valid +-- belong to no defined field, or nil when valid. The +-- code "internal" is a fault in the stored form (a +-- pattern that no longer compiles), not in the +-- answers: the caller answers 500, not 422 function definition.validate_answers(def: any, answers: any): (any, any, any) local field_errors = {} local form_errors = {} @@ -473,17 +510,27 @@ function definition.validate_answers(def: any, answers: any): (any, any, any) local has_field_errors = false for _, field in ipairs(def.fields) do local value = answers[field.id] - local positions = option_index(field) local result, err = nil, nil - if value ~= nil then - if field.multiple then - result, err = check_multiple(field, value, positions) - else - result, err = check_single(field, value, positions) + if field.type == "choice" then + local positions = option_index(field) + if value ~= nil then + if field.multiple then + result, err = check_multiple(field, value, positions) + else + result, err = check_single(field, value, positions) + end + end + if not err and result == nil and field.required then + err = required_error() + end + else + -- A standard field decides `required` itself: an unchecked + -- required checkbox is a value (false) that still fails it. + result, err = field_values.check_answer(field, value) + if err and err.internal then + -- A fault in the stored form, not in the answers. + return nil, nil, { { code = "internal", field = field.id, message = err.message } } end - end - if not err and result == nil and field.required then - err = required_error() end if err then field_errors[field.id] = { err } @@ -499,11 +546,12 @@ function definition.validate_answers(def: any, answers: any): (any, any, any) return normalized, nil, nil end --- Encodes one normalized answer value (string, { value, note }, { other }, or --- a list of them) with a fixed key order, so equal answers encode equally. +-- Encodes one normalized answer value (a string, a number or a boolean, +-- { value, note }, { other }, or a list of choice answers) with a fixed key +-- order, so equal answers encode equally. local function encode_value(value: any): string - if type(value) == "string" then - return (json.encode(value)) + if type(value) ~= "table" then + return field_values.literal(value) end if is_array(value) and #value > 0 then local parts = {} diff --git a/src/forms/field_values.lua b/src/forms/field_values.lua new file mode 100644 index 0000000..f2fa18f --- /dev/null +++ b/src/forms/field_values.lua @@ -0,0 +1,671 @@ +local json = require("json") +local text = require("text") +local time = require("time") +local form_text = require("form_text") +local form_pattern = require("form_pattern") + +-- The standard field types of Session forms V2 (text, textarea, number, +-- email, date, checkbox): their definition keys, the check of one answer, the +-- canonical text of a stored answer and the descriptions the answer record +-- and read_form give an agent. definition.lua calls in here from the places +-- that are specific to a field's type; choice fields stay in definition.lua. +-- +-- Pure functions, no I/O. The rules are written down once more in the form +-- component (frontend/wippy-form), and both sides are tested against the one +-- fixture file test/forms/fixtures/v2_conformance.json. +-- +-- Answer checks return (value, nil) for an accepted answer (the normalized +-- value to store), (nil, nil) for a field left unanswered and (nil, err) for +-- a refusal, where err is { code, message }. A refusal never repeats the +-- value the user sent. +-- +-- An answer is checked in this order: its type, its raw size (a value longer +-- than any valid one is refused before anything scans it), UTF-8, control +-- characters, whether it is blank, its length, and last its pattern. + +local field_values = {} + +field_values.LIMITS = { + TEXT_DEFAULT = 500, + TEXT_MAX = 2000, + TEXTAREA_MAX = 8000, + -- The sum of the largest answers of all text, textarea and email fields + -- of one form. With the 20 field limit and four bytes per character this + -- keeps every valid submission inside the 64 KiB request cap. + TEXT_BUDGET = 16000, + EMAIL_MAX = 254, + EMAIL_LOCAL_MAX = 64, + PLACEHOLDER = 200, + NUMBER_MAGNITUDE = 1e12, + NUMBER_DECIMALS = 6, +} + +-- A valid answer is at most this many UTF-8 characters, four bytes each. +local BYTES_PER_CHARACTER = 4 +-- An email is trimmed, so it may carry surrounding whitespace that a valid +-- address does not; this bounds the scan of such a value. +local RAW_EMAIL_MULTIPLIER = 8 +local RAW_DATE_BYTES = 512 + +local COMMON_KEYS = { id = true, type = true, label = true, description = true, required = true } + +local function keys_of(extra: {string}): {[string]: boolean} + local keys: {[string]: boolean} = {} + for key in pairs(COMMON_KEYS) do keys[key] = true end + for _, key in ipairs(extra) do keys[key] = true end + return keys +end + +-- The keys a field of each type may carry. Anything else is `unknown_key`. +field_values.KEYS = { + text = keys_of({ "min_length", "max_length", "pattern", "placeholder", "default" }), + textarea = keys_of({ "min_length", "max_length", "placeholder", "default" }), + number = keys_of({ "min", "max", "step", "integer", "default" }), + email = keys_of({ "default" }), + date = keys_of({ "min", "max", "default" }), + checkbox = keys_of({ "default" }), +} + +field_values.TYPE_NAMES = "choice, text, textarea, number, email, date, checkbox" + +function field_values.is_standard(type_name: any): boolean + return type(type_name) == "string" and field_values.KEYS[type_name] ~= nil +end + +local function refuse(code: string, message: string): (any, any) + return nil, { code = code, message = message } +end + +local function unanswered(field: any): (any, any) + if field.required then + return nil, { code = "required", message = "This question requires an answer." } + end + return nil, nil +end + +--------------------------------------------------------------------------- +-- Decimal numbers +-- +-- Step checks and the canonical text of a number work on the number's +-- shortest decimal digits, not on float or integer arithmetic: the runtime +-- compares and takes remainders of integers through 64-bit floats, which is +-- not exact above 2^53, and a value of 1e12 with six decimals is 1e18 once +-- scaled. Digit strings of at most 19 digits are added, subtracted, compared +-- and divided here as plain schoolbook arithmetic. +--------------------------------------------------------------------------- + +-- A finite number as (negative, digits, power): its magnitude is the integer +-- written by `digits` (no leading zeros, "0" for zero) times 10^power, with +-- the fewest digits that read back as the same number. +local function decimal_of(value: number): (boolean, string, integer) + local negative = value < 0 + local magnitude = negative and -value or value + if magnitude == 0 then return false, "0", 0 end + local digits, power + if magnitude == math.floor(magnitude) then + digits, power = string.format("%d", magnitude), 0 + else + -- The shortest of 2 to 17 significant digits that parses back to the + -- same number (tonumber needs the point in the mantissa). + local formatted + for precision = 1, 16 do + formatted = string.format("%." .. precision .. "e", magnitude) + if tonumber(formatted) == magnitude then break end + end + local mantissa, sign, exponent = string.match(formatted, "^(%d%.%d+)e([+-])(%d+)$") + digits = (string.gsub(mantissa :: string, "%.", "")) + local exponent_value = math.tointeger(tonumber(exponent)) :: integer + if sign == "-" then exponent_value = -exponent_value end + power = exponent_value - (#digits - 1) + end + -- Drop trailing zeros into the power. + local trimmed = (string.gsub(digits, "0+$", "")) + power = power + (#digits - #trimmed) + return negative, trimmed, power +end + +-- How many digits a number has after the decimal point. +local function decimals_of(value: number): integer + local _, _, power = decimal_of(value) + return power < 0 and -power or 0 +end + +-- The canonical JSON text of a number: an integer when it is whole, else its +-- shortest decimal digits with no exponent. json.encode is not used: it +-- writes an exponent for some magnitudes and not for others, depending on +-- where the number sits in the document. +function field_values.number_text(value: number): string + local negative, digits, power = decimal_of(value) + if digits == "0" then return "0" end + local body + if power >= 0 then + body = digits .. string.rep("0", power) + else + local point = #digits + power + if point > 0 then + body = string.sub(digits, 1, point) .. "." .. string.sub(digits, point + 1) + else + body = "0." .. string.rep("0", -point) .. digits + end + end + return negative and ("-" .. body) or body +end + +-- Non-negative integers as digit strings without leading zeros. +local function compare_digits(a: string, b: string): integer + if #a ~= #b then return #a < #b and -1 or 1 end + if a == b then return 0 end + return a < b and -1 or 1 +end + +local function add_digits(a: string, b: string): string + local out = {} + local carry = 0 + local i, j = #a, #b + while i >= 1 or j >= 1 or carry > 0 do + local sum = carry + if i >= 1 then + sum = sum + (string.byte(a, i) - 48) + i = i - 1 + end + if j >= 1 then + sum = sum + (string.byte(b, j) - 48) + j = j - 1 + end + if sum >= 10 then + sum, carry = sum - 10, 1 + else + carry = 0 + end + table.insert(out, 1, string.char(sum + 48)) + end + return table.concat(out) +end + +-- a - b, for a >= b. +local function subtract_digits(a: string, b: string): string + local out = {} + local borrow = 0 + local j = #b + for i = #a, 1, -1 do + local difference = (string.byte(a, i) - 48) - borrow + if j >= 1 then + difference = difference - (string.byte(b, j) - 48) + j = j - 1 + end + if difference < 0 then + difference, borrow = difference + 10, 1 + else + borrow = 0 + end + table.insert(out, 1, string.char(difference + 48)) + end + local stripped = (string.gsub(table.concat(out), "^0+", "")) + return stripped == "" and "0" or stripped +end + +-- Whether `divisor` (not zero) divides `dividend` exactly: long division +-- keeping only the remainder. +local function divides(dividend: string, divisor: string): boolean + local remainder = "0" + for index = 1, #dividend do + local digit = string.sub(dividend, index, index) + remainder = remainder == "0" and digit or (remainder .. digit) + while compare_digits(remainder, divisor) >= 0 do + remainder = subtract_digits(remainder, divisor) + end + end + return remainder == "0" +end + +-- Whether `value` lies on the grid that starts at the field's min (zero when +-- there is none) and moves in steps of its step. The value must have no more +-- decimals than the step and the base do together; then |value - base|, scaled +-- to whole numbers, must be a multiple of the scaled step. +local function on_step(field: any, value: number): boolean + -- A stored step that is not positive has no grid; this also keeps the + -- division below from looping on a zero divisor. + if field.step <= 0 then return false end + local base = field.min or 0 + local step = field.step :: number + local places = decimals_of(step) + local base_places = decimals_of(base) + if base_places > places then places = base_places end + local value_negative, value_digits, value_power = decimal_of(value) + if value_power < -places then return false end + local base_negative, base_digits, base_power = decimal_of(base) + local _, step_digits, step_power = decimal_of(step) + + local function scaled(digits: string, power: integer): string + if digits == "0" then return "0" end + return digits .. string.rep("0", power + places) + end + local scaled_value = scaled(value_digits, value_power) + local scaled_base = scaled(base_digits, base_power) + local distance + if value_negative == base_negative then + if compare_digits(scaled_value, scaled_base) >= 0 then + distance = subtract_digits(scaled_value, scaled_base) + else + distance = subtract_digits(scaled_base, scaled_value) + end + else + distance = add_digits(scaled_value, scaled_base) + end + return divides(distance, scaled(step_digits, step_power)) +end + +--------------------------------------------------------------------------- +-- Answers +--------------------------------------------------------------------------- + +-- Compiled once: a pragmatic address syntax (WHATWG-style dot-atom local +-- part, DNS-label domain with at least one dot), ASCII only. No claim that +-- the address exists. +local EMAIL_PATTERN = "^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*" + .. "@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?" + .. "(?:\\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$" +local email_expression = nil + +local function check_free_text(field: any, value: any): (any, any) + if type(value) ~= "string" then + return refuse("invalid_type", "Must be text.") + end + if #value > field.max_length * BYTES_PER_CHARACTER then + return refuse("too_long", string.format("Must be at most %d characters.", field.max_length)) + end + local length = form_text.char_length(value) + if not length then + return refuse("invalid_text", "Must be valid UTF-8 text.") + end + if form_text.has_control_characters(value, field.type == "textarea") then + return refuse("invalid_text", field.type == "textarea" + and "Must not contain control characters other than tabs and line breaks." + or "Must not contain control characters or line breaks.") + end + -- Text is kept exactly as typed; only a value of nothing but whitespace + -- counts as no answer. + if form_text.is_whitespace_only(value) then return unanswered(field) end + if length > field.max_length then + return refuse("too_long", string.format("Must be at most %d characters.", field.max_length)) + end + if length < (field.min_length or 0) then + return refuse("too_short", string.format("Must be at least %d characters.", field.min_length)) + end + if field.pattern ~= nil then + local expression = form_pattern.compile(field.pattern :: string) + if not expression then + -- A stored pattern that no longer compiles is the form's fault, + -- not the user's. + return nil, { code = "internal", internal = true, + message = "This question's pattern cannot be checked." } + end + local matched = (expression :: any):match_string(value) + if not matched then + return refuse("pattern_mismatch", "Does not match the expected format.") + end + end + return value, nil +end + +local function check_email(field: any, value: any): (any, any) + if type(value) ~= "string" then + return refuse("invalid_type", "Must be text.") + end + if #value > field_values.LIMITS.EMAIL_MAX * RAW_EMAIL_MULTIPLIER then + return refuse("invalid_email", "Enter a valid email address.") + end + local address = form_text.trim(value) + if address == "" then return unanswered(field) end + local at = string.find(address, "@", 1, true) + if #address > field_values.LIMITS.EMAIL_MAX or (at ~= nil and at - 1 > field_values.LIMITS.EMAIL_LOCAL_MAX) then + return refuse("invalid_email", "Enter a valid email address.") + end + if email_expression == nil then + email_expression = text.regexp.compile(EMAIL_PATTERN) + end + local matched = (email_expression :: any):match_string(address) + if not matched then + return refuse("invalid_email", "Enter a valid email address.") + end + return address, nil +end + +local function check_number(field: any, value: any): (any, any) + if type(value) ~= "number" then + return refuse("invalid_type", "Must be a number.") + end + local limit = field_values.LIMITS.NUMBER_MAGNITUDE + if value ~= value or value > limit or value < -limit then + return refuse("invalid_number", "Enter a number between -1000000000000 and 1000000000000.") + end + local whole = value == math.floor(value) + if field.integer and not whole then + return refuse("not_integer", "Enter a whole number.") + end + -- A whole number is stored as an integer, however it was written. + local stored = whole and math.tointeger(value) or value + if field.min ~= nil and stored < field.min then + return refuse("below_min", "Is below the minimum.") + end + if field.max ~= nil and stored > field.max then + return refuse("above_max", "Is above the maximum.") + end + if field.step ~= nil and not on_step(field, stored) then + return refuse("not_on_step", "Is not one of the allowed steps.") + end + return stored, nil +end + +-- Whether `value` is a calendar date written YYYY-MM-DD in ASCII, from year +-- 0001 to 9999. +local function is_calendar_date(value: string): boolean + if not string.find(value, "^%d%d%d%d%-%d%d%-%d%d$") then return false end + if value < "0001-01-01" then return false end + local parsed = time.parse(time.DATE_ONLY, value) + return parsed ~= nil +end + +local function check_date(field: any, value: any): (any, any) + if type(value) ~= "string" then + return refuse("invalid_type", "Must be a date written YYYY-MM-DD.") + end + if #value > RAW_DATE_BYTES then + return refuse("invalid_date", "Enter a date as YYYY-MM-DD.") + end + if form_text.is_whitespace_only(value) then return unanswered(field) end + if not is_calendar_date(value) then + return refuse("invalid_date", "Enter a real date as YYYY-MM-DD.") + end + if field.min ~= nil and value < field.min then + return refuse("below_min", "Is before the earliest date.") + end + if field.max ~= nil and value > field.max then + return refuse("above_max", "Is after the latest date.") + end + return value, nil +end + +local function check_checkbox(field: any, value: any): (any, any) + if type(value) ~= "boolean" then + return refuse("invalid_type", "Must be true or false.") + end + -- An unchecked optional box is an answer; a required one must be checked. + if field.required and not value then + return refuse("required", "This question requires an answer.") + end + return value, nil +end + +local ANSWER_CHECKS = { + text = check_free_text, + textarea = check_free_text, + email = check_email, + number = check_number, + date = check_date, + checkbox = check_checkbox, +} + +-- Checks one answer to a standard field (a normalized field of one of the +-- standard types). `value` is nil when the user sent nothing. A refusal with +-- `internal` set is a fault in the stored form, not in the answer. +function field_values.check_answer(field: any, value: any): (any, any) + if value == nil then return unanswered(field) end + local check = ANSWER_CHECKS[field.type] + if check == nil then + return nil, { code = "internal", internal = true, message = "This question's type cannot be checked." } + end + return check(field, value) +end + +--------------------------------------------------------------------------- +-- Canonical and record text +--------------------------------------------------------------------------- + +-- The JSON literal of a normalized scalar answer: a string, a number or a +-- boolean. Used for the canonical answers (replay key, GET `data`) and the +-- answer record. +function field_values.literal(value: any): string + if type(value) == "boolean" then return value and "true" or "false" end + if type(value) == "number" then return field_values.number_text(value) end + return (json.encode(value)) +end + +-- The entries { key, value } that describe a field to an agent that lost the +-- original ask_user call, after "question" and "type": what it needs to +-- restate the question (answer_text.questions_json). A key with no value is +-- left out. +function field_values.question_entries(field: any): {any} + local entries = {} + local function add(key: string, value: any) + if type(value) == "number" then + table.insert(entries, { key, raw = field_values.number_text(value) }) + elseif value ~= nil then + table.insert(entries, { key, value }) + end + end + add("required", field.required == true) + if field.type == "text" or field.type == "textarea" then + add("min_length", field.min_length) + add("max_length", field.max_length) + add("pattern", field.pattern) + elseif field.type == "number" then + add("integer", field.integer == true) + add("min", field.min) + add("max", field.max) + add("step", field.step) + elseif field.type == "date" then + add("min", field.min) + add("max", field.max) + end + add("default", field.default) + return entries +end + +--------------------------------------------------------------------------- +-- Definition +--------------------------------------------------------------------------- + +local function is_whole(value: number): boolean + return value == math.floor(value) +end + +-- A whole number as an integer, whatever it was written as. +local function as_integer(value: number): number + return math.tointeger(value) or value +end + +-- Reads an optional whole-number key. `low`/`high` are the inclusive bounds +-- checked for new definitions (opts.strict). Returns the value, or nil when +-- absent or refused (the refusal is in errors). +local function read_whole(errors: {any}, path: string, value: any, low: number, high: number, strict: boolean): number? + if value == nil then return nil end + if type(value) ~= "number" or value ~= value or not is_whole(value) then + form_text.add_error(errors, path, "invalid_type", "Must be a whole number.") + return nil + end + if strict and (value < low or value > high) then + form_text.add_error(errors, path, "invalid_range", + string.format("Must be between %d and %d.", low, high)) + return nil + end + return as_integer(value) +end + +-- Reads an optional number bound or step. `code` is the refusal code for a +-- value outside the supported magnitude and decimals. +local function read_bound(errors: {any}, path: string, value: any, code: string, strict: boolean): number? + if value == nil then return nil end + if type(value) ~= "number" or value ~= value then + form_text.add_error(errors, path, "invalid_type", "Must be a number.") + return nil + end + if strict then + local limit = field_values.LIMITS.NUMBER_MAGNITUDE + if value > limit or value < -limit then + form_text.add_error(errors, path, code, "Must be between -1000000000000 and 1000000000000.") + return nil + end + if decimals_of(value) > field_values.LIMITS.NUMBER_DECIMALS then + form_text.add_error(errors, path, code, + string.format("Must have at most %d decimals.", field_values.LIMITS.NUMBER_DECIMALS)) + return nil + end + end + return as_integer(value) +end + +local function read_date_bound(errors: {any}, path: string, value: any, strict: boolean): string? + if value == nil then return nil end + if type(value) ~= "string" then + form_text.add_error(errors, path, "invalid_type", "Must be a date written YYYY-MM-DD.") + return nil + end + if strict and (#value > RAW_DATE_BYTES or not is_calendar_date(value)) then + form_text.add_error(errors, path, "invalid_range", "Must be a real date written YYYY-MM-DD.") + return nil + end + return value +end + +-- The default of a field: checked as an answer to the field (without the +-- default itself) and stored as that answer's normalized value. A default is +-- only ever shown prefilled; the server never submits it. Stored definitions +-- keep what they hold. +local function read_default(errors: {any}, path: string, raw: any, field: any, strict: boolean): any + local value = raw.default + if value == nil then return nil end + if not strict then return value end + if field.type == "checkbox" then + if type(value) ~= "boolean" then + form_text.add_error(errors, path .. ".default", "invalid_default", "A default must be true or false.") + return nil + end + if value then + form_text.add_error(errors, path .. ".default", "default_not_allowed", + "A checkbox cannot start checked: a default must not agree to anything for the user.") + return nil + end + return false + end + local candidate = {} + for key, item in pairs(field) do candidate[key] = item end + candidate.default = nil + candidate.required = false + local checked, err = field_values.check_answer(candidate, value) + if err then + form_text.add_error(errors, path .. ".default", "invalid_default", + "The default is not a valid answer for this question (" .. tostring(err.code) .. ").") + return nil + end + if checked == nil then + form_text.add_error(errors, path .. ".default", "invalid_default", "A default must not be blank.") + return nil + end + return checked +end + +local function normalize_text_like(errors: {any}, path: string, raw: any, field: any, strict: boolean) + local is_text = field.type == "text" + local cap = is_text and field_values.LIMITS.TEXT_MAX or field_values.LIMITS.TEXTAREA_MAX + local default_max = is_text and field_values.LIMITS.TEXT_DEFAULT or field_values.LIMITS.TEXTAREA_MAX + local min_length = read_whole(errors, path .. ".min_length", raw.min_length, 0, cap, strict) + local max_length = read_whole(errors, path .. ".max_length", raw.max_length, 1, cap, strict) + field.min_length = min_length or 0 + field.max_length = max_length or default_max + if strict and min_length ~= nil and field.min_length > field.max_length then + form_text.add_error(errors, path .. ".min_length", "invalid_range", "Must not be more than max_length.") + end + if raw.pattern ~= nil then + if type(raw.pattern) ~= "string" then + form_text.add_error(errors, path .. ".pattern", "invalid_type", "Must be a string.") + elseif strict then + local ok, code, message = form_pattern.check(raw.pattern) + if not ok then + form_text.add_error(errors, path .. ".pattern", code :: string, message :: string) + else + field.pattern = raw.pattern + end + else + field.pattern = raw.pattern + end + end + field.placeholder = (form_text.check_text(errors, path .. ".placeholder", raw.placeholder, + { required = false, max = field_values.LIMITS.PLACEHOLDER, strict = strict })) +end + +local function normalize_number(errors: {any}, path: string, raw: any, field: any, strict: boolean) + field.integer = form_text.check_boolean(errors, path .. ".integer", raw.integer) + field.min = read_bound(errors, path .. ".min", raw.min, "invalid_range", strict) + field.max = read_bound(errors, path .. ".max", raw.max, "invalid_range", strict) + field.step = read_bound(errors, path .. ".step", raw.step, "invalid_step", strict) + if not strict then return end + if field.step ~= nil and field.step <= 0 then + form_text.add_error(errors, path .. ".step", "invalid_step", "Must be more than zero.") + field.step = nil + return + end + if field.min ~= nil and field.max ~= nil and field.min > field.max then + form_text.add_error(errors, path .. ".min", "invalid_range", "Must not be more than max.") + end + if field.integer then + if field.step ~= nil and not is_whole(field.step) then + form_text.add_error(errors, path .. ".step", "invalid_step", "Must be a whole number for a whole-number question.") + elseif field.step ~= nil and field.min ~= nil and not is_whole(field.min) then + form_text.add_error(errors, path .. ".step", "invalid_step", + "Steps count from min, so min must be a whole number for a whole-number question.") + end + if field.min ~= nil and field.max ~= nil and math.ceil(field.min) > math.floor(field.max) then + form_text.add_error(errors, path .. ".min", "invalid_range", "No whole number lies between min and max.") + end + end +end + +local function normalize_date(errors: {any}, path: string, raw: any, field: any, strict: boolean) + field.min = read_date_bound(errors, path .. ".min", raw.min, strict) + field.max = read_date_bound(errors, path .. ".max", raw.max, strict) + if strict and field.min ~= nil and field.max ~= nil and field.min > field.max then + form_text.add_error(errors, path .. ".min", "invalid_range", "Must not be after max.") + end +end + +local NORMALIZERS = { + text = normalize_text_like, + textarea = normalize_text_like, + number = normalize_number, + date = normalize_date, +} + +-- Normalizes a field of a standard type. `base` carries the already checked +-- common keys { id, type, label, description, required }; the unknown-key +-- check and the common keys are the caller's. Returns the normalized field. +-- Anything wrong is appended to `errors`; the caller decides from the error +-- count whether the field is usable. +function field_values.normalize(errors: {any}, path: string, raw: any, base: any, strict: boolean): any + local field = { + id = base.id, + type = base.type, + label = base.label, + description = base.description, + required = base.required, + } + local normalizer = NORMALIZERS[base.type] + if normalizer then normalizer(errors, path, raw, field, strict) end + field.default = read_default(errors, path, raw, field, strict) + return field +end + +-- The most characters the answers of `fields` can hold in all. +function field_values.text_budget(fields: {any}): integer + local total = 0 + for _, field in ipairs(fields) do + if field.type == "text" or field.type == "textarea" then + total = total + (field.max_length or 0) + elseif field.type == "email" then + total = total + field_values.LIMITS.EMAIL_MAX + end + end + return total +end + +return field_values diff --git a/src/forms/form_artifact.lua b/src/forms/form_artifact.lua index d2c88fb..8318cf9 100644 --- a/src/forms/form_artifact.lua +++ b/src/forms/form_artifact.lua @@ -25,7 +25,7 @@ function form_artifact.build_content(def: any, state: string): (string?, string? specification = form_artifact.SPECIFICATION, wippy = { tagName = form_artifact.TAG_NAME }, props = { - ["form-version"] = definition.FORM_VERSION, + ["form-version"] = definition.version_of(def), definition = def, state = state, }, @@ -44,8 +44,10 @@ function form_artifact.new_meta(origin: any): any end -- Reads a form out of an artifact row (meta already decoded). Returns --- { definition, state, meta } or nil plus a reason when the artifact is not --- a supported form. +-- { definition, version, state, meta, content } or nil plus a reason when the +-- artifact is not a supported form. `version` is the form version stored in +-- the content (1 for choices only, 2 with standard fields; see +-- definition.version_of). function form_artifact.parse(artifact: any): (any, string?) local meta = type(artifact) == "table" and form_meta.of(artifact.meta) or nil if not meta or meta.version ~= form_artifact.META_VERSION then @@ -63,7 +65,8 @@ function form_artifact.parse(artifact: any): (any, string?) or type(content.props) ~= "table" then return nil, "Form content is not a wippy-form package" end - if content.props["form-version"] ~= definition.FORM_VERSION then + local version = content.props["form-version"] + if version ~= definition.VERSIONS.CHOICES and version ~= definition.VERSIONS.FIELDS then return nil, "Unsupported form version" end -- The stored definition was normalized at creation; normalizing again @@ -73,8 +76,14 @@ function form_artifact.parse(artifact: any): (any, string?) if not def then return nil, "Stored form definition is invalid: " .. definition.format_errors(def_errors) end + -- A record labeled with a lower version than its fields need would be + -- read by an older component as a form it can render. + if version < definition.version_of(def) then + return nil, "Unsupported form version" + end return { definition = def, + version = version, state = content.props.state, meta = meta, content = content, diff --git a/src/forms/form_pattern.lua b/src/forms/form_pattern.lua new file mode 100644 index 0000000..6b626b7 --- /dev/null +++ b/src/forms/form_pattern.lua @@ -0,0 +1,305 @@ +local text = require("text") +local form_text = require("form_text") + +-- The pattern an agent may put on a `text` field (V2): a small subset of +-- regular expression syntax that means the same in Go RE2 (the runtime's +-- text.regexp, used here) and in JavaScript with the `u` flag (the form +-- component, which runs the same pattern in the browser). +-- +-- A pattern must match the whole answer. It is compiled as ^(?:pattern)$, so +-- an alternation is matched as a whole and the author needs no anchors. +-- +-- The subset: +-- literals, `.`, `^` and `$` +-- classes [abc] [^abc] [a-z] with the escapes \d \w (ASCII) and escaped +-- punctuation; no POSIX classes, no [ inside a class +-- groups ( ) and (?: ), alternation | +-- quantifiers * + ? {n} {n,} {n,m}, each with an optional lazy ? +-- escapes \d \w and a backslash before one of ^ $ \ . * + ? ( ) [ ] { } | / +-- (and - inside a class); every other escape is outside the subset +-- +-- Left out because RE2 and JavaScript differ on them or because a browser's +-- backtracking engine can stall on them: lookaround, backreferences, named +-- groups, flags, \s \S \D \W \b \B \p \x \A \z and the other letter escapes. +-- +-- Bounds: 200 characters, 3 nested groups, repeat counts up to 100, and no +-- unbounded repeat (* + {n,}) of a group that itself holds an unbounded +-- repeat, such as (a+)+. +-- +-- check() answers with a code. "unsupported_pattern" means a well-formed +-- construct or bound the subset does not allow; "invalid_pattern" means the +-- pattern is malformed. + +local form_pattern = {} + +form_pattern.MAX_LENGTH = 200 +form_pattern.MAX_DEPTH = 3 +form_pattern.MAX_REPEAT = 100 + +local UNSUPPORTED = "unsupported_pattern" +local INVALID = "invalid_pattern" + +-- A backslash before one of these is that character, in and out of a class. +local ESCAPABLE = {} +for character in string.gmatch("^$\\.*+?()[]{}|/", ".") do ESCAPABLE[character] = true end + +-- Aborts the parse with a code and a message; check() catches it. +local function reject(code: string, message: string) + error({ pattern_error = true, code = code, message = message }, 0) +end + +-- The pattern as a list of characters and their code points. +local function split(pattern: string): ({string}, {integer}) + local chars, points = {}, {} + local index = 1 + local size = #pattern + while index <= size do + local point, next_index = form_text.decode_at(pattern, index) + table.insert(chars, string.sub(pattern, index, next_index - 1)) + table.insert(points, point :: integer) + index = next_index + end + return chars, points +end + +local function is_digit(character: string?): boolean + return character ~= nil and string.find(character, "^%d$") ~= nil +end + +local function parse(chars: {string}, points: {integer}) + local size = #chars + local position = 1 + + local parse_alternation + + -- After a backslash. Returns "class" for \d and \w, "char" for an escaped + -- punctuation character and its code point. + local function parse_escape(in_class: boolean): (string, integer) + local escaped = chars[position + 1] + if escaped == nil then + reject(INVALID, "The pattern ends with a backslash.") + end + if escaped == "d" or escaped == "w" then + position = position + 2 + return "class", 0 + end + if ESCAPABLE[escaped] or (in_class and escaped == "-") then + local point = points[position + 1] + position = position + 2 + return "char", point + end + reject(UNSUPPORTED, "The escape \\" .. escaped .. " is not supported. Supported: \\d, \\w and a " + .. "backslash before one of ^ $ \\ . * + ? ( ) [ ] { } | /" .. (in_class and " and -." or ".")) + return "char", 0 -- not reached: reject() raises + end + + local function parse_class() + position = position + 1 -- the [ + if chars[position] == "^" then position = position + 1 end + local atoms = 0 + while true do + local character = chars[position] + if character == nil then reject(INVALID, "A character class is not closed with ].") end + if character == "]" then + if atoms == 0 then reject(INVALID, "A character class is empty.") end + position = position + 1 + return + end + local kind, low + if character == "\\" then + kind, low = parse_escape(true) + elseif character == "[" then + reject(UNSUPPORTED, "Write [ as \\[ inside a character class. POSIX classes are not supported.") + elseif character == "-" then + if atoms > 0 and chars[position + 1] ~= "]" then + reject(INVALID, "Put - first or last in a character class, or write it as \\-.") + end + kind, low = "char", points[position] + position = position + 1 + else + kind, low = "char", points[position] + position = position + 1 + end + atoms = atoms + 1 + if chars[position] == "-" and chars[position + 1] ~= nil and chars[position + 1] ~= "]" then + if kind ~= "char" then + reject(INVALID, "A range cannot start at \\d or \\w.") + end + position = position + 1 -- the - + local high + local end_character = chars[position] + if end_character == "\\" then + local end_kind, end_point = parse_escape(true) + if end_kind ~= "char" then reject(INVALID, "A range cannot end at \\d or \\w.") end + high = end_point + elseif end_character == "[" then + reject(UNSUPPORTED, "Write [ as \\[ inside a character class. POSIX classes are not supported.") + else + high = points[position] + position = position + 1 + end + if (high :: integer) < (low :: integer) then + reject(INVALID, "A character range is out of order.") + end + end + end + end + + -- The braces after an atom: {n}, {n,} or {n,m}. Returns whether the + -- repeat is unbounded. + local function parse_braces(): boolean + position = position + 1 -- the { + local function read_number(): string? + local start = position + while is_digit(chars[position]) do position = position + 1 end + if position == start then return nil end + return table.concat(chars, "", start, position - 1) + end + local low = read_number() + if low == nil then reject(INVALID, "A { must start a repeat such as {2}, {2,} or {2,5}; write \\{ for a brace.") end + local high, unbounded = low, false + if chars[position] == "," then + position = position + 1 + high = read_number() + unbounded = high == nil + end + if chars[position] ~= "}" then + reject(INVALID, "A repeat such as {2,5} is not closed with }.") + end + position = position + 1 + local function too_big(digits: string): boolean + local trimmed = (string.gsub(digits, "^0+", "")) + return #trimmed > 3 or (#trimmed == 3 and trimmed > tostring(form_pattern.MAX_REPEAT)) + end + if too_big(low :: string) or (high ~= nil and too_big(high)) then + reject(UNSUPPORTED, string.format("A repeat count is at most %d.", form_pattern.MAX_REPEAT)) + end + if high ~= nil and #tostring(high) <= 3 and #tostring(low) <= 3 and tonumber(high) < tonumber(low) then + reject(INVALID, "A repeat range is out of order.") + end + return unbounded + end + + local function parse_sequence(depth: integer): boolean + local unbounded = false + while position <= size do + local character = chars[position] + if character == "|" then break end + if character == ")" then + if depth == 0 then reject(INVALID, "A ) has no matching (.") end + break + end + local quantifiable = true + local group_unbounded = false + if character == "(" then + position = position + 1 + if chars[position] == "?" then + if chars[position + 1] ~= ":" then + reject(UNSUPPORTED, "Lookaround, named groups and flags are not supported. " + .. "Only (?: ) groups are.") + end + position = position + 2 + end + if depth + 1 > form_pattern.MAX_DEPTH then + reject(UNSUPPORTED, string.format("Groups can be nested %d deep at most.", form_pattern.MAX_DEPTH)) + end + group_unbounded = parse_alternation(depth + 1) + if chars[position] ~= ")" then reject(INVALID, "A group is not closed with ).") end + position = position + 1 + elseif character == "[" then + parse_class() + elseif character == "\\" then + parse_escape(false) + elseif character == "^" or character == "$" then + position = position + 1 + quantifiable = false + elseif character == "." then + position = position + 1 + elseif character == "*" or character == "+" or character == "?" then + reject(INVALID, "A " .. character .. " has nothing to repeat.") + elseif character == "{" then + reject(INVALID, "A { must follow something to repeat; write \\{ for a brace.") + elseif character == "}" or character == "]" then + reject(INVALID, "Write " .. character .. " as \\" .. character .. ".") + else + position = position + 1 + end + + local quantifier = chars[position] + if quantifier == "*" or quantifier == "+" or quantifier == "?" or quantifier == "{" then + if not quantifiable then reject(INVALID, "A " .. quantifier .. " has nothing to repeat.") end + local repeats_forever + if quantifier == "{" then + repeats_forever = parse_braces() + else + position = position + 1 + repeats_forever = quantifier ~= "?" + end + if chars[position] == "?" then position = position + 1 end -- lazy + local next_character = chars[position] + if next_character == "*" or next_character == "+" or next_character == "?" or next_character == "{" then + reject(INVALID, "A repeat cannot be repeated again.") + end + if repeats_forever and group_unbounded then + reject(UNSUPPORTED, "A group that repeats without end cannot hold a repeat without end, " + .. "as in (a+)+.") + end + unbounded = unbounded or repeats_forever + end + unbounded = unbounded or group_unbounded + end + return unbounded + end + + parse_alternation = function(depth: integer): boolean + local unbounded = false + while true do + if parse_sequence(depth) then unbounded = true end + if chars[position] == "|" then + position = position + 1 + else + break + end + end + return unbounded + end + + parse_alternation(0) +end + +-- Checks a pattern against the subset. Returns true, or nil, a code +-- ("invalid_pattern" or "unsupported_pattern") and a plain message. +function form_pattern.check(pattern: any): (boolean?, string?, string?) + if type(pattern) ~= "string" or pattern == "" then + return nil, INVALID, "A pattern must not be empty." + end + local length = form_text.char_length(pattern) + if not length then return nil, INVALID, "A pattern must be valid UTF-8 text." end + if form_text.has_control_characters(pattern, false) then + return nil, INVALID, "A pattern must not contain control characters." + end + if length > form_pattern.MAX_LENGTH then + return nil, UNSUPPORTED, string.format("A pattern is at most %d characters.", form_pattern.MAX_LENGTH) + end + local chars, points = split(pattern) + local ok, failure = pcall(parse, chars, points) + if not ok then + if type(failure) == "table" and failure.pattern_error then + local rejection = failure :: any + return nil, rejection.code :: string, rejection.message :: string + end + error(failure, 0) + end + local re, compile_err = text.regexp.compile("^(?:" .. pattern .. ")$") + if not re then + return nil, INVALID, "The pattern could not be compiled: " .. tostring(compile_err) + end + return true, nil, nil +end + +-- The compiled full-match expression for a pattern, or nil and the error. +function form_pattern.compile(pattern: string): (any, any) + return text.regexp.compile("^(?:" .. pattern .. ")$") +end + +return form_pattern diff --git a/src/forms/state_service.lua b/src/forms/state_service.lua index b31bb7a..0b35275 100644 --- a/src/forms/state_service.lua +++ b/src/forms/state_service.lua @@ -191,7 +191,7 @@ function state_service.get_state(user_id, artifact_id) end return 200, ordered_json.object({ - { "form_version", definition.FORM_VERSION }, + { "form_version", form.version }, { "artifact_id", artifact.artifact_id }, { "session_id", artifact.session_id }, { "status", submission and form_artifact.STATE.IDLE or form_artifact.STATE.ACTIVE }, @@ -214,14 +214,16 @@ local function is_integer_number(value) return type(value) == "number" and value == math.floor(value) and value >= 1 end --- Checks the versioned envelope. Returns the envelope or (nil, message). -local function parse_envelope(raw_body) +-- Checks the versioned envelope. The request carries the version of the form +-- it answers (form.version), as read from GET. Returns the envelope or +-- (nil, message). +local function parse_envelope(raw_body, form_version) if raw_body == "" then return nil, "Request body is required" end local body, decode_err = json.decode(raw_body) if decode_err or type(body) ~= "table" or not definition.is_object(body) or next(body) == nil then return nil, "Request body must be a JSON object" end - if body.form_version ~= definition.FORM_VERSION then + if body.form_version ~= form_version then return nil, "Unsupported form_version" end local request_id = body.request_id @@ -333,7 +335,7 @@ function state_service.submit(user_id, artifact_id, raw_body) return failure(403, "forbidden", "You cannot answer forms in this session.") end - local envelope, envelope_err = parse_envelope(raw_body) + local envelope, envelope_err = parse_envelope(raw_body, form.version) if not envelope then return failure(400, "invalid_request", tostring(envelope_err)) end local request_id = envelope.request_id @@ -349,6 +351,11 @@ function state_service.submit(user_id, artifact_id, raw_body) end local normalized, field_errors, form_errors = definition.validate_answers(form.definition, envelope.answers) if not normalized then + if form_errors and form_errors[1].code == "internal" then + -- The form itself cannot be checked (a stored pattern that no + -- longer compiles): nothing the user can fix. + return failure(500, "internal", "This form cannot be checked right now.") + end return 422, validation_body(field_errors, form_errors) end diff --git a/test/forms/_index.yaml b/test/forms/_index.yaml index 3f7b09f..bbba4e8 100644 --- a/test/forms/_index.yaml +++ b/test/forms/_index.yaml @@ -20,6 +20,46 @@ entries: test: wippy.test:test method: run + - name: fixtures + kind: fs.directory + meta: + comment: Shared JSON fixtures for the form rules, such as the V2 conformance cases the form component is tested against too + directory: ./forms/fixtures + + - name: v2_conformance_test + kind: function.lua + meta: + name: V2 standard fields conformance + type: test + suite: session + comment: Every case of the shared V2 conformance fixture through the definition normalizer and the answer validator + source: file://v2_conformance_test.lua + modules: + - json + - fs + imports: + definition: wippy.session.forms:definition + test: wippy.test:test + method: run + + - name: field_values_test + kind: function.lua + meta: + name: Standard field rules + type: test + suite: session + comment: Decimal number text and step arithmetic, the pattern subset checker, a stored form whose pattern no longer compiles, the ask_user schema against the accepted keys, and the shared text helpers + source: file://field_values_test.lua + modules: + - json + - registry + imports: + field_values: wippy.session.forms:field_values + form_pattern: wippy.session.forms:form_pattern + form_text: wippy.session.forms:form_text + test: wippy.test:test + method: run + - name: runtime_probe_test kind: function.lua meta: diff --git a/test/forms/definition_test.lua b/test/forms/definition_test.lua index 708e6f5..bb93e65 100644 --- a/test/forms/definition_test.lua +++ b/test/forms/definition_test.lua @@ -352,7 +352,7 @@ local function define_tests() end) it("rejects unsupported types and non-boolean flags", function() - test.eq(first_code(form({ choice("a", { type = "text" }) })), "unsupported_type") + test.eq(first_code(form({ choice("a", { type = "rating" }) })), "unsupported_type") test.eq(first_code(form({ choice("a", { multiple = "yes" }) })), "invalid_type") test.eq(first_code("title"), "invalid_type") end) @@ -739,6 +739,213 @@ local function define_tests() test.eq(record.title, hostile) test.eq(record.answers.a.question, hostile) end) + + it("lists the questions of a choice-only form exactly as before standard fields existed", function() + test.eq(answer_text.questions_json(def), + '{"delivery":{"question":"Question delivery","multiple":false,"required":true,"allow_other":false,' + .. '"options":[{"value":"courier","label":"Courier"},{"value":"Locker","label":"Locker"}]},' + .. '"extras":{"question":"Question extras","multiple":true,"required":false,"allow_other":true,' + .. '"options":[{"value":"Gift wrap","label":"Gift wrap"},{"value":"Receipt","label":"Receipt"}]},' + .. '"note":{"question":"Question note","multiple":false,"required":false,"allow_other":false,' + .. '"options":[{"value":"Courier","label":"Courier"},{"value":"Pickup point","label":"Pickup point"}]}}') + end) + end) + + describe("standard fields in the version and the answer text", function() + local function standard_form() + return normalized(form({ + choice("delivery", { required = true }), + { id = "name", type = "text", label = "Your name", required = true, max_length = 40, + pattern = "[A-Za-z ]+", default = "Ann" }, + { id = "story", type = "textarea", label = "Story", max_length = 200 }, + { id = "parcels", type = "number", label = "Parcels", integer = true, min = 1, max = 10, step = 1, + default = 2 }, + { id = "email", type = "email", label = "Email", required = true }, + { id = "ready", type = "date", label = "Ready on", min = "2026-01-01", max = "2026-12-31" }, + { id = "terms", type = "checkbox", label = "I agree", required = true, default = false }, + }, { title = "Delivery details" })) + end + + it("is version 2 as soon as one field is not a choice, and version 1 otherwise", function() + test.eq(definition.version_of(normalized(form({ choice("a"), choice("b") }))), 1) + test.eq(definition.version_of(standard_form()), 2) + test.eq(definition.version_of(normalized(form({ choice("a"), + { id = "b", type = "checkbox", label = "B" } }))), 2) + end) + + it("lists the questions of a form with standard fields, with their types and limits", function() + test.eq(answer_text.questions_json(standard_form()), + '{"delivery":{"question":"Question delivery","type":"choice","multiple":false,"required":true,' + .. '"allow_other":false,"options":[{"value":"Courier","label":"Courier"},' + .. '{"value":"Pickup point","label":"Pickup point"}]},' + .. '"name":{"question":"Your name","type":"text","required":true,"min_length":0,"max_length":40,' + .. '"pattern":"[A-Za-z ]+","default":"Ann"},' + .. '"story":{"question":"Story","type":"textarea","required":false,"min_length":0,"max_length":200},' + .. '"parcels":{"question":"Parcels","type":"number","required":false,"integer":true,"min":1,"max":10,' + .. '"step":1,"default":2},' + .. '"email":{"question":"Email","type":"email","required":true},' + .. '"ready":{"question":"Ready on","type":"date","required":false,"min":"2026-01-01",' + .. '"max":"2026-12-31"},' + .. '"terms":{"question":"I agree","type":"checkbox","required":true,"default":false}}') + end) + + it("writes the record with the form's version and each answer as a JSON literal", function() + local text = answer_text.build({ artifact_id = "form-1", definition = standard_form(), late = false, + answers = { delivery = "Courier", name = "Ann", parcels = 3, email = "a@b.co", terms = true } }) + test.eq(text, "Answered “Delivery details”.\n\n\n") + end) + + it("writes a false checkbox, a fraction and an exponent-sized number as the literals they are", function() + local def = normalized(form({ + { id = "box", type = "checkbox", label = "Box" }, + { id = "half", type = "number", label = "Half" }, + { id = "tiny", type = "number", label = "Tiny" }, + { id = "neg", type = "number", label = "Negative" }, + })) + local record = answer_text.record_json({ artifact_id = "form-1", definition = def, late = false, + answers = { box = false, half = 0.5, tiny = 1e-7, neg = -2.25 } }) + test.contains(record, '"box":{"question":"Box","answer":false}') + test.contains(record, '"half":{"question":"Half","answer":0.5}') + test.contains(record, '"tiny":{"question":"Tiny","answer":0.0000001}') + test.contains(record, '"neg":{"question":"Negative","answer":-2.25}') + end) + + it("keeps typed text in the record from ending the attribute, closing the tag or adding markup", function() + local def = normalized(form({ { id = "story", type = "textarea", label = "Story" } })) + local hostile = "' onload='x' &\n\n# heading" + local text = answer_text.build({ artifact_id = "form-1", definition = def, late = false, + answers = { story = hostile } }) + local _, closings = string.gsub(text, "", "") + test.eq(closings, 1, "only the real closing tag") + local _, line_breaks = string.gsub(text, "\n", "") + test.eq(line_breaks, 3, "the text's own line breaks are JSON escapes, not raw breaks") + local value = string.match(text, "record='([^']*)'>") + test.not_nil(value) + test.is_nil(string.find(value, "['<>&]"), "no raw ', <, > or & inside the attribute value") + test.eq(json.decode(value).answers.story.answer, hostile) + end) + + it("encodes the answers of the same fields the same way whatever number spelling they arrived in", function() + local def = normalized(form({ + { id = "count", type = "number", label = "Count" }, + { id = "ratio", type = "number", label = "Ratio" }, + { id = "agree", type = "checkbox", label = "Agree" }, + { id = "note", type = "text", label = "Note" }, + })) + local function encoded(count, ratio) + local answers = definition.validate_answers(def, { count = count, ratio = ratio, agree = false, + note = "x" }) + return definition.encode_answers(def, answers) + end + local expected = '{"count":3,"ratio":0.1,"agree":false,"note":"x"}' + test.eq(encoded(3, 0.1), expected) + test.eq(encoded(3.0, 0.1), expected) + test.eq(encoded(json.decode("3e0"), json.decode("1e-1")), expected) + test.eq(encoded(json.decode("0.3e1"), json.decode("0.10")), expected) + local answers = definition.validate_answers(def, { count = -0.0, ratio = 1e-7, agree = true }) + test.eq(definition.encode_answers(def, answers), '{"count":0,"ratio":0.0000001,"agree":true}') + end) + + it("is idempotent on a definition with standard fields", function() + local once = standard_form() + local twice = normalized(once) + local function same(a: any, b: any): boolean + if type(a) ~= type(b) then return false end + if type(a) ~= "table" then return a == b end + for key, value in pairs(a) do + if not same(value, b[key]) then return false end + end + for key in pairs(b) do + if a[key] == nil then return false end + end + return true + end + test.is_true(same(once, twice)) + end) + + it("reads a stored definition without the rules that were added after it was created", function() + local raw = form({ + { id = "code", type = "text", label = "Code", pattern = "[A-Z]+", default = "ABC" }, + { id = "big", type = "textarea", label = "Big", default = "text" }, + { id = "more", type = "textarea", label = "More" }, + { id = "extra", type = "text", label = "Extra" }, + }) + test.not_nil(select(2, definition.normalize(raw)), "four fields use more than the text budget") + raw.fields[1].pattern = "(?=a)A" + raw.fields[1].default = "abc" + local stored, errors = definition.normalize(raw, { stored = true }) + test.is_nil(errors, definition.format_errors(errors)) + test.eq(stored.fields[1].pattern, "(?=a)A", "a pattern is kept as stored, not re-checked") + test.eq(stored.fields[1].default, "abc", "a default is kept as stored, not re-validated") + local _, unknown = definition.normalize(form({ { id = "a", type = "text", label = "A", rows = 3 } }), + { stored = true }) + test.eq(unknown[1].code, "unknown_key", "structure is still checked") + end) + + it("checks an answer in the order: type, size, text, control characters, blank, length, pattern", function() + local def = normalized(form({ + { id = "code", type = "text", label = "Code", pattern = "[a-z]+", max_length = 10 }, + })) + local function code_of(value: any): any + local _, field_errors = definition.validate_answers(def, { code = value }) + return field_errors and field_errors.code[1].code or nil + end + test.eq(code_of(5), "invalid_type") + test.eq(code_of(string.rep("a", 41)), "too_long", "a value longer than any valid one is refused unscanned") + test.eq(code_of("ab\255"), "invalid_text") + test.eq(code_of("ab\1"), "invalid_text") + test.eq(code_of("abcdefghijk"), "too_long") + test.eq(code_of("ab1"), "pattern_mismatch") + test.is_nil(code_of("abc")) + test.is_nil(code_of(" "), "blank and optional") + end) + + it("refuses invalid UTF-8 in an email as an invalid address", function() + local def = normalized(form({ { id = "mail", type = "email", label = "Mail" } })) + local _, field_errors = definition.validate_answers(def, { mail = "a@b.co\255" }) + test.eq(field_errors.mail[1].code, "invalid_email") + local _, long_errors = definition.validate_answers(def, { mail = string.rep("a", 3000) .. "@b.co" }) + test.eq(long_errors.mail[1].code, "invalid_email", "far longer than any address, refused unscanned") + end) + + it("names the path of every standard field error by its position", function() + local _, errors = definition.normalize(form({ + choice("a"), + { id = "b", type = "number", label = "B", min = 5, max = 1 }, + { id = "c", type = "text", label = "C", max_length = 9999 }, + })) + local found = {} + for _, item in ipairs(errors) do found[item.path] = item.code end + test.eq(found["fields[1].min"], "invalid_range") + test.eq(found["fields[2].max_length"], "invalid_range") + test.contains(definition.format_errors(errors), "fields[1].min: invalid_range: ") + end) + + it("reports a missing label and an invalid id on a standard field like on a choice", function() + local _, errors = definition.normalize({ title = "T", fields = { + { id = "Bad-Id", type = "date" }, { id = "ok", type = "checkbox", label = "" } } }) + local found = {} + for _, item in ipairs(errors) do found[item.path] = item.code end + test.eq(found["fields[0].id"], "invalid_id") + test.eq(found["fields[0].label"], "required") + test.eq(found["fields[1].label"], "blank") + end) + + it("lists the supported types when a type is not one of them", function() + local _, errors = definition.normalize(form({ { id = "a", type = "rating", label = "A" } })) + test.eq(errors[1].code, "unsupported_type") + test.contains(errors[1].message, "textarea") + test.contains(errors[1].message, "checkbox") + end) end) end diff --git a/test/forms/field_values_test.lua b/test/forms/field_values_test.lua new file mode 100644 index 0000000..d7e9047 --- /dev/null +++ b/test/forms/field_values_test.lua @@ -0,0 +1,214 @@ +local test = require("test") +local json = require("json") +local registry = require("registry") +local field_values = require("field_values") +local form_pattern = require("form_pattern") +local form_text = require("form_text") + +-- The parts of the standard field rules the shared conformance fixture +-- (v2_conformance_test.lua) does not reach: the decimal text and step +-- arithmetic on their own, the pattern checker's results, a stored form whose +-- pattern no longer compiles, and the text helpers. + +local function define_tests() + describe("number text", function() + it("writes whole numbers as integers and others as their shortest decimal, never with an exponent", function() + local cases = { + { 3, "3" }, { 3.0, "3" }, { -3, "-3" }, { 0, "0" }, { -0.0, "0" }, { 100, "100" }, + { 0.1, "0.1" }, { -0.5, "-0.5" }, { 2.5, "2.5" }, { -2.25, "-2.25" }, { 100.5, "100.5" }, + { 0.001, "0.001" }, { 0.000001, "0.000001" }, { 1e-7, "0.0000001" }, { 1.5e-6, "0.0000015" }, + { 1e12, "1000000000000" }, { -1e12, "-1000000000000" }, { 999999999999.5, "999999999999.5" }, + { 123456789.125, "123456789.125" }, { 0.30000000000000004, "0.30000000000000004" }, + { 1 / 3, "0.3333333333333333" }, + } + for _, case in ipairs(cases) do + test.eq(field_values.number_text(case[1]), case[2], tostring(case[2])) + end + end) + + it("reads back as the same number", function() + for _, value in ipairs({ 0.1, 0.2, 1 / 3, 123456789.125, 1e-7, 2.5e-5, 999999999999.5 }) do + test.eq(tonumber(field_values.number_text(value)), value) + end + end) + end) + + describe("number step", function() + local function on_step(step: number, value: number, min: number?): boolean + local stored, err = field_values.check_answer({ type = "number", step = step, min = min }, value) + test.is_true(err == nil or err.code == "not_on_step", "unexpected " .. tostring(err and err.code)) + return stored ~= nil + end + + it("accepts every multiple of a decimal step and refuses the values between", function() + for _, step in ipairs({ 0.1, 0.01, 0.25, 0.7, 3 }) do + local places = step == 3 and 0 or (step == 0.25 and 2 or (step == 0.01 and 2 or 1)) + local scale = 10 ^ places + local scaled_step = math.floor(step * scale + 0.5) + for count = 0, 120 do + -- The decimal text of count * step, built from digits. + local whole = count * scaled_step + local text + if places == 0 then + text = string.format("%d", whole) + else + local integer_part = math.floor(whole / scale) + local fraction = string.format("%d", whole - integer_part * scale) + text = string.format("%d", integer_part) .. "." .. string.rep("0", places - #fraction) .. fraction + end + local value = tonumber(text) :: number + test.is_true(on_step(step, value), text .. " is a multiple of " .. step) + if places > 0 then + local between = tonumber(text .. "5") :: number + test.is_false(on_step(step, between), text .. "5 lies between steps of " .. step) + end + end + end + end) + + it("counts steps from min, and keeps exact at the largest magnitudes", function() + test.is_true(on_step(0.3, 0.4, 0.1)) + test.is_false(on_step(0.3, 0.5, 0.1)) + test.is_true(on_step(0.000001, 1000000000000)) + test.is_true(on_step(0.000001, -1000000000000)) + test.is_true(on_step(0.000001, 999999999999.5)) + test.is_true(on_step(7, 999999999999)) + test.is_false(on_step(7, 1000000000000)) + test.is_true(on_step(1, -999999999999, -1000000000000)) + test.is_false(on_step(2, -999999999999, -1000000000000)) + end) + + it("treats a stored step that is not positive as having no grid", function() + local _, zero = field_values.check_answer({ type = "number", step = 0 }, 3) + test.eq(zero.code, "not_on_step") + local _, negative = field_values.check_answer({ type = "number", step = -1 }, 3) + test.eq(negative.code, "not_on_step") + end) + end) + + describe("pattern check", function() + local function check(pattern: any) + local ok, code, message = form_pattern.check(pattern) + return ok, code, message + end + + it("accepts patterns inside the subset", function() + for _, pattern in ipairs({ + "a", "[a-z]+", "\\d{3}-\\d{4}", "(Mr|Mrs)\\. [A-Z]", "[^0-9]*", "a|b|c", "(?:ab)+", "x{2,5}?", + "^abc$", "[\\w.+-]+", "\\.", "é+", "[a-]", "[-a]", + }) do + local ok, code = check(pattern) + test.is_true(ok, pattern .. " -> " .. tostring(code)) + end + end) + + it("tells a construct outside the subset from a malformed pattern", function() + local _, unsupported = check("(?=a)a") + test.eq(unsupported, "unsupported_pattern") + local _, malformed = check("(a") + test.eq(malformed, "invalid_pattern") + local _, bound = check("a{101}") + test.eq(bound, "unsupported_pattern") + local _, empty = check("") + test.eq(empty, "invalid_pattern") + local _, not_text = check(5) + test.eq(not_text, "invalid_pattern") + local _, bad_utf8 = check("a\255") + test.eq(bad_utf8, "invalid_pattern") + end) + + it("gives a plain message that names what to change", function() + local _, _, message = check("a\\sb") + test.contains(message, "\\s") + local _, _, nested = check("(a+)+") + test.contains(nested, "(a+)+") + end) + + it("compiles to a whole-text match", function() + local expression = form_pattern.compile("a|b") + test.is_true(((expression :: any):match_string("a"))) + test.is_false(((expression :: any):match_string("ab"))) + end) + + it("does not let an unbalanced pattern break out of the full-match wrapper", function() + local ok, code = check("a)(b") + test.is_nil(ok) + test.eq(code, "invalid_pattern") + end) + end) + + describe("a stored text field whose pattern no longer compiles", function() + it("is reported as a fault of the form, not of the answer", function() + local field = { type = "text", max_length = 10, min_length = 0, pattern = "(" } + local value, err = field_values.check_answer(field, "abc") + test.is_nil(value) + test.eq(err.code, "internal") + test.is_true(err.internal) + -- An empty answer never reaches the pattern. + local blank, blank_err = field_values.check_answer(field, "") + test.is_nil(blank) + test.is_nil(blank_err) + end) + + it("reports a field type it does not know the same way", function() + local _, err = field_values.check_answer({ type = "rating" }, "x") + test.eq(err.code, "internal") + end) + end) + + describe("the ask_user tool schema", function() + local function field_schema(): any + local entry, err = registry.get("wippy.session.forms:ask_user_tool") + test.is_nil(err) + local schema, decode_err = json.decode(tostring(entry.meta.input_schema)) + test.is_nil(decode_err) + return schema.properties.fields.items + end + + it("is one flat field object that names all seven types and does not require options", function() + local item = field_schema() + test.eq(item.additionalProperties, false) + local names = {} + for _, name in ipairs(item.properties.type.enum) do names[name] = true end + for _, name in ipairs({ "choice", "text", "textarea", "number", "email", "date", "checkbox" }) do + test.is_true(names[name], name) + end + test.eq(#item.properties.type.enum, 7) + table.sort(item.required) + test.eq(table.concat(item.required, ","), "id,label,type") + end) + + it("offers every key a field type accepts, so the model can write each of them", function() + local item = field_schema() + local keys = { id = true, type = true, label = true, description = true, required = true, + options = true, multiple = true, allow_other = true } + for _, type_keys in pairs(field_values.KEYS) do + for key in pairs(type_keys) do keys[key] = true end + end + for key in pairs(keys) do + test.not_nil(item.properties[key], "the schema describes " .. key) + test.not_nil(item.properties[key].description, "the schema explains " .. key) + end + for key in pairs(item.properties) do + test.is_true(keys[key], "the schema offers " .. key .. " but no type accepts it") + end + end) + end) + + describe("text helpers", function() + it("counts blank answer text by the JavaScript trim set, where a zero-width space is content", function() + test.is_true(form_text.is_whitespace_only("")) + test.is_true(form_text.is_whitespace_only(" \t\n\r")) + test.is_true(form_text.is_whitespace_only("\194\160\227\128\128\239\187\191")) + test.is_false(form_text.is_whitespace_only("\226\128\139"), "U+200B is not whitespace here") + test.is_false(form_text.is_whitespace_only(" a ")) + test.is_false(form_text.is_whitespace_only("\255"), "invalid UTF-8 is not blank") + end) + + it("stays blank for definition text that shows nothing", function() + test.is_true(form_text.is_blank("\226\128\139 "), "zero-width characters make a title blank") + end) + end) +end + +return test.run_cases(define_tests) diff --git a/test/forms/fixtures/v2_conformance.json b/test/forms/fixtures/v2_conformance.json new file mode 100644 index 0000000..5dda372 --- /dev/null +++ b/test/forms/fixtures/v2_conformance.json @@ -0,0 +1,483 @@ +[ +{"name":"text: a plain value is kept","field":{"type":"text"},"value":"hello","expect":{"same":true}}, +{"name":"text: outer and inner whitespace is kept as typed","field":{"type":"text"},"value":" a b ","expect":{"same":true}}, +{"name":"text: an absent optional value is unanswered","field":{"type":"text"},"expect":{"unanswered":true}}, +{"name":"text: a null optional value is unanswered","field":{"type":"text"},"value":null,"expect":{"unanswered":true}}, +{"name":"text: an empty optional value is unanswered","field":{"type":"text"},"value":"","expect":{"unanswered":true}}, +{"name":"text: a whitespace-only optional value is unanswered","field":{"type":"text"},"value":" ","expect":{"unanswered":true}}, +{"name":"text: no-break and ideographic spaces alone are unanswered","field":{"type":"text"},"value":"\u00a0\u3000\ufeff","expect":{"unanswered":true}}, +{"name":"text: a zero-width space is content","field":{"type":"text"},"value":"\u200b","expect":{"same":true}}, +{"name":"text: a required value that is absent","field":{"type":"text","required":true},"expect":{"error":"required"}}, +{"name":"text: a required value that is empty","field":{"type":"text","required":true},"value":"","expect":{"error":"required"}}, +{"name":"text: a required value of spaces only","field":{"type":"text","required":true},"value":" ","expect":{"error":"required"}}, +{"name":"text: a required value of no-break spaces only","field":{"type":"text","required":true},"value":"\u00a0\u00a0","expect":{"error":"required"}}, +{"name":"text: a required value with content","field":{"type":"text","required":true},"value":" x ","expect":{"same":true}}, +{"name":"text: a number is not text","field":{"type":"text"},"value":5,"expect":{"error":"invalid_type"}}, +{"name":"text: a boolean is not text","field":{"type":"text"},"value":true,"expect":{"error":"invalid_type"}}, +{"name":"text: an object is not text","field":{"type":"text"},"value":{},"expect":{"error":"invalid_type"}}, +{"name":"text: a list is not text","field":{"type":"text"},"value":["a"],"expect":{"error":"invalid_type"}}, +{"name":"text: the default limit is 500 characters","field":{"type":"text"},"value_concat":[{"repeat":"a","times":500}],"expect":{"same":true}}, +{"name":"text: 501 characters is over the default limit","field":{"type":"text"},"value_concat":[{"repeat":"a","times":501}],"expect":{"error":"too_long"}}, +{"name":"text: max_length is inclusive","field":{"type":"text","max_length":5},"value":"abcde","expect":{"same":true}}, +{"name":"text: one character over max_length","field":{"type":"text","max_length":5},"value":"abcdef","expect":{"error":"too_long"}}, +{"name":"text: max_length counts characters, not bytes","field":{"type":"text","max_length":5},"value":"ééééé","expect":{"same":true}}, +{"name":"text: six two-byte characters are over max_length 5","field":{"type":"text","max_length":5},"value":"éééééé","expect":{"error":"too_long"}}, +{"name":"text: an astral character counts once","field":{"type":"text","max_length":3},"value":"😀😀😀","expect":{"same":true}}, +{"name":"text: four astral characters are over max_length 3","field":{"type":"text","max_length":3},"value":"😀😀😀😀","expect":{"error":"too_long"}}, +{"name":"text: min_length rejects a shorter value","field":{"type":"text","min_length":3},"value":"ab","expect":{"error":"too_short"}}, +{"name":"text: min_length is inclusive","field":{"type":"text","min_length":3},"value":"abc","expect":{"same":true}}, +{"name":"text: min_length counts the text untrimmed","field":{"type":"text","min_length":3},"value":" a","expect":{"same":true}}, +{"name":"text: a whitespace-only optional value stays unanswered under min_length","field":{"type":"text","min_length":5},"value":" ","expect":{"unanswered":true}}, +{"name":"text: a whitespace-only required value is required under min_length","field":{"type":"text","min_length":5,"required":true},"value":" ","expect":{"error":"required"}}, +{"name":"text: a control character is refused","field":{"type":"text"},"value":"a\u0001b","expect":{"error":"invalid_text"}}, +{"name":"text: a line break is refused","field":{"type":"text"},"value":"a\nb","expect":{"error":"invalid_text"}}, +{"name":"text: a tab is refused","field":{"type":"text"},"value":"a\tb","expect":{"error":"invalid_text"}}, +{"name":"text: a carriage return is refused","field":{"type":"text"},"value":"a\rb","expect":{"error":"invalid_text"}}, +{"name":"text: a delete character is refused","field":{"type":"text"},"value":"a\u007fb","expect":{"error":"invalid_text"}}, +{"name":"text: a C1 control is refused","field":{"type":"text"},"value":"a\u0085b","expect":{"error":"invalid_text"}}, +{"name":"text: a bidi override is refused","field":{"type":"text"},"value":"a\u202eb","expect":{"error":"invalid_text"}}, +{"name":"text: a bidi isolate is refused","field":{"type":"text"},"value":"a\u2066b","expect":{"error":"invalid_text"}}, +{"name":"text: a line separator is refused","field":{"type":"text"},"value":"a\u2028b","expect":{"error":"invalid_text"}}, +{"name":"text: a paragraph separator is refused","field":{"type":"text"},"value":"a\u2029b","expect":{"error":"invalid_text"}}, +{"name":"text: line breaks and spaces alone are a control character error, not unanswered","field":{"type":"text"},"value":"\n ","expect":{"error":"invalid_text"}}, +{"name":"text pattern: a full match is accepted","field":{"type":"text","pattern":"[a-z]+"},"value":"abc","expect":{"same":true}}, +{"name":"text pattern: trailing characters do not match","field":{"type":"text","pattern":"[a-z]+"},"value":"abc1","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: leading characters do not match (not a search)","field":{"type":"text","pattern":"[a-z]+"},"value":"1abc","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: the value is not trimmed before matching","field":{"type":"text","pattern":"[a-z]+"},"value":" abc","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: an unanswered optional value skips the pattern","field":{"type":"text","pattern":"[a-z]+"},"value":"","expect":{"unanswered":true}}, +{"name":"text pattern: a required empty value is required, not a mismatch","field":{"type":"text","pattern":"[a-z]+","required":true},"value":"","expect":{"error":"required"}}, +{"name":"text pattern: alternation is matched as a whole (first branch)","field":{"type":"text","pattern":"a|b"},"value":"a","expect":{"same":true}}, +{"name":"text pattern: alternation is matched as a whole (second branch)","field":{"type":"text","pattern":"a|b"},"value":"b","expect":{"same":true}}, +{"name":"text pattern: alternation does not match a longer value","field":{"type":"text","pattern":"a|b"},"value":"ab","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: an author's own anchors are harmless","field":{"type":"text","pattern":"^a$"},"value":"a","expect":{"same":true}}, +{"name":"text pattern: digits with a literal hyphen","field":{"type":"text","pattern":"\\d{3}-\\d{4}"},"value":"555-1234","expect":{"same":true}}, +{"name":"text pattern: digits without the hyphen","field":{"type":"text","pattern":"\\d{3}-\\d{4}"},"value":"5551234","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: a dot matches one character","field":{"type":"text","pattern":"a.c"},"value":"abc","expect":{"same":true}}, +{"name":"text pattern: a dot matches one astral character","field":{"type":"text","pattern":"a.c"},"value":"a😀c","expect":{"same":true}}, +{"name":"text pattern: a dot needs a character","field":{"type":"text","pattern":"a.c"},"value":"ac","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: a code","field":{"type":"text","pattern":"[A-Z]{2}[0-9]{4}","max_length":6},"value":"AB1234","expect":{"same":true}}, +{"name":"text pattern: length is checked before the pattern","field":{"type":"text","pattern":".*","max_length":3},"value":"abcd","expect":{"error":"too_long"}}, +{"name":"text pattern: a group with alternatives","field":{"type":"text","pattern":"(Mr|Mrs|Ms)\\. [A-Z][a-z]+"},"value":"Mrs. Smith","expect":{"same":true}}, +{"name":"text pattern: a group with alternatives, wrong case","field":{"type":"text","pattern":"(Mr|Mrs|Ms)\\. [A-Z][a-z]+"},"value":"Mr. smith","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: a negated class","field":{"type":"text","pattern":"[^0-9]+"},"value":"abc","expect":{"same":true}}, +{"name":"text pattern: a negated class rejects a digit","field":{"type":"text","pattern":"[^0-9]+"},"value":"ab1","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: a non-ASCII literal","field":{"type":"text","pattern":"é+"},"value":"ééé","expect":{"same":true}}, +{"name":"text pattern: a lazy quantifier still matches the whole value","field":{"type":"text","pattern":"a+?"},"value":"aaa","expect":{"same":true}}, +{"name":"text pattern: a trailing hyphen in a class is literal","field":{"type":"text","pattern":"[a-z-]+"},"value":"a-b","expect":{"same":true}}, +{"name":"text pattern: \\w is ASCII only","field":{"type":"text","pattern":"\\w+"},"value":"é","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: \\d is ASCII only","field":{"type":"text","pattern":"\\d+"},"value":"١٢٣","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: a class with a word escape and a dot","field":{"type":"text","pattern":"[\\w.]+"},"value":"a.b_c","expect":{"same":true}}, +{"name":"text pattern: a bounded repeat inside","field":{"type":"text","pattern":"x{2,3}"},"value":"xx","expect":{"same":true}}, +{"name":"text pattern: a bounded repeat, too few","field":{"type":"text","pattern":"x{2,3}"},"value":"x","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: a bounded repeat, too many","field":{"type":"text","pattern":"x{2,3}"},"value":"xxxx","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: an optional atom","field":{"type":"text","pattern":"a?b"},"value":"b","expect":{"same":true}}, +{"name":"text pattern: an optional atom, too many","field":{"type":"text","pattern":"a?b"},"value":"aab","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: an escaped metacharacter","field":{"type":"text","pattern":"a\\.b"},"value":"a.b","expect":{"same":true}}, +{"name":"text pattern: an escaped metacharacter is not a wildcard","field":{"type":"text","pattern":"a\\.b"},"value":"axb","expect":{"error":"pattern_mismatch"}}, +{"name":"text pattern: a pattern is accepted and kept as written","field":{"type":"text","pattern":"[a-z]+"},"expect":{"normalized":{"pattern":"[a-z]+"}}}, +{"name":"text pattern: the longest accepted pattern is 200 characters","field":{"type":"text","pattern":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},"expect":{"definition_ok":true}}, +{"name":"text pattern: 201 characters is over the bound","field":{"type":"text","pattern":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a repeat count of 100 is accepted","field":{"type":"text","pattern":"a{100}"},"expect":{"definition_ok":true}}, +{"name":"text pattern: a repeat range up to 100 is accepted","field":{"type":"text","pattern":"a{1,100}"},"expect":{"definition_ok":true}}, +{"name":"text pattern: a repeat count of 101 is over the bound","field":{"type":"text","pattern":"a{101}"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a repeat range past 100 is over the bound","field":{"type":"text","pattern":"a{1,101}"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: three nested groups are accepted","field":{"type":"text","pattern":"(((a)))"},"expect":{"definition_ok":true}}, +{"name":"text pattern: four nested groups are over the bound","field":{"type":"text","pattern":"((((a))))"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a bounded repeat of a group with an unbounded repeat is accepted","field":{"type":"text","pattern":"(a+){1,5}"},"expect":{"definition_ok":true}}, +{"name":"text pattern: an unbounded repeat of an alternation is accepted","field":{"type":"text","pattern":"(a|b)+"},"expect":{"definition_ok":true}}, +{"name":"text pattern: an unbounded repeat of a plain group is accepted","field":{"type":"text","pattern":"(?:ab)*"},"expect":{"definition_ok":true}}, +{"name":"text pattern: classes with literal specials are accepted","field":{"type":"text","pattern":"[.][(][\\]][\\^a][a^]"},"expect":{"definition_ok":true}}, +{"name":"text pattern: escaped punctuation is accepted","field":{"type":"text","pattern":"\\.\\/\\\\\\|\\(\\)\\[\\]\\{\\}\\*\\+\\?\\^\\$"},"expect":{"definition_ok":true}}, +{"name":"text pattern: an escaped hyphen inside a class is accepted","field":{"type":"text","pattern":"[a\\-z]+"},"expect":{"definition_ok":true}}, +{"name":"text pattern: a class range and a word escape","field":{"type":"text","pattern":"[a-z0-9._-]+"},"expect":{"definition_ok":true}}, +{"name":"text pattern: lookahead is not supported","field":{"type":"text","pattern":"(?=a)a"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: negative lookahead is not supported","field":{"type":"text","pattern":"(?!a)b"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: lookbehind is not supported","field":{"type":"text","pattern":"(?<=a)b"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a backreference is not supported","field":{"type":"text","pattern":"(a)\\1"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a named group is not supported","field":{"type":"text","pattern":"(?Pa)"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a named group in the other spelling is not supported","field":{"type":"text","pattern":"(?a)"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an inline flag is not supported","field":{"type":"text","pattern":"(?i)a"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a flag group is not supported","field":{"type":"text","pattern":"(?i:a)"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\s is not supported","field":{"type":"text","pattern":"a\\sb"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\S is not supported","field":{"type":"text","pattern":"\\S+"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\D is not supported","field":{"type":"text","pattern":"\\D+"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\W is not supported","field":{"type":"text","pattern":"\\W+"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\b is not supported","field":{"type":"text","pattern":"\\ba"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\B is not supported","field":{"type":"text","pattern":"\\Ba"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\p{L} is not supported","field":{"type":"text","pattern":"\\p{L}+"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\pL is not supported","field":{"type":"text","pattern":"\\pL+"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a POSIX class is not supported","field":{"type":"text","pattern":"[[:alpha:]]+"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\x is not supported","field":{"type":"text","pattern":"\\x41"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\n is not supported","field":{"type":"text","pattern":"a\\nb"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\A is not supported","field":{"type":"text","pattern":"\\Aa"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: \\z is not supported","field":{"type":"text","pattern":"a\\z"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an escaped letter that means nothing is not supported","field":{"type":"text","pattern":"\\q"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an escaped hyphen outside a class is not supported","field":{"type":"text","pattern":"a\\-b"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an escaped underscore is not supported","field":{"type":"text","pattern":"a\\_b"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an unescaped bracket inside a class is not supported","field":{"type":"text","pattern":"[a[]"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: nested unbounded repeats are not supported","field":{"type":"text","pattern":"(a+)+"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: nested star repeats are not supported","field":{"type":"text","pattern":"(a*)*"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: nested repeats through a plain group are not supported","field":{"type":"text","pattern":"(?:a+)+b"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: nested repeats through an alternation are not supported","field":{"type":"text","pattern":"(a|b+)*"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: nested repeats deeper inside a group are not supported","field":{"type":"text","pattern":"((a+)b)+"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an open-ended range repeat of a group with an unbounded repeat is not supported","field":{"type":"text","pattern":"(a+|b){2,}"},"expect":{"definition_error":"unsupported_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an unclosed group is invalid","field":{"type":"text","pattern":"(a"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an unopened group is invalid","field":{"type":"text","pattern":"a)"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an unclosed class is invalid","field":{"type":"text","pattern":"[a"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a stray class bracket is invalid","field":{"type":"text","pattern":"a]"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a doubled quantifier is invalid","field":{"type":"text","pattern":"a**"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a quantifier with nothing to repeat is invalid","field":{"type":"text","pattern":"*a"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a plus with nothing to repeat is invalid","field":{"type":"text","pattern":"+a"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a question mark with nothing to repeat is invalid","field":{"type":"text","pattern":"?a"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a quantifier after an anchor is invalid","field":{"type":"text","pattern":"^*a"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an unfinished repeat is invalid","field":{"type":"text","pattern":"a{"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an unclosed repeat is invalid","field":{"type":"text","pattern":"a{2"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a repeat without a minimum is invalid","field":{"type":"text","pattern":"a{,3}"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a repeat range out of order is invalid","field":{"type":"text","pattern":"a{3,2}"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: two repeats in a row are invalid","field":{"type":"text","pattern":"a{2}{3}"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a stray closing brace is invalid","field":{"type":"text","pattern":"a}"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a class range out of order is invalid","field":{"type":"text","pattern":"[z-a]"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an empty class is invalid","field":{"type":"text","pattern":"a[]"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an empty negated class is invalid","field":{"type":"text","pattern":"[^]"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a class range ending in a class escape is invalid","field":{"type":"text","pattern":"[a-\\d]"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a class range starting at a class escape is invalid","field":{"type":"text","pattern":"[\\d-z]"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a trailing backslash is invalid","field":{"type":"text","pattern":"a\\"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: an empty pattern is invalid","field":{"type":"text","pattern":""},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text pattern: a pattern must be a string","field":{"type":"text","pattern":5},"expect":{"definition_error":"invalid_type","path":"fields[0].pattern"}}, +{"name":"text pattern: a control character in a pattern is invalid","field":{"type":"text","pattern":"a\u0001"},"expect":{"definition_error":"invalid_pattern","path":"fields[0].pattern"}}, +{"name":"text definition: step is not a text key","field":{"type":"text","step":1},"expect":{"definition_error":"unknown_key","path":"fields[0].step"}}, +{"name":"text definition: options is not a text key","field":{"type":"text","options":["a"]},"expect":{"definition_error":"unknown_key","path":"fields[0].options"}}, +{"name":"text definition: min is not a text key","field":{"type":"text","min":1},"expect":{"definition_error":"unknown_key","path":"fields[0].min"}}, +{"name":"text definition: multiple is not a text key","field":{"type":"text","multiple":true},"expect":{"definition_error":"unknown_key","path":"fields[0].multiple"}}, +{"name":"text definition: an unknown key is named","field":{"type":"text","rows":3},"expect":{"definition_error":"unknown_key","path":"fields[0].rows"}}, +{"name":"text definition: the defaults are filled in","field":{"type":"text"},"expect":{"normalized":{"min_length":0,"max_length":500,"required":false}}}, +{"name":"text definition: max_length 2000 is the cap","field":{"type":"text","max_length":2000},"expect":{"normalized":{"max_length":2000}}}, +{"name":"text definition: max_length 2001 is over the cap","field":{"type":"text","max_length":2001},"expect":{"definition_error":"invalid_range","path":"fields[0].max_length"}}, +{"name":"text definition: max_length 0 is below the floor","field":{"type":"text","max_length":0},"expect":{"definition_error":"invalid_range","path":"fields[0].max_length"}}, +{"name":"text definition: max_length must be a number","field":{"type":"text","max_length":"5"},"expect":{"definition_error":"invalid_type","path":"fields[0].max_length"}}, +{"name":"text definition: max_length must be a whole number","field":{"type":"text","max_length":2.5},"expect":{"definition_error":"invalid_type","path":"fields[0].max_length"}}, +{"name":"text definition: min_length must be a number","field":{"type":"text","min_length":"1"},"expect":{"definition_error":"invalid_type","path":"fields[0].min_length"}}, +{"name":"text definition: min_length must be a whole number","field":{"type":"text","min_length":1.5},"expect":{"definition_error":"invalid_type","path":"fields[0].min_length"}}, +{"name":"text definition: min_length cannot be negative","field":{"type":"text","min_length":-1},"expect":{"definition_error":"invalid_range","path":"fields[0].min_length"}}, +{"name":"text definition: min_length cannot pass the cap","field":{"type":"text","min_length":2001},"expect":{"definition_error":"invalid_range","path":"fields[0].min_length"}}, +{"name":"text definition: min_length cannot pass max_length","field":{"type":"text","min_length":10,"max_length":5},"expect":{"definition_error":"invalid_range","path":"fields[0].min_length"}}, +{"name":"text definition: min_length may equal max_length","field":{"type":"text","min_length":5,"max_length":5},"expect":{"normalized":{"min_length":5,"max_length":5}}}, +{"name":"text definition: a whole number spelled with a fraction is accepted","field":{"type":"text","max_length":50.0},"expect":{"normalized":{"max_length":50}}}, +{"name":"text definition: a placeholder is kept","field":{"type":"text","placeholder":"Your name"},"expect":{"normalized":{"placeholder":"Your name"}}}, +{"name":"text definition: a placeholder must be a string","field":{"type":"text","placeholder":5},"expect":{"definition_error":"invalid_type","path":"fields[0].placeholder"}}, +{"name":"text definition: a placeholder is at most 200 characters","field":{"type":"text","placeholder":"ppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp"},"expect":{"definition_error":"too_long","path":"fields[0].placeholder"}}, +{"name":"text definition: a placeholder takes no line break","field":{"type":"text","placeholder":"a\nb"},"expect":{"definition_error":"control_characters","path":"fields[0].placeholder"}}, +{"name":"text default: a valid default is kept","field":{"type":"text","default":"abc"},"expect":{"normalized":{"default":"abc"}}}, +{"name":"text default: a default is kept as typed, including its spaces","field":{"type":"text","default":" abc "},"expect":{"normalized":{"default":" abc "}}}, +{"name":"text default: a default over the limit","field":{"type":"text","default":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"text default: a default of the wrong type","field":{"type":"text","default":5},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"text default: a blank default is not an answer","field":{"type":"text","default":" "},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"text default: a default must match the pattern","field":{"type":"text","pattern":"[a-z]+","default":"A1"},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"text default: a default that matches the pattern","field":{"type":"text","pattern":"[a-z]+","default":"ab"},"expect":{"normalized":{"default":"ab"}}}, +{"name":"text default: a default below min_length","field":{"type":"text","min_length":3,"default":"ab"},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"text default: a default with a control character","field":{"type":"text","default":"a\nb"},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"text default: a required field may have a default","field":{"type":"text","required":true,"default":"ab"},"expect":{"normalized":{"default":"ab","required":true}}}, +{"name":"textarea: line breaks are kept","field":{"type":"textarea"},"value":"a\nb","expect":{"same":true}}, +{"name":"textarea: a tab is kept","field":{"type":"textarea"},"value":"a\tb","expect":{"same":true}}, +{"name":"textarea: outer blank lines are kept","field":{"type":"textarea"},"value":"\nabc\n","expect":{"same":true}}, +{"name":"textarea: a carriage return is refused","field":{"type":"textarea"},"value":"a\r\nb","expect":{"error":"invalid_text"}}, +{"name":"textarea: another control character is refused","field":{"type":"textarea"},"value":"a\u0001b","expect":{"error":"invalid_text"}}, +{"name":"textarea: a C1 control is refused","field":{"type":"textarea"},"value":"a\u0085b","expect":{"error":"invalid_text"}}, +{"name":"textarea: a line separator is refused","field":{"type":"textarea"},"value":"a\u2028b","expect":{"error":"invalid_text"}}, +{"name":"textarea: line breaks and spaces alone are unanswered when optional","field":{"type":"textarea"},"value":"\n\n \t\n","expect":{"unanswered":true}}, +{"name":"textarea: line breaks and spaces alone are required when required","field":{"type":"textarea","required":true},"value":"\n\n ","expect":{"error":"required"}}, +{"name":"textarea: an absent required value","field":{"type":"textarea","required":true},"expect":{"error":"required"}}, +{"name":"textarea: the default limit is 8000 characters","field":{"type":"textarea"},"value_concat":[{"repeat":"a","times":8000}],"expect":{"same":true}}, +{"name":"textarea: 8001 characters is over the limit","field":{"type":"textarea"},"value_concat":[{"repeat":"a","times":8001}],"expect":{"error":"too_long"}}, +{"name":"textarea: a line break counts toward max_length","field":{"type":"textarea","max_length":4},"value":"ab\ncd","expect":{"error":"too_long"}}, +{"name":"textarea: a line break counts as one character","field":{"type":"textarea","max_length":5},"value":"ab\ncd","expect":{"same":true}}, +{"name":"textarea: min_length is checked","field":{"type":"textarea","min_length":10},"value":"short","expect":{"error":"too_short"}}, +{"name":"textarea: a number is not text","field":{"type":"textarea"},"value":1,"expect":{"error":"invalid_type"}}, +{"name":"textarea definition: the defaults are filled in","field":{"type":"textarea"},"expect":{"normalized":{"min_length":0,"max_length":8000}}}, +{"name":"textarea definition: max_length 8001 is over the cap","field":{"type":"textarea","max_length":8001},"expect":{"definition_error":"invalid_range","path":"fields[0].max_length"}}, +{"name":"textarea definition: pattern is not a textarea key","field":{"type":"textarea","pattern":"a"},"expect":{"definition_error":"unknown_key","path":"fields[0].pattern"}}, +{"name":"textarea definition: a placeholder is kept","field":{"type":"textarea","placeholder":"Tell us more"},"expect":{"normalized":{"placeholder":"Tell us more"}}}, +{"name":"textarea default: a multi-line default is kept","field":{"type":"textarea","default":"line one\nline two"},"expect":{"normalized":{"default":"line one\nline two"}}}, +{"name":"textarea default: a default over the limit","field":{"type":"textarea","max_length":5,"default":"abcdef"},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"email: a plain address","field":{"type":"email"},"value":"a@b.co","expect":{"same":true}}, +{"name":"email: case is preserved","field":{"type":"email"},"value":"A@B.CO","expect":{"same":true}}, +{"name":"email: an apostrophe in the local part","field":{"type":"email"},"value":"o'neil@example.org","expect":{"same":true}}, +{"name":"email: a tag and subdomains","field":{"type":"email"},"value":"a.b+tag@sub.example.co.uk","expect":{"same":true}}, +{"name":"email: an underscore and a hyphenated domain","field":{"type":"email"},"value":"a_b@c-d.com","expect":{"same":true}}, +{"name":"email: punycode in the domain","field":{"type":"email"},"value":"user@xn--bcher-kva.example","expect":{"same":true}}, +{"name":"email: outer spaces are trimmed","field":{"type":"email"},"value":" a@b.co ","expect":{"ok":"a@b.co"}}, +{"name":"email: a trailing line break is trimmed","field":{"type":"email"},"value":"a@b.co\n","expect":{"ok":"a@b.co"}}, +{"name":"email: no-break and ideographic spaces are trimmed","field":{"type":"email"},"value":"\u00a0a@b.co\u3000","expect":{"ok":"a@b.co"}}, +{"name":"email: an empty optional value is unanswered","field":{"type":"email"},"value":"","expect":{"unanswered":true}}, +{"name":"email: a whitespace-only optional value is unanswered","field":{"type":"email"},"value":" ","expect":{"unanswered":true}}, +{"name":"email: an absent optional value is unanswered","field":{"type":"email"},"expect":{"unanswered":true}}, +{"name":"email: a required value that is absent","field":{"type":"email","required":true},"expect":{"error":"required"}}, +{"name":"email: a required value of spaces only","field":{"type":"email","required":true},"value":" ","expect":{"error":"required"}}, +{"name":"email: no at sign","field":{"type":"email"},"value":"plain","expect":{"error":"invalid_email"}}, +{"name":"email: a domain without a dot","field":{"type":"email"},"value":"a@b","expect":{"error":"invalid_email"}}, +{"name":"email: no local part","field":{"type":"email"},"value":"@b.co","expect":{"error":"invalid_email"}}, +{"name":"email: no domain","field":{"type":"email"},"value":"a@","expect":{"error":"invalid_email"}}, +{"name":"email: two at signs","field":{"type":"email"},"value":"a@@b.co","expect":{"error":"invalid_email"}}, +{"name":"email: a space inside","field":{"type":"email"},"value":"a b@c.de","expect":{"error":"invalid_email"}}, +{"name":"email: an empty domain label","field":{"type":"email"},"value":"a@b..co","expect":{"error":"invalid_email"}}, +{"name":"email: a leading dot in the local part","field":{"type":"email"},"value":".a@b.co","expect":{"error":"invalid_email"}}, +{"name":"email: a trailing dot in the local part","field":{"type":"email"},"value":"a.@b.co","expect":{"error":"invalid_email"}}, +{"name":"email: two dots in the local part","field":{"type":"email"},"value":"a..b@c.de","expect":{"error":"invalid_email"}}, +{"name":"email: a domain label starting with a hyphen","field":{"type":"email"},"value":"a@-b.co","expect":{"error":"invalid_email"}}, +{"name":"email: a domain label ending with a hyphen","field":{"type":"email"},"value":"a@b-.co","expect":{"error":"invalid_email"}}, +{"name":"email: an underscore in the domain","field":{"type":"email"},"value":"a@b.c_d","expect":{"error":"invalid_email"}}, +{"name":"email: a trailing dot after the domain","field":{"type":"email"},"value":"a@b.co.","expect":{"error":"invalid_email"}}, +{"name":"email: a non-ASCII local part","field":{"type":"email"},"value":"é@b.co","expect":{"error":"invalid_email"}}, +{"name":"email: a non-ASCII domain","field":{"type":"email"},"value":"a@bé.co","expect":{"error":"invalid_email"}}, +{"name":"email: a control character at the end","field":{"type":"email"},"value":"a@b.co\u0001","expect":{"error":"invalid_email"}}, +{"name":"email: a control character inside","field":{"type":"email"},"value":"a\u0001@b.co","expect":{"error":"invalid_email"}}, +{"name":"email: a line break inside","field":{"type":"email"},"value":"a\n@b.co","expect":{"error":"invalid_email"}}, +{"name":"email: two addresses","field":{"type":"email"},"value":"a@b.co,c@d.ef","expect":{"error":"invalid_email"}}, +{"name":"email: angle brackets","field":{"type":"email"},"value":"","expect":{"error":"invalid_email"}}, +{"name":"email: a mailto prefix","field":{"type":"email"},"value":"mailto:a@b.co","expect":{"error":"invalid_email"}}, +{"name":"email: a comment in the local part","field":{"type":"email"},"value":"a(b)@c.de","expect":{"error":"invalid_email"}}, +{"name":"email: a quoted local part","field":{"type":"email"},"value":"\"a b\"@c.de","expect":{"error":"invalid_email"}}, +{"name":"email: an address literal","field":{"type":"email"},"value":"a@[1.2.3.4]","expect":{"error":"invalid_email"}}, +{"name":"email: a 64 character local part","field":{"type":"email"},"value_concat":[{"repeat":"a","times":64},"@b.co"],"expect":{"same":true}}, +{"name":"email: a 65 character local part","field":{"type":"email"},"value_concat":[{"repeat":"a","times":65},"@b.co"],"expect":{"error":"invalid_email"}}, +{"name":"email: a 63 character domain label","field":{"type":"email"},"value_concat":["a@",{"repeat":"x","times":63},".de"],"expect":{"same":true}}, +{"name":"email: a 64 character domain label","field":{"type":"email"},"value_concat":["a@",{"repeat":"x","times":64},".de"],"expect":{"error":"invalid_email"}}, +{"name":"email: 254 characters in all","field":{"type":"email"},"value_concat":[{"repeat":"a","times":64},"@",{"repeat":"x","times":63},".",{"repeat":"x","times":63},".",{"repeat":"x","times":61}],"expect":{"same":true}}, +{"name":"email: 255 characters in all","field":{"type":"email"},"value_concat":[{"repeat":"a","times":64},"@",{"repeat":"x","times":63},".",{"repeat":"x","times":63},".",{"repeat":"x","times":62}],"expect":{"error":"invalid_email"}}, +{"name":"email: a number is not an address","field":{"type":"email"},"value":5,"expect":{"error":"invalid_type"}}, +{"name":"email: a boolean is not an address","field":{"type":"email"},"value":true,"expect":{"error":"invalid_type"}}, +{"name":"email definition: placeholder is not an email key","field":{"type":"email","placeholder":"a@b.co"},"expect":{"definition_error":"unknown_key","path":"fields[0].placeholder"}}, +{"name":"email definition: min_length is not an email key","field":{"type":"email","min_length":3},"expect":{"definition_error":"unknown_key","path":"fields[0].min_length"}}, +{"name":"email definition: max_length is not an email key","field":{"type":"email","max_length":30},"expect":{"definition_error":"unknown_key","path":"fields[0].max_length"}}, +{"name":"email definition: pattern is not an email key","field":{"type":"email","pattern":"a"},"expect":{"definition_error":"unknown_key","path":"fields[0].pattern"}}, +{"name":"email default: a default is trimmed","field":{"type":"email","default":" A@B.CO "},"expect":{"normalized":{"default":"A@B.CO"}}}, +{"name":"email default: an invalid default","field":{"type":"email","default":"nope"},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"number: an integer","field":{"type":"number"},"value":3,"expect":{"ok":3}}, +{"name":"number: a float with a whole value is stored as an integer","field":{"type":"number"},"value":3.0,"expect":{"ok":3}}, +{"name":"number: exponent spelling","field":{"type":"number"},"value":100.0,"expect":{"ok":100}}, +{"name":"number: capital exponent spelling","field":{"type":"number"},"value":100.0,"expect":{"ok":100}}, +{"name":"number: a fraction","field":{"type":"number"},"value":0.1,"expect":{"ok":0.1}}, +{"name":"number: a long fraction","field":{"type":"number"},"value":0.30000000000000004,"expect":{"ok":0.30000000000000004}}, +{"name":"number: a negative fraction","field":{"type":"number"},"value":-2.5,"expect":{"ok":-2.5}}, +{"name":"number: zero is an answer","field":{"type":"number","required":true},"value":0,"expect":{"ok":0}}, +{"name":"number: negative zero is zero","field":{"type":"number"},"value":0,"expect":{"ok":0}}, +{"name":"number: negative zero with a fraction is zero","field":{"type":"number"},"value":-0.0,"expect":{"ok":0}}, +{"name":"number: a small exponent","field":{"type":"number"},"value":1e-07,"expect":{"ok":1e-07}}, +{"name":"number: one trillion is the largest magnitude","field":{"type":"number"},"value":1000000000000,"expect":{"ok":1000000000000}}, +{"name":"number: minus one trillion is the smallest magnitude","field":{"type":"number"},"value":-1000000000000,"expect":{"ok":-1000000000000}}, +{"name":"number: just over one trillion","field":{"type":"number"},"value":1000000000001,"expect":{"error":"invalid_number"}}, +{"name":"number: just under minus one trillion","field":{"type":"number"},"value":-1000000000001,"expect":{"error":"invalid_number"}}, +{"name":"number: ten trillion","field":{"type":"number"},"value":10000000000000.0,"expect":{"error":"invalid_number"}}, +{"name":"number: a huge value","field":{"type":"number"},"value":1e+21,"expect":{"error":"invalid_number"}}, +{"name":"number: a numeric string is not coerced","field":{"type":"number"},"value":"3","expect":{"error":"invalid_type"}}, +{"name":"number: a decimal string is not coerced","field":{"type":"number"},"value":"3.5","expect":{"error":"invalid_type"}}, +{"name":"number: an empty string is not a number","field":{"type":"number"},"value":"","expect":{"error":"invalid_type"}}, +{"name":"number: a boolean is not a number","field":{"type":"number"},"value":true,"expect":{"error":"invalid_type"}}, +{"name":"number: a list is not a number","field":{"type":"number"},"value":[1],"expect":{"error":"invalid_type"}}, +{"name":"number: an object is not a number","field":{"type":"number"},"value":{},"expect":{"error":"invalid_type"}}, +{"name":"number: an absent optional value is unanswered","field":{"type":"number"},"expect":{"unanswered":true}}, +{"name":"number: a null optional value is unanswered","field":{"type":"number"},"value":null,"expect":{"unanswered":true}}, +{"name":"number: an absent required value","field":{"type":"number","required":true},"expect":{"error":"required"}}, +{"name":"number min: a value below the minimum","field":{"type":"number","min":0},"value":-1,"expect":{"error":"below_min"}}, +{"name":"number min: the minimum itself is accepted","field":{"type":"number","min":0},"value":0,"expect":{"ok":0}}, +{"name":"number max: a value above the maximum","field":{"type":"number","max":10},"value":11,"expect":{"error":"above_max"}}, +{"name":"number max: the maximum itself is accepted","field":{"type":"number","max":10},"value":10,"expect":{"ok":10}}, +{"name":"number range: the lower end","field":{"type":"number","min":-5,"max":5},"value":-5,"expect":{"ok":-5}}, +{"name":"number range: the upper end","field":{"type":"number","min":-5,"max":5},"value":5,"expect":{"ok":5}}, +{"name":"number range: just below the lower end","field":{"type":"number","min":-5,"max":5},"value":-5.000001,"expect":{"error":"below_min"}}, +{"name":"number range: just above the upper end","field":{"type":"number","min":-5,"max":5},"value":5.000001,"expect":{"error":"above_max"}}, +{"name":"number range: a fractional minimum, just below","field":{"type":"number","min":0.5},"value":0.4,"expect":{"error":"below_min"}}, +{"name":"number range: a fractional minimum, equal","field":{"type":"number","min":0.5},"value":0.5,"expect":{"ok":0.5}}, +{"name":"number range: min equal to max accepts that value","field":{"type":"number","min":7,"max":7},"value":7,"expect":{"ok":7}}, +{"name":"number range: a value past the magnitude bound is invalid before the range","field":{"type":"number","max":10},"value":10000000000000.0,"expect":{"error":"invalid_number"}}, +{"name":"number integer: an integer","field":{"type":"number","integer":true},"value":3,"expect":{"ok":3}}, +{"name":"number integer: a float with a whole value is accepted and stored as an integer","field":{"type":"number","integer":true},"value":3.0,"expect":{"ok":3}}, +{"name":"number integer: exponent spelling of a whole value","field":{"type":"number","integer":true},"value":100.0,"expect":{"ok":100}}, +{"name":"number integer: a negative integer","field":{"type":"number","integer":true},"value":-3,"expect":{"ok":-3}}, +{"name":"number integer: a fraction","field":{"type":"number","integer":true},"value":3.5,"expect":{"error":"not_integer"}}, +{"name":"number integer: a tiny fraction","field":{"type":"number","integer":true},"value":0.1,"expect":{"error":"not_integer"}}, +{"name":"number integer: the largest magnitude","field":{"type":"number","integer":true},"value":1000000000000,"expect":{"ok":1000000000000}}, +{"name":"number integer: a fraction is reported before a bound","field":{"type":"number","integer":true,"min":10},"value":3.5,"expect":{"error":"not_integer"}}, +{"name":"number step: zero is on a whole step","field":{"type":"number","step":5},"value":0,"expect":{"ok":0}}, +{"name":"number step: a multiple","field":{"type":"number","step":5},"value":10,"expect":{"ok":10}}, +{"name":"number step: a negative multiple","field":{"type":"number","step":5},"value":-5,"expect":{"ok":-5}}, +{"name":"number step: a whole float multiple","field":{"type":"number","step":5},"value":15.0,"expect":{"ok":15}}, +{"name":"number step: not a multiple","field":{"type":"number","step":5},"value":3,"expect":{"error":"not_on_step"}}, +{"name":"number step: a fraction of a whole step","field":{"type":"number","step":5},"value":2.5,"expect":{"error":"not_on_step"}}, +{"name":"number step: a negative value off the step","field":{"type":"number","step":5},"value":-3,"expect":{"error":"not_on_step"}}, +{"name":"number step 0.1: one step","field":{"type":"number","step":0.1},"value":0.1,"expect":{"ok":0.1}}, +{"name":"number step 0.1: three steps","field":{"type":"number","step":0.1},"value":0.3,"expect":{"ok":0.3}}, +{"name":"number step 0.1: seven steps","field":{"type":"number","step":0.1},"value":0.7,"expect":{"ok":0.7}}, +{"name":"number step 0.1: ten steps","field":{"type":"number","step":0.1},"value":1,"expect":{"ok":1}}, +{"name":"number step 0.1: a negative multiple","field":{"type":"number","step":0.1},"value":-0.2,"expect":{"ok":-0.2}}, +{"name":"number step 0.1: two and a half steps","field":{"type":"number","step":0.1},"value":0.25,"expect":{"error":"not_on_step"}}, +{"name":"number step 0.1: half a step","field":{"type":"number","step":0.1},"value":0.05,"expect":{"error":"not_on_step"}}, +{"name":"number step 0.1: a float sum that is not exactly 0.3 is off the step","field":{"type":"number","step":0.1},"value":0.30000000000000004,"expect":{"error":"not_on_step"}}, +{"name":"number step 0.25: two steps","field":{"type":"number","step":0.25},"value":0.5,"expect":{"ok":0.5}}, +{"name":"number step 0.25: three steps","field":{"type":"number","step":0.25},"value":0.75,"expect":{"ok":0.75}}, +{"name":"number step 0.25: minus one step","field":{"type":"number","step":0.25},"value":-0.25,"expect":{"ok":-0.25}}, +{"name":"number step 0.25: off the step","field":{"type":"number","step":0.25},"value":0.3,"expect":{"error":"not_on_step"}}, +{"name":"number step 0.01: a price","field":{"type":"number","step":0.01},"value":1.15,"expect":{"ok":1.15}}, +{"name":"number step 0.01: a third decimal","field":{"type":"number","step":0.01},"value":1.155,"expect":{"error":"not_on_step"}}, +{"name":"number step 0.01: a whole value","field":{"type":"number","step":0.01},"value":100,"expect":{"ok":100}}, +{"name":"number step from min: the minimum","field":{"type":"number","min":1,"step":2},"value":1,"expect":{"ok":1}}, +{"name":"number step from min: two steps up","field":{"type":"number","min":1,"step":2},"value":5,"expect":{"ok":5}}, +{"name":"number step from min: a value between steps","field":{"type":"number","min":1,"step":2},"value":2,"expect":{"error":"not_on_step"}}, +{"name":"number step from min: below the minimum","field":{"type":"number","min":1,"step":2},"value":0,"expect":{"error":"below_min"}}, +{"name":"number step from a fractional min: below zero","field":{"type":"number","min":-0.5,"step":0.25},"value":-0.5,"expect":{"ok":-0.5}}, +{"name":"number step from a fractional min: crossing zero","field":{"type":"number","min":-0.5,"step":0.25},"value":0,"expect":{"ok":0}}, +{"name":"number step from a fractional min: off the step","field":{"type":"number","min":-0.5,"step":0.25},"value":0.1,"expect":{"error":"not_on_step"}}, +{"name":"number step from a fractional min: a different base than zero","field":{"type":"number","min":0.1,"step":0.3},"value":0.4,"expect":{"ok":0.4}}, +{"name":"number step from a fractional min: two steps up","field":{"type":"number","min":0.1,"step":0.3},"value":0.7,"expect":{"ok":0.7}}, +{"name":"number step from a fractional min: a different base, off the step","field":{"type":"number","min":0.1,"step":0.3},"value":0.5,"expect":{"error":"not_on_step"}}, +{"name":"number step: a large multiple of 7","field":{"type":"number","step":7},"value":999999999999,"expect":{"ok":999999999999}}, +{"name":"number step: one trillion is not a multiple of 7","field":{"type":"number","step":7},"value":1000000000000,"expect":{"error":"not_on_step"}}, +{"name":"number step: a small multiple of 7","field":{"type":"number","step":7},"value":7,"expect":{"ok":7}}, +{"name":"number step 0.000001: one step","field":{"type":"number","step":1e-06},"value":1e-06,"expect":{"ok":1e-06}}, +{"name":"number step 0.000001: one and a half steps","field":{"type":"number","step":1e-06},"value":1.5e-06,"expect":{"error":"not_on_step"}}, +{"name":"number step 0.000001: one trillion needs 19 digits scaled","field":{"type":"number","step":1e-06},"value":1000000000000,"expect":{"ok":1000000000000}}, +{"name":"number step 0.000001: a large value with a half","field":{"type":"number","step":1e-06},"value":999999999999.5,"expect":{"ok":999999999999.5}}, +{"name":"number step 0.000001: a seventh decimal","field":{"type":"number","step":1e-06},"value":1e-07,"expect":{"error":"not_on_step"}}, +{"name":"number step 0.000003: 333333 steps","field":{"type":"number","step":3e-06},"value":0.999999,"expect":{"ok":0.999999}}, +{"name":"number step 0.000003: off by one micro-unit","field":{"type":"number","step":3e-06},"value":0.999998,"expect":{"error":"not_on_step"}}, +{"name":"number step with integer: a multiple","field":{"type":"number","integer":true,"step":3},"value":9,"expect":{"ok":9}}, +{"name":"number step with integer: not a multiple","field":{"type":"number","integer":true,"step":3},"value":10,"expect":{"error":"not_on_step"}}, +{"name":"number step with integer: a fraction is not an integer first","field":{"type":"number","integer":true,"step":3},"value":7.5,"expect":{"error":"not_integer"}}, +{"name":"number step with max: the maximum on a step","field":{"type":"number","step":5,"max":20},"value":20,"expect":{"ok":20}}, +{"name":"number step with max: above the maximum","field":{"type":"number","step":5,"max":20},"value":25,"expect":{"error":"above_max"}}, +{"name":"number step with min: on a step but below the minimum","field":{"type":"number","step":5,"min":10},"value":5,"expect":{"error":"below_min"}}, +{"name":"number step with range: above the maximum is reported before the step","field":{"type":"number","step":5,"min":0,"max":10},"value":12,"expect":{"error":"above_max"}}, +{"name":"number step with range: inside the range but off the step","field":{"type":"number","step":5,"min":0,"max":10},"value":7,"expect":{"error":"not_on_step"}}, +{"name":"number definition: the defaults are filled in","field":{"type":"number"},"expect":{"normalized":{"integer":false,"required":false}}}, +{"name":"number definition: whole bounds written with a fraction are stored as integers","field":{"type":"number","min":3.0,"max":9.0,"step":3.0},"expect":{"normalized":{"min":3,"max":9,"step":3}}}, +{"name":"number definition: six decimals are accepted","field":{"type":"number","min":1e-06,"max":0.999999,"step":1e-06},"expect":{"normalized":{"min":1e-06,"max":0.999999,"step":1e-06}}}, +{"name":"number definition: min must be a number","field":{"type":"number","min":"0"},"expect":{"definition_error":"invalid_type","path":"fields[0].min"}}, +{"name":"number definition: max must be a number","field":{"type":"number","max":true},"expect":{"definition_error":"invalid_type","path":"fields[0].max"}}, +{"name":"number definition: step must be a number","field":{"type":"number","step":"1"},"expect":{"definition_error":"invalid_type","path":"fields[0].step"}}, +{"name":"number definition: integer must be a boolean","field":{"type":"number","integer":"yes"},"expect":{"definition_error":"invalid_type","path":"fields[0].integer"}}, +{"name":"number definition: min past the magnitude bound","field":{"type":"number","min":10000000000000.0},"expect":{"definition_error":"invalid_range","path":"fields[0].min"}}, +{"name":"number definition: max past the magnitude bound","field":{"type":"number","max":-10000000000000.0},"expect":{"definition_error":"invalid_range","path":"fields[0].max"}}, +{"name":"number definition: min with seven decimals","field":{"type":"number","min":1e-07},"expect":{"definition_error":"invalid_range","path":"fields[0].min"}}, +{"name":"number definition: max with seven decimals","field":{"type":"number","max":1.2345678},"expect":{"definition_error":"invalid_range","path":"fields[0].max"}}, +{"name":"number definition: min above max","field":{"type":"number","min":5,"max":4},"expect":{"definition_error":"invalid_range","path":"fields[0].min"}}, +{"name":"number definition: step zero","field":{"type":"number","step":0},"expect":{"definition_error":"invalid_step","path":"fields[0].step"}}, +{"name":"number definition: a negative step","field":{"type":"number","step":-1},"expect":{"definition_error":"invalid_step","path":"fields[0].step"}}, +{"name":"number definition: a step with seven decimals","field":{"type":"number","step":1e-07},"expect":{"definition_error":"invalid_step","path":"fields[0].step"}}, +{"name":"number definition: a step past the magnitude bound","field":{"type":"number","step":10000000000000.0},"expect":{"definition_error":"invalid_step","path":"fields[0].step"}}, +{"name":"number definition: an integer field needs a whole step","field":{"type":"number","integer":true,"step":0.5},"expect":{"definition_error":"invalid_step","path":"fields[0].step"}}, +{"name":"number definition: an integer field needs a whole step base","field":{"type":"number","integer":true,"min":0.5,"step":1},"expect":{"definition_error":"invalid_step","path":"fields[0].step"}}, +{"name":"number definition: an integer field needs a whole number inside its range","field":{"type":"number","integer":true,"min":0.2,"max":0.8},"expect":{"definition_error":"invalid_range","path":"fields[0].min"}}, +{"name":"number definition: an integer field with fractional bounds that hold a whole number","field":{"type":"number","integer":true,"min":0.5,"max":1.5},"expect":{"definition_ok":true}}, +{"name":"number definition: min_length is not a number key","field":{"type":"number","min_length":1},"expect":{"definition_error":"unknown_key","path":"fields[0].min_length"}}, +{"name":"number definition: placeholder is not a number key","field":{"type":"number","placeholder":"0"},"expect":{"definition_error":"unknown_key","path":"fields[0].placeholder"}}, +{"name":"number definition: pattern is not a number key","field":{"type":"number","pattern":"a"},"expect":{"definition_error":"unknown_key","path":"fields[0].pattern"}}, +{"name":"number definition: options is not a number key","field":{"type":"number","options":["a"]},"expect":{"definition_error":"unknown_key","path":"fields[0].options"}}, +{"name":"number default: a whole float is stored as an integer","field":{"type":"number","default":3.0},"expect":{"normalized":{"default":3}}}, +{"name":"number default: a fraction","field":{"type":"number","default":2.5},"expect":{"normalized":{"default":2.5}}}, +{"name":"number default: below the minimum","field":{"type":"number","min":5,"default":1},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"number default: above the maximum","field":{"type":"number","max":5,"default":9},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"number default: not a number","field":{"type":"number","default":"3"},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"number default: not an integer","field":{"type":"number","integer":true,"default":2.5},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"number default: off the step","field":{"type":"number","step":5,"default":3},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"number default: on the step","field":{"type":"number","step":5,"default":15},"expect":{"normalized":{"default":15}}}, +{"name":"date: a day","field":{"type":"date"},"value":"2023-12-31","expect":{"same":true}}, +{"name":"date: a leap day","field":{"type":"date"},"value":"2024-02-29","expect":{"same":true}}, +{"name":"date: a leap day in a century year divisible by 400","field":{"type":"date"},"value":"2000-02-29","expect":{"same":true}}, +{"name":"date: the first supported day","field":{"type":"date"},"value":"0001-01-01","expect":{"same":true}}, +{"name":"date: the last supported day","field":{"type":"date"},"value":"9999-12-31","expect":{"same":true}}, +{"name":"date: February 30","field":{"type":"date"},"value":"2023-02-30","expect":{"error":"invalid_date"}}, +{"name":"date: February 29 in a common year","field":{"type":"date"},"value":"2023-02-29","expect":{"error":"invalid_date"}}, +{"name":"date: February 29 in 1900","field":{"type":"date"},"value":"1900-02-29","expect":{"error":"invalid_date"}}, +{"name":"date: February 29 in 2100","field":{"type":"date"},"value":"2100-02-29","expect":{"error":"invalid_date"}}, +{"name":"date: April 31","field":{"type":"date"},"value":"2023-04-31","expect":{"error":"invalid_date"}}, +{"name":"date: month 13","field":{"type":"date"},"value":"2023-13-01","expect":{"error":"invalid_date"}}, +{"name":"date: month 00","field":{"type":"date"},"value":"2023-00-10","expect":{"error":"invalid_date"}}, +{"name":"date: day 00","field":{"type":"date"},"value":"2023-01-00","expect":{"error":"invalid_date"}}, +{"name":"date: day 32","field":{"type":"date"},"value":"2023-01-32","expect":{"error":"invalid_date"}}, +{"name":"date: year 0000","field":{"type":"date"},"value":"0000-01-01","expect":{"error":"invalid_date"}}, +{"name":"date: a one-digit month","field":{"type":"date"},"value":"2023-1-01","expect":{"error":"invalid_date"}}, +{"name":"date: a one-digit day","field":{"type":"date"},"value":"2023-01-1","expect":{"error":"invalid_date"}}, +{"name":"date: a two-digit year","field":{"type":"date"},"value":"23-01-01","expect":{"error":"invalid_date"}}, +{"name":"date: slashes","field":{"type":"date"},"value":"2023/01/01","expect":{"error":"invalid_date"}}, +{"name":"date: no separators","field":{"type":"date"},"value":"20230101","expect":{"error":"invalid_date"}}, +{"name":"date: a date and time","field":{"type":"date"},"value":"2023-01-01T00:00:00Z","expect":{"error":"invalid_date"}}, +{"name":"date: a leading space","field":{"type":"date"},"value":" 2023-01-01","expect":{"error":"invalid_date"}}, +{"name":"date: a trailing space","field":{"type":"date"},"value":"2023-01-01 ","expect":{"error":"invalid_date"}}, +{"name":"date: trailing text","field":{"type":"date"},"value":"2023-01-01x","expect":{"error":"invalid_date"}}, +{"name":"date: a plus sign","field":{"type":"date"},"value":"+2023-01-01","expect":{"error":"invalid_date"}}, +{"name":"date: a five-digit year","field":{"type":"date"},"value":"10000-01-01","expect":{"error":"invalid_date"}}, +{"name":"date: full-width digits","field":{"type":"date"},"value":"2023-01-01","expect":{"error":"invalid_date"}}, +{"name":"date: a word","field":{"type":"date"},"value":"today","expect":{"error":"invalid_date"}}, +{"name":"date: a week date","field":{"type":"date"},"value":"2023-W01-1","expect":{"error":"invalid_date"}}, +{"name":"date: a number is not a date","field":{"type":"date"},"value":20230101,"expect":{"error":"invalid_type"}}, +{"name":"date: a boolean is not a date","field":{"type":"date"},"value":true,"expect":{"error":"invalid_type"}}, +{"name":"date: an object is not a date","field":{"type":"date"},"value":{},"expect":{"error":"invalid_type"}}, +{"name":"date: an empty optional value is unanswered","field":{"type":"date"},"value":"","expect":{"unanswered":true}}, +{"name":"date: a whitespace-only optional value is unanswered","field":{"type":"date"},"value":" ","expect":{"unanswered":true}}, +{"name":"date: an absent optional value is unanswered","field":{"type":"date"},"expect":{"unanswered":true}}, +{"name":"date: a null optional value is unanswered","field":{"type":"date"},"value":null,"expect":{"unanswered":true}}, +{"name":"date: an absent required value","field":{"type":"date","required":true},"expect":{"error":"required"}}, +{"name":"date: an empty required value","field":{"type":"date","required":true},"value":"","expect":{"error":"required"}}, +{"name":"date min: the day before the minimum","field":{"type":"date","min":"2024-01-01"},"value":"2023-12-31","expect":{"error":"below_min"}}, +{"name":"date min: the minimum itself","field":{"type":"date","min":"2024-01-01"},"value":"2024-01-01","expect":{"same":true}}, +{"name":"date min: the day after the minimum","field":{"type":"date","min":"2024-01-01"},"value":"2024-01-02","expect":{"same":true}}, +{"name":"date max: the day after the maximum","field":{"type":"date","max":"2024-12-31"},"value":"2025-01-01","expect":{"error":"above_max"}}, +{"name":"date max: the maximum itself","field":{"type":"date","max":"2024-12-31"},"value":"2024-12-31","expect":{"same":true}}, +{"name":"date range: a single allowed day","field":{"type":"date","min":"2024-05-06","max":"2024-05-06"},"value":"2024-05-06","expect":{"same":true}}, +{"name":"date range: a calendar error is reported before the range","field":{"type":"date","min":"2024-01-01","max":"2024-12-31"},"value":"2024-02-30","expect":{"error":"invalid_date"}}, +{"name":"date range: years compare before months","field":{"type":"date","min":"2023-12-31"},"value":"2024-01-01","expect":{"same":true}}, +{"name":"date definition: the minimum and maximum are kept","field":{"type":"date","min":"2024-01-01","max":"2024-12-31"},"expect":{"normalized":{"min":"2024-01-01","max":"2024-12-31"}}}, +{"name":"date definition: min must be a string","field":{"type":"date","min":20240101},"expect":{"definition_error":"invalid_type","path":"fields[0].min"}}, +{"name":"date definition: max must be a string","field":{"type":"date","max":true},"expect":{"definition_error":"invalid_type","path":"fields[0].max"}}, +{"name":"date definition: min must be a date","field":{"type":"date","min":"tomorrow"},"expect":{"definition_error":"invalid_range","path":"fields[0].min"}}, +{"name":"date definition: min must be a real day","field":{"type":"date","min":"2024-02-30"},"expect":{"definition_error":"invalid_range","path":"fields[0].min"}}, +{"name":"date definition: max must be a date","field":{"type":"date","max":"2024-1-1"},"expect":{"definition_error":"invalid_range","path":"fields[0].max"}}, +{"name":"date definition: min cannot be year 0000","field":{"type":"date","min":"0000-01-01"},"expect":{"definition_error":"invalid_range","path":"fields[0].min"}}, +{"name":"date definition: min after max","field":{"type":"date","min":"2024-12-31","max":"2024-01-01"},"expect":{"definition_error":"invalid_range","path":"fields[0].min"}}, +{"name":"date definition: step is not a date key","field":{"type":"date","step":1},"expect":{"definition_error":"unknown_key","path":"fields[0].step"}}, +{"name":"date definition: integer is not a date key","field":{"type":"date","integer":true},"expect":{"definition_error":"unknown_key","path":"fields[0].integer"}}, +{"name":"date definition: placeholder is not a date key","field":{"type":"date","placeholder":"YYYY-MM-DD"},"expect":{"definition_error":"unknown_key","path":"fields[0].placeholder"}}, +{"name":"date default: a valid default","field":{"type":"date","default":"2024-05-06"},"expect":{"normalized":{"default":"2024-05-06"}}}, +{"name":"date default: not a real day","field":{"type":"date","default":"2024-02-30"},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"date default: before the minimum","field":{"type":"date","min":"2024-06-01","default":"2024-05-06"},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"date default: not a string","field":{"type":"date","default":20240506},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"checkbox: checked","field":{"type":"checkbox"},"value":true,"expect":{"ok":true}}, +{"name":"checkbox: an unchecked optional box is an answer","field":{"type":"checkbox"},"value":false,"expect":{"ok":false}}, +{"name":"checkbox: an absent optional box is unanswered","field":{"type":"checkbox"},"expect":{"unanswered":true}}, +{"name":"checkbox: a null optional box is unanswered","field":{"type":"checkbox"},"value":null,"expect":{"unanswered":true}}, +{"name":"checkbox: a required box that is checked","field":{"type":"checkbox","required":true},"value":true,"expect":{"ok":true}}, +{"name":"checkbox: a required box that is unchecked","field":{"type":"checkbox","required":true},"value":false,"expect":{"error":"required"}}, +{"name":"checkbox: a required box that is absent","field":{"type":"checkbox","required":true},"expect":{"error":"required"}}, +{"name":"checkbox: the text true is not a boolean","field":{"type":"checkbox"},"value":"true","expect":{"error":"invalid_type"}}, +{"name":"checkbox: the number 1 is not a boolean","field":{"type":"checkbox"},"value":1,"expect":{"error":"invalid_type"}}, +{"name":"checkbox: the number 0 is not a boolean","field":{"type":"checkbox"},"value":0,"expect":{"error":"invalid_type"}}, +{"name":"checkbox: the text on is not a boolean","field":{"type":"checkbox"},"value":"on","expect":{"error":"invalid_type"}}, +{"name":"checkbox: an object is not a boolean","field":{"type":"checkbox"},"value":{},"expect":{"error":"invalid_type"}}, +{"name":"checkbox: a list is not a boolean","field":{"type":"checkbox"},"value":[true],"expect":{"error":"invalid_type"}}, +{"name":"checkbox definition: a false default is kept","field":{"type":"checkbox","default":false},"expect":{"normalized":{"default":false}}}, +{"name":"checkbox definition: a true default is not allowed","field":{"type":"checkbox","default":true},"expect":{"definition_error":"default_not_allowed","path":"fields[0].default"}}, +{"name":"checkbox definition: a default of the wrong type","field":{"type":"checkbox","default":"no"},"expect":{"definition_error":"invalid_default","path":"fields[0].default"}}, +{"name":"checkbox definition: a required box may have a false default","field":{"type":"checkbox","required":true,"default":false},"expect":{"normalized":{"required":true,"default":false}}}, +{"name":"checkbox definition: min is not a checkbox key","field":{"type":"checkbox","min":0},"expect":{"definition_error":"unknown_key","path":"fields[0].min"}}, +{"name":"checkbox definition: pattern is not a checkbox key","field":{"type":"checkbox","pattern":"a"},"expect":{"definition_error":"unknown_key","path":"fields[0].pattern"}}, +{"name":"checkbox definition: options is not a checkbox key","field":{"type":"checkbox","options":["a"]},"expect":{"definition_error":"unknown_key","path":"fields[0].options"}}, +{"name":"checkbox definition: multiple is not a checkbox key","field":{"type":"checkbox","multiple":true},"expect":{"definition_error":"unknown_key","path":"fields[0].multiple"}}, +{"name":"field: an unknown type","field":{"type":"rating"},"expect":{"definition_error":"unsupported_type","path":"fields[0].type"}}, +{"name":"field: type names are case sensitive","field":{"type":"Text"},"expect":{"definition_error":"unsupported_type","path":"fields[0].type"}}, +{"name":"field: a select is not a type","field":{"type":"select"},"expect":{"definition_error":"unsupported_type","path":"fields[0].type"}}, +{"name":"field: a description is kept","field":{"type":"text","description":"Line one\nLine two"},"expect":{"normalized":{"description":"Line one\nLine two"}}}, +{"name":"field: required must be a boolean","field":{"type":"text","required":"yes"},"expect":{"definition_error":"invalid_type","path":"fields[0].required"}}, +{"name":"field: a description control character","field":{"type":"number","description":"a\u0001b"},"expect":{"definition_error":"control_characters","path":"fields[0].description"}}, +{"name":"form: a duplicate id across types","fields":[{"id":"a","type":"text"},{"id":"a","type":"checkbox"}],"expect":{"definition_error":"duplicate_id","path":"fields[1].id"}}, +{"name":"form: V2 types mixed with a choice","fields":[{"type":"text"},{"type":"choice","options":["a","b"]},{"type":"number"},{"type":"date"},{"type":"email"},{"type":"checkbox"},{"type":"textarea"}],"expect":{"definition_ok":true}}, +{"name":"form: a choice may not take a default","fields":[{"type":"choice","options":["a"],"default":"a"}],"expect":{"definition_error":"unknown_key","path":"fields[0].default"}}, +{"name":"form: a choice may not take min_length","fields":[{"type":"choice","options":["a"],"min_length":1}],"expect":{"definition_error":"unknown_key","path":"fields[0].min_length"}}, +{"name":"form budget: two textareas use the whole 16,000","fields":[{"type":"textarea"},{"type":"textarea"}],"expect":{"definition_ok":true}}, +{"name":"form budget: a text field on top of two textareas","fields":[{"type":"textarea"},{"type":"textarea"},{"type":"text"}],"expect":{"definition_error":"text_budget_exceeded","path":"fields"}}, +{"name":"form budget: three limits that sum to 16,000","fields":[{"type":"textarea","max_length":5333},{"type":"textarea","max_length":5333},{"type":"textarea","max_length":5334}],"expect":{"definition_ok":true}}, +{"name":"form budget: three limits that sum to 16,001","fields":[{"type":"textarea","max_length":5333},{"type":"textarea","max_length":5333},{"type":"textarea","max_length":5335}],"expect":{"definition_error":"text_budget_exceeded","path":"fields"}}, +{"name":"form budget: an email counts as 254","fields":[{"type":"textarea"},{"type":"text","max_length":2000},{"type":"text","max_length":2000},{"type":"text","max_length":2000},{"type":"text","max_length":1746},{"type":"email"}],"expect":{"definition_ok":true}}, +{"name":"form budget: an email counts as 254 and one more character is over","fields":[{"type":"textarea"},{"type":"text","max_length":2000},{"type":"text","max_length":2000},{"type":"text","max_length":2000},{"type":"text","max_length":1747},{"type":"email"}],"expect":{"definition_error":"text_budget_exceeded","path":"fields"}}, +{"name":"form budget: other types do not count","fields":[{"type":"textarea"},{"type":"textarea"},{"type":"number"},{"type":"date"},{"type":"checkbox"},{"type":"choice","options":["a","b"]}],"expect":{"definition_ok":true}}, +{"name":"form budget: even a one-character text field is over when two textareas use the whole budget","fields":[{"type":"textarea"},{"type":"textarea"},{"type":"text","max_length":1}],"expect":{"definition_error":"text_budget_exceeded","path":"fields"}} +] diff --git a/test/forms/state_service_test.lua b/test/forms/state_service_test.lua index 8ac5b10..1d6d2b6 100644 --- a/test/forms/state_service_test.lua +++ b/test/forms/state_service_test.lua @@ -666,6 +666,337 @@ local function define_tests() local retry_status = post(artifact_id, answer()) test.eq(retry_status, 200) end) + + -- Standard fields (V2): a form with any field that is not a choice is + -- version 2 end to end; a choice-only form stays version 1. + describe("with standard fields", function() + local V2_DEFINITION = { + title = "Delivery details", + fields = { + { id = "name", type = "text", label = "Your name", required = true, max_length = 40 }, + { id = "email", type = "email", label = "Email" }, + { id = "parcels", type = "number", label = "Parcels", integer = true, min = 1, max = 10, + required = true }, + { id = "ready", type = "date", label = "Ready on", min = "2026-01-01" }, + { id = "notes", type = "textarea", label = "Notes" }, + { id = "terms", type = "checkbox", label = "I agree", required = true }, + { id = "courier", type = "choice", label = "Courier", options = { "A", "B" } }, + }, + } + + local function new_v2_form(ctx, def: any?) + local normalized, errors = definition.normalize(def or V2_DEFINITION) + test.is_nil(errors, definition.format_errors(errors)) + local artifact_id = uuid.v7() + local _, err = control_handlers.control_forms(ctx, { + forms = { { artifact_id = artifact_id, definition = normalized } }, + turn_id = "turn-" .. uuid.v7(), call_id = "call-1", tool_message_id = "tool-row-1", + }) + test.is_nil(err) + return artifact_id + end + + local function v2_answers() + return { name = "Ann Lee", email = " ann@example.org ", parcels = 3.0, ready = "2026-05-06", + notes = "Line one\nLine two", terms = true, courier = "A" } + end + + local function v2_request(answers: any?, request_id: any?, version: any?) + return { form_version = 2, request_id = request_id or uuid.v7(), expected_content_version = version or 1, + answers = answers or v2_answers() } + end + + -- Replaces the stored content of a form, as an older or damaged + -- record would hold it. + local function rewrite_content(artifact_id: string, change: any) + local row: any = artifact_repo.get(artifact_id) + local content: any = json.decode(tostring(row.content)) + change(content) + local db, db_err = sql.get(consts.get_db_resource()) + test.is_nil(db_err) + local _, update_err = db:execute("UPDATE artifacts SET content = $1 WHERE artifact_id = $2", + { json.encode(content), artifact_id }) + db:release() + test.is_nil(update_err) + end + + it("stores a form with a standard field as version 2 and a choice-only form as version 1", function() + local ctx = new_session() + local v2_id = new_v2_form(ctx) + local v1_id = new_form(ctx) + local v2_row: any = artifact_repo.get(v2_id) + local v1_row: any = artifact_repo.get(v1_id) + test.eq(json.decode(tostring(v2_row.content)).props["form-version"], 2) + test.eq(json.decode(tostring(v1_row.content)).props["form-version"], 1) + + local _, v2_state = get(v2_id) + test.eq(v2_state.form_version, 2) + local _, v1_state = get(v1_id) + test.eq(v1_state.form_version, 1) + end) + + it("reads the definition with its defaults filled in", function() + local ctx = new_session() + local _, state = get(new_v2_form(ctx)) + local fields = state.definition.fields + test.eq(fields[1].type, "text") + test.eq(fields[1].min_length, 0) + test.eq(fields[1].max_length, 40) + test.eq(fields[2].type, "email") + test.eq(fields[3].integer, true) + test.eq(fields[3].min, 1) + test.eq(fields[5].max_length, 8000) + test.eq(fields[6].type, "checkbox") + test.eq(fields[7].options[1].value, "A") + end) + + it("answers 400 when the envelope names another form version, in both directions", function() + local ctx = new_session() + local v2_id = new_v2_form(ctx) + local v1_id = new_form(ctx) + local old_envelope = v2_request() + old_envelope.form_version = 1 + local status, body = post(v2_id, old_envelope) + test.eq(status, 400) + test.eq(body.code, "invalid_request") + local new_envelope = answer() + new_envelope.form_version = 2 + local v1_status, v1_body = post(v1_id, new_envelope) + test.eq(v1_status, 400) + test.eq(v1_body.code, "invalid_request") + test.eq(#user_rows(ctx.session_id), 0) + test.eq(post(v2_id, v2_request()), 200) + test.eq(post(v1_id, answer()), 200) + end) + + it("accepts standard answers, stores them canonically and reads them back", function() + local ctx = new_session() + local artifact_id = new_v2_form(ctx) + local status, body = post(artifact_id, v2_request()) + test.eq(status, 200) + test.eq(body.success, true) + + local submission = artifact_repo.get(artifact_id).meta.session_form.submission + test.eq(submission.answers.email, "ann@example.org", "an address is stored trimmed") + test.eq(submission.answers.parcels, 3) + test.eq(math.type(submission.answers.parcels), "integer", "a whole number is stored as an integer") + test.eq(submission.answers.terms, true) + test.eq(submission.answers.notes, "Line one\nLine two") + test.eq(submission.answers.courier, "A") + + local status_after, state, raw = get(artifact_id) + test.eq(status_after, 200) + test.eq(state.data.name, "Ann Lee") + test.eq(state.data.parcels, 3) + test.contains(raw, '"parcels":3,') + test.contains(raw, '"terms":true,') + test.contains(raw, '"email":"ann@example.org",') + end) + + it("keeps an optional checkbox that was left unchecked as an answer", function() + local ctx = new_session() + local def = { title = "Consent", fields = { + { id = "news", type = "checkbox", label = "Send me news" }, + { id = "terms", type = "checkbox", label = "Terms", required = true }, + } } + local artifact_id = new_v2_form(ctx, def) + local status = post(artifact_id, v2_request({ news = false, terms = true })) + test.eq(status, 200) + local _, state, raw = get(artifact_id) + test.eq(state.data.news, false) + test.contains(raw, '"news":false') + end) + + it("returns a stable code for each field that is not valid", function() + local ctx = new_session() + local artifact_id = new_v2_form(ctx) + local status, body = post(artifact_id, v2_request({ + name = "", email = "nope", parcels = 11, ready = "2025-12-31", terms = false, notes = 5, + courier = "Z", ghost = 1, + })) + test.eq(status, 422) + test.eq(body.code, "validation_failed") + test.eq(body.errors.name[1].code, "required") + test.eq(body.errors.email[1].code, "invalid_email") + test.eq(body.errors.parcels[1].code, "above_max") + test.eq(body.errors.ready[1].code, "below_min") + test.eq(body.errors.terms[1].code, "required") + test.eq(body.errors.notes[1].code, "invalid_type") + test.eq(body.errors.courier[1].code, "invalid_option") + test.eq(body.form_errors[1].code, "unknown_field") + test.is_nil(string.find(tostring(body.errors.email[1].message), "nope", 1, true), "no value is echoed") + test.eq(artifact_repo.get(artifact_id).content_version, 1) + test.eq(#user_rows(ctx.session_id), 0) + + local string_status, as_string = post(artifact_id, v2_request({ name = "A", parcels = "3", terms = true })) + test.eq(string_status, 422) + test.eq(as_string.errors.parcels[1].code, "invalid_type", "a numeric string is not a number") + end) + + it("reads a null answer as an answer not sent", function() + local ctx = new_session() + local artifact_id = new_v2_form(ctx) + local body = '{"form_version":2,"request_id":"r-null","expected_content_version":1,"answers":' + .. '{"name":"Ann","parcels":2,"terms":true,"email":null,"notes":null}}' + local status = post(artifact_id, body) + test.eq(status, 200) + local answers = artifact_repo.get(artifact_id).meta.session_form.submission.answers + test.is_nil(answers.email) + test.is_nil(answers.notes) + test.eq(answers.name, "Ann") + end) + + it("answers 400 for a number the runtime cannot read", function() + local ctx = new_session() + local artifact_id = new_v2_form(ctx) + local body = '{"form_version":2,"request_id":"r-big","answers":{"name":"Ann","parcels":1e400,"terms":true}}' + local status, response = post(artifact_id, body) + test.eq(status, 400) + test.eq(response.code, "invalid_request") + test.eq(#user_rows(ctx.session_id), 0) + end) + + it("replays a retry whose keys, spacing and number spellings differ", function() + local ctx = new_session() + local artifact_id = new_v2_form(ctx) + local request_id = uuid.v7() + local first_body = '{"form_version":2,"request_id":' .. (json.encode(request_id)) + .. ',"expected_content_version":1,"answers":{"name":"Ann Lee","email":"ann@example.org",' + .. '"parcels":3,"ready":"2026-05-06","terms":true,"courier":"A"}}' + local retry_body = '{"answers":{"courier":"A","terms":true,"parcels":3.0,"ready":"2026-05-06",' + .. '"email":" ann@example.org ","name":"Ann Lee"},"expected_content_version":1,' + .. '"request_id":' .. (json.encode(request_id)) .. ',"form_version":2}' + local status, first = post(artifact_id, first_body) + test.eq(status, 200) + test.eq(first.replayed, false) + local retry_status, retry = post(artifact_id, retry_body) + test.eq(retry_status, 200, "a retry, not a request_conflict") + test.eq(retry.replayed, true) + test.eq(retry.answer_message_id, first.answer_message_id) + test.eq(#user_rows(ctx.session_id), 1) + test.eq(#wakes, 1) + end) + + it("rejects the same request id with a different standard answer", function() + local ctx = new_session() + local artifact_id = new_v2_form(ctx) + local request = v2_request() + test.eq(post(artifact_id, request), 200) + request.answers.parcels = 4 + local status, body = post(artifact_id, request) + test.eq(status, 409) + test.eq(body.code, "request_conflict") + request.answers.parcels = 3 + request.answers.terms = true + request.answers.notes = "Line one\nLine two " + local note_status, note_body = post(artifact_id, request) + test.eq(note_status, 409, "text is stored as typed, so trailing spaces are a different answer") + test.eq(note_body.code, "request_conflict") + end) + + it("writes the answer message with the version and the answers as JSON literals", function() + local ctx = new_session() + local artifact_id = new_v2_form(ctx) + local status, body = post(artifact_id, v2_request({ name = "Ann", parcels = 2.0, terms = true })) + test.eq(status, 200) + local row: any = message_repo.get(body.answer_message_id) + test.contains(row.data, '"form_version":2,') + test.contains(row.data, '"name":{"question":"Your name","answer":"Ann"}') + test.contains(row.data, '"parcels":{"question":"Parcels","answer":2}') + test.contains(row.data, '"terms":{"question":"I agree","answer":true}') + test.contains(row.data, '"email":{"question":"Email","answer":null}') + test.contains(row.data, '"courier":{"question":"Courier","answer":null}') + end) + + it("refuses a stored version lower than the definition needs and any unknown version", function() + local ctx = new_session() + local artifact_id = new_v2_form(ctx) + rewrite_content(artifact_id, function(content) content.props["form-version"] = 1 end) + local status, body = get(artifact_id) + test.eq(status, 400) + test.eq(body.code, "not_a_form") + local post_status, post_body = post(artifact_id, v2_request()) + test.eq(post_status, 400) + test.eq(post_body.code, "not_a_form") + + rewrite_content(artifact_id, function(content) content.props["form-version"] = 3 end) + test.eq(get(artifact_id), 400) + + rewrite_content(artifact_id, function(content) content.props["form-version"] = 2 end) + test.eq(get(artifact_id), 200) + end) + + it("answers 500, not 422, when a stored pattern no longer compiles, and still reads the form", function() + local ctx = new_session() + local def = { title = "Code", fields = { { id = "code", type = "text", label = "Code", pattern = "[A-Z]+" } } } + local artifact_id = new_v2_form(ctx, def) + rewrite_content(artifact_id, function(content) + content.props.definition.fields[1].pattern = "(" + end) + test.eq(get(artifact_id), 200) + local status, body = post(artifact_id, v2_request({ code = "ABC" })) + test.eq(status, 500) + test.eq(body.code, "internal") + test.eq(#user_rows(ctx.session_id), 0) + end) + + -- P6: the largest answers a valid form can take, with the + -- characters the record escapes six-fold, through acceptance and + -- the answer message. + it("accepts the largest text a form can hold, and keeps it exact in the record", function() + local ctx = new_session() + local def = { title = "Letters", fields = { + { id = "first", type = "textarea", label = "First" }, + { id = "second", type = "textarea", label = "Second" }, + } } + local artifact_id = new_v2_form(ctx, def) + local text = string.rep("'<>&", 2000) + test.eq(#text, 8000) + -- Written by hand: a browser's JSON.stringify leaves ', < > and & + -- as they are, where json.encode would write < > & as six-byte + -- escapes and push the body over the cap. + local request = '{"form_version":2,"request_id":"r-largest","expected_content_version":1,' + .. '"answers":{"first":"' .. text .. '","second":"' .. text .. '"}}' + test.lt(#request, state_service.MAX_BODY_BYTES) + local status, body = post(artifact_id, request) + test.eq(status, 200) + + local row: any = message_repo.get(body.answer_message_id) + test.gt(#row.data, 90000, "each of the 16,000 characters is written as a six-byte escape") + local record = string.match(tostring(row.data), "record='([^']*)'") + test.not_nil(record) + local decoded: any = json.decode(record) + test.eq(decoded.answers.first.answer, text) + test.eq(decoded.answers.second.answer, text) + + local _, state = get(artifact_id) + test.eq(state.data.first, text) + local stored = artifact_repo.get(artifact_id).meta.session_form.submission.answers + test.eq(stored.second, text) + end) + + it("accepts the largest request a valid form can send under the 64 KiB body cap", function() + local ctx = new_session() + local fields = {} + local answers = {} + for index = 1, 20 do + local id = string.format("f%037d_x", index) + test.eq(#id, 40) + table.insert(fields, { id = id, type = index == 20 and "textarea" or "text", label = "Q" .. index, + max_length = 800 }) + answers[id] = string.rep("😀", 800) + end + local artifact_id = new_v2_form(ctx, { title = "Everything", fields = fields }) + local request_id = string.rep("r", 128) + local raw = json.encode(v2_request(answers, request_id)) + test.gt(#raw, 64000) + test.lt(#raw, state_service.MAX_BODY_BYTES, "a valid form never needs more than the cap") + local status, body = post(artifact_id, raw) + test.eq(status, 200, tostring(body and body.message)) + local row: any = message_repo.get(body.answer_message_id) + test.gt(#row.data, 64000) + end) + end) end) end diff --git a/test/forms/v2_conformance_test.lua b/test/forms/v2_conformance_test.lua new file mode 100644 index 0000000..75c552e --- /dev/null +++ b/test/forms/v2_conformance_test.lua @@ -0,0 +1,164 @@ +local test = require("test") +local json = require("json") +local fs = require("fs") +local definition = require("definition") + +-- Runs test/forms/fixtures/v2_conformance.json through the definition +-- normalizer and the answer validator. The same file is the contract the +-- form component's client-side checks are tested against, so a rule changes +-- in one place and both sides follow. +-- +-- A case is an object: +-- name unique title of the case +-- field one field as an agent would write it (id "f" and label +-- "Question" are added when missing), or +-- fields several fields (ids f0, f1, ... and the label are added when +-- missing) +-- value the answer for the first field; absent or null = no answer +-- value_concat instead of value: a list of strings and { repeat, times } +-- objects joined into one string (long values) +-- expect one of +-- { ok = v } the answer is accepted and stored as v +-- { same = true } accepted and stored exactly as sent +-- { unanswered = true } accepted, nothing stored for the field +-- { error = code } the answer is refused with this code +-- { definition_ok = true } the definition is accepted +-- { definition_error = code, path = "fields[0].key" } +-- the definition is refused (path optional) +-- { normalized = { ... } } the definition is accepted and the first +-- field, normalized, has these values (the +-- client receives definitions in this form, +-- so its answer checks start from them) + +local FIXTURE = "/v2_conformance.json" + +local function load_cases(): any + local volume, volume_err = fs.get("app.forms:fixtures") + if not volume then error("fixture volume: " .. tostring(volume_err)) end + local raw, read_err = volume:readfile(FIXTURE) + if not raw then error("fixture file: " .. tostring(read_err)) end + local cases, decode_err = json.decode(raw) + if type(cases) ~= "table" then error("fixture JSON: " .. tostring(decode_err)) end + return cases +end + +local function expand(case: any): any + if case.value_concat == nil then return case.value end + local parts = {} + for _, part in ipairs(case.value_concat) do + if type(part) == "string" then + table.insert(parts, part) + else + table.insert(parts, string.rep(part["repeat"], part.times)) + end + end + return table.concat(parts) +end + +local function build_fields(case: any): {any} + local source = case.fields or { case.field } + local fields = {} + for index, raw in ipairs(source) do + local field = {} + for key, value in pairs(raw) do field[key] = value end + field.id = field.id or (case.fields and ("f" .. (index - 1)) or "f") + field.label = field.label or "Question" + table.insert(fields, field) + end + return fields +end + +local function normalize(case: any): (any, any) + return definition.normalize({ title = "Title", fields = build_fields(case) }) +end + +local function has_error(errors: any, code: string, path: string?): boolean + for _, item in ipairs(errors or {}) do + if item.code == code and (path == nil or item.path == path) then return true end + end + return false +end + +-- A whole number is stored as an integer, whatever spelling it arrived in. +local function check_number_type(result: any) + if type(result) == "number" and result == math.floor(result) then + test.eq(math.type(result), "integer", "a whole number is stored as an integer") + end +end + +local function check_definition(case: any) + local expect = case.expect + local def, errors = normalize(case) + if expect.definition_ok then + test.is_nil(errors, definition.format_errors(errors)) + test.not_nil(def) + return + end + if expect.definition_error then + test.is_nil(def, "the definition must be refused") + test.is_true(has_error(errors, expect.definition_error, expect.path), + "expected " .. expect.definition_error .. " at " .. tostring(expect.path) + .. ", got:\n" .. definition.format_errors(errors)) + return + end + test.is_nil(errors, definition.format_errors(errors)) + local field = def.fields[1] + for key, expected in pairs(expect.normalized) do + test.eq(field[key], expected, "normalized " .. key) + check_number_type(field[key]) + end +end + +local function check_answer(case: any) + local expect = case.expect + local def, errors = normalize(case) + test.is_nil(errors, definition.format_errors(errors)) + local value = expand(case) + local answers = {} + if value ~= nil then answers.f = value end + local normalized, field_errors, form_errors = definition.validate_answers(def, answers) + test.is_nil(form_errors) + if expect.error then + test.is_nil(normalized, "the answer must be refused") + test.not_nil(field_errors, "expected " .. expect.error) + test.eq(field_errors.f[1].code, expect.error) + return + end + test.is_nil(field_errors, "unexpected " .. tostring(field_errors and field_errors.f and field_errors.f[1].code)) + test.not_nil(normalized) + if expect.unanswered then + test.is_nil(normalized.f, "nothing is stored for an unanswered field") + return + end + local expected = expect.same and value or expect.ok + test.eq(normalized.f, expected) + check_number_type(normalized.f) +end + +local function define_tests() + describe("V2 conformance fixture", function() + local cases = load_cases() + + it("holds a case for every rule, each with a unique name", function() + test.gt(#cases, 400) + local seen = {} + for _, case in ipairs(cases) do + test.is_nil(seen[case.name], "duplicate case name: " .. case.name) + seen[case.name] = true + end + end) + + for _, case in ipairs(cases) do + it(case.name, function() + local expect = case.expect + if expect.definition_ok or expect.definition_error or expect.normalized then + check_definition(case) + else + check_answer(case) + end + end) + end + end) +end + +return test.run_cases(define_tests) diff --git a/test/process/forms_test.lua b/test/process/forms_test.lua index fa848df..02576b3 100644 --- a/test/process/forms_test.lua +++ b/test/process/forms_test.lua @@ -40,6 +40,16 @@ local DEFINITION = { }, } +-- A form with standard fields (version 2). +local DETAILS_DEFINITION = { + title = "Delivery details", + fields = { + { id = "name", type = "text", label = "Your name", required = true, max_length = 40 }, + { id = "parcels", type = "number", label = "Parcels", integer = true, min = 1 }, + { id = "terms", type = "checkbox", label = "I agree", required = true }, + }, +} + local function define_tests() describe("session forms in the session process", function() local user_id = "" @@ -236,9 +246,9 @@ local function define_tests() return artifacts[1].artifact_id end - local function submit(artifact_id, answers) + local function submit(artifact_id, answers, form_version) local status, body = state_service.submit(user_id, artifact_id, json.encode({ - form_version = 1, request_id = uuid.v7(), expected_content_version = 1, + form_version = form_version or 1, request_id = uuid.v7(), expected_content_version = 1, answers = answers or { delivery = "Courier" }, })) test.eq(status, 200, body) @@ -478,6 +488,39 @@ local function define_tests() test.eq(#rows_of(ctx, consts.MSG_TYPE.ARTIFACT), 0) end) + it("stores a form with standard fields as version 2 and ends the turn", function() + local ctx = new_ctx() + local result, err = ask(ctx, DETAILS_DEFINITION) + test.is_nil(err) + test.is_false(has_continue(result), "a created form ends the turn") + local artifact: any = artifact_repo.get(form_id_of(ctx)) + local content = json.decode(tostring(artifact.content)) + test.eq(content.props["form-version"], 2) + test.eq(content.props.definition.fields[1].type, "text") + test.eq(content.props.definition.fields[1].max_length, 40) + test.eq(content.props.definition.fields[1].min_length, 0) + test.eq(content.props.definition.fields[2].integer, true) + test.eq(artifact.meta.session_form.version, 1, "the record layout is a separate number from the form version") + end) + + it("returns a tool error naming the path and code of each standard field that is not valid", function() + local ctx = new_ctx() + local result, err, call_id, ids = ask(ctx, { title = "Broken", fields = { + { id = "n", type = "number", label = "N", min = 5, max = 1 }, + { id = "c", type = "text", label = "C", pattern = "(?=a)a" }, + { id = "d", type = "date", label = "D", default = "2023-02-30" }, + } }) + test.is_nil(err) + test.is_true(has_continue(result), "the agent gets a step to correct the definition") + local call_row = message_repo.get((ids :: any)[call_id]) + test.eq(call_row.metadata.status, consts.FUNC_STATUS.ERROR) + local text = tostring(call_row.metadata.result) + test.contains(text, "fields[0].min: invalid_range") + test.contains(text, "fields[1].pattern: unsupported_pattern") + test.contains(text, "fields[2].default: invalid_default") + test.eq(#artifact_repo.list_by_session(ctx.session_id), 0) + end) + it("allows one form per turn and marks the refused call as an error", function() local ctx = new_ctx() local _, first_err = ask(ctx, DEFINITION) @@ -862,6 +905,33 @@ local function define_tests() test.contains(tostring(foreign_err), "No form") end) + it("reads a form with standard fields, then its answer record with the values as typed", function() + local ctx = new_ctx() + ask(ctx, DETAILS_DEFINITION) + local form_id = form_id_of(ctx) + local caller = funcs.new():with_context({ agent_run = { host = { session_id = ctx.session_id } } }) + local open = caller:call("wippy.session.forms:read_form_tool", { form_id = form_id }) + test.contains(open.message, '"status":"unanswered"') + test.contains(open.message, + '"name":{"question":"Your name","type":"text","required":true,"min_length":0,"max_length":40}') + test.contains(open.message, + '"parcels":{"question":"Parcels","type":"number","required":false,"integer":true,"min":1}') + test.contains(open.message, '"terms":{"question":"I agree","type":"checkbox","required":true}') + + submit(form_id, { name = "Ann Lee", parcels = 3, terms = true }, 2) + local hidden = caller:call("wippy.session.forms:read_form_tool", { form_id = form_id }) + test.contains(hidden.message, '"status":"answer_pending"') + test.is_false(string.find(hidden.message, "Ann Lee", 1, true) ~= nil) + + message_handlers.admit_in_order(ctx, nil) + local answered = caller:call("wippy.session.forms:read_form_tool", { form_id = form_id }) + test.contains(answered.message, '"status":"answered"') + test.contains(answered.message, '"form_version":2') + test.contains(answered.message, '"name":{"question":"Your name","answer":"Ann Lee"}') + test.contains(answered.message, '"parcels":{"question":"Parcels","answer":3}') + test.contains(answered.message, '"terms":{"question":"I agree","answer":true}') + end) + it("gives read_form in a running turn no values of an answer the turn may not see yet", function() local ctx = new_ctx() ask(ctx, DEFINITION) From 60ba9567473e92654aabc9ee76d89c85f404a6da Mon Sep 17 00:00:00 2001 From: Andrei Kirkouski Date: Thu, 1 Oct 2026 11:05:57 +0200 Subject: [PATCH 04/63] feat(forms-ui): render the V2 standard fields in the fields form Accept form versions 1 and 2 (a higher version, or a field type the component does not know, keeps the "needs a newer version" notice). Extract the field shell (label, required marker, description, control slot, error list linked by aria-describedby, read-only value) from the choice field and build the text, textarea, email, number, date and checkbox controls on it. Number is a text input with a strict parser, not InputNumber. Date is a PrimeVue DatePicker in string mode that never drops typed text it cannot parse. Add the per-type client rules and serialization (lib/answers.ts, scalar-answers.ts, decimal.ts, text.ts), defaults that prefill, the Yes/No/Skipped read-only display, and disabled controls for V2 fields in both notices. The fields form is novalidate and gains parts, w-form__ classes and two tokens. A conformance test runs test/forms/fixtures/v2_conformance.json through the client checker. --- .../wippy-form/src/app/active-form-v2.test.ts | 280 +++++++++++++++ .../wippy-form/src/app/active-form.test.ts | 4 +- frontend/wippy-form/src/app/active-form.vue | 100 ++++-- .../src/app/fields/checkbox-field.vue | 67 ++++ .../src/app/fields/choice-field.test.ts | 6 +- .../src/app/fields/choice-field.vue | 201 +++++------ .../wippy-form/src/app/fields/date-field.vue | 138 ++++++++ .../src/app/fields/field-shell.test.ts | 163 +++++++++ .../wippy-form/src/app/fields/field-shell.vue | 227 ++++++++++++ .../src/app/fields/form-field.test.ts | 285 +++++++++++++++ .../wippy-form/src/app/fields/form-field.vue | 95 +++++ .../wippy-form/src/app/fields/input-field.vue | 74 ++++ .../src/app/fields/textarea-field.vue | 60 ++++ .../wippy-form/src/app/idle-form-v2.test.ts | 107 ++++++ frontend/wippy-form/src/app/idle-form.vue | 37 +- .../src/app/needs-newer-host-notice.vue | 41 ++- .../wippy-form/src/app/no-artifact-notice.vue | 33 +- frontend/wippy-form/src/app/notices.test.ts | 66 ++++ .../question-card-active.test.ts | 14 +- .../question-card/question-card-active.vue | 6 +- .../question-card-answer-items.vue | 2 +- .../question-card/question-card-question.vue | 7 +- .../question-card/question-card-review.vue | 4 +- .../question-card-summary.test.ts | 8 +- .../question-card/question-card-summary.vue | 4 +- .../wippy-form/src/app/wippy-form.test.ts | 123 ++++++- frontend/wippy-form/src/app/wippy-form.vue | 52 +-- .../src/composables/use-question-card.test.ts | 10 +- .../src/composables/use-question-card.ts | 64 ++-- frontend/wippy-form/src/lib/answers.test.ts | 177 +++++++++- frontend/wippy-form/src/lib/answers.ts | 262 +++++++++----- .../wippy-form/src/lib/conformance.test.ts | 217 ++++++++++++ frontend/wippy-form/src/lib/decimal.test.ts | 92 +++++ frontend/wippy-form/src/lib/decimal.ts | 97 ++++++ frontend/wippy-form/src/lib/definition.ts | 146 +++++++- .../wippy-form/src/lib/scalar-answers.test.ts | 328 ++++++++++++++++++ frontend/wippy-form/src/lib/scalar-answers.ts | 249 +++++++++++++ frontend/wippy-form/src/lib/text.test.ts | 71 ++++ frontend/wippy-form/src/lib/text.ts | 75 ++++ frontend/wippy-form/src/styles.css | 107 ++++++ frontend/wippy-form/src/styles.test.ts | 31 ++ frontend/wippy-form/vitest.setup.ts | 16 + 42 files changed, 3785 insertions(+), 361 deletions(-) create mode 100644 frontend/wippy-form/src/app/active-form-v2.test.ts create mode 100644 frontend/wippy-form/src/app/fields/checkbox-field.vue create mode 100644 frontend/wippy-form/src/app/fields/date-field.vue create mode 100644 frontend/wippy-form/src/app/fields/field-shell.test.ts create mode 100644 frontend/wippy-form/src/app/fields/field-shell.vue create mode 100644 frontend/wippy-form/src/app/fields/form-field.test.ts create mode 100644 frontend/wippy-form/src/app/fields/form-field.vue create mode 100644 frontend/wippy-form/src/app/fields/input-field.vue create mode 100644 frontend/wippy-form/src/app/fields/textarea-field.vue create mode 100644 frontend/wippy-form/src/app/idle-form-v2.test.ts create mode 100644 frontend/wippy-form/src/app/notices.test.ts create mode 100644 frontend/wippy-form/src/lib/conformance.test.ts create mode 100644 frontend/wippy-form/src/lib/decimal.test.ts create mode 100644 frontend/wippy-form/src/lib/decimal.ts create mode 100644 frontend/wippy-form/src/lib/scalar-answers.test.ts create mode 100644 frontend/wippy-form/src/lib/scalar-answers.ts create mode 100644 frontend/wippy-form/src/lib/text.test.ts create mode 100644 frontend/wippy-form/src/lib/text.ts diff --git a/frontend/wippy-form/src/app/active-form-v2.test.ts b/frontend/wippy-form/src/app/active-form-v2.test.ts new file mode 100644 index 0000000..89ba286 --- /dev/null +++ b/frontend/wippy-form/src/app/active-form-v2.test.ts @@ -0,0 +1,280 @@ +import type { FormState } from '../lib/definition' +import { PrimeVuePlugin } from '@wippy-fe/theme/primevue-plugin' +import { mount } from '@vue/test-utils' +import { describe, expect, it } from 'vitest' +import ActiveForm from './active-form.vue' + +const v2State: FormState = { + form_version: 2, + artifact_id: 'artifact-1', + session_id: 'session-1', + status: 'active', + stale: false, + late: false, + content_version: 1, + definition: { + title: 'Delivery details', + description: 'Where should it go?', + fields: [ + { id: 'name', type: 'text', label: 'Full name', required: true }, + { id: 'mail', type: 'email', label: 'Email', required: true }, + { id: 'qty', type: 'number', label: 'Parcels', required: false, integer: true, min: 1, max: 9 }, + { id: 'day', type: 'date', label: 'Delivery date', required: false }, + { id: 'notes', type: 'textarea', label: 'Notes', required: false }, + { id: 'consent', type: 'checkbox', label: 'I agree', required: true }, + ], + }, + data: null, + submitted_at: null, + answer_message_id: null, + delivery: null, +} + +function mountForm(state: FormState = v2State) { + return mount(ActiveForm, { + props: { state, submitting: false, banner: null }, + global: { plugins: [PrimeVuePlugin] }, + attachTo: document.body, + }) +} + +function inputOf(wrapper: ReturnType, label: string) { + const labelEl = wrapper.findAll('label').find(l => l.text().startsWith(label)) + if (!labelEl) + throw new Error(`no label starting with "${label}"`) + return wrapper.find(`[id="${labelEl.attributes('for')}"]`) +} + +async function fillValid(wrapper: ReturnType) { + await inputOf(wrapper, 'Full name').setValue(' Ada Lovelace ') + await inputOf(wrapper, 'Email').setValue(' ada@example.com ') + await inputOf(wrapper, 'I agree').setValue(true) +} + +describe('ActiveForm — V2 standard fields', () => { + it('renders a labelled control for every field type and a form the browser may not validate natively', () => { + const wrapper = mountForm() + expect(wrapper.find('form').attributes('novalidate')).toBeDefined() + expect(wrapper.findAll('input[type="text"]')).toHaveLength(3) + expect(wrapper.find('input[type="email"]').exists()).toBe(true) + expect(wrapper.find('textarea').exists()).toBe(true) + expect(wrapper.find('input[type="checkbox"]').exists()).toBe(true) + expect(wrapper.text()).toContain('Delivery details') + expect(wrapper.text()).toContain('Where should it go?') + }) + + it('does not submit an empty required form: names each missing field and moves focus to the first', async () => { + const wrapper = mountForm() + await wrapper.find('form').trigger('submit') + await wrapper.vm.$nextTick() + await wrapper.vm.$nextTick() + + expect(wrapper.emitted('submit')).toBeUndefined() + expect(wrapper.findAll('ul').map(ul => ul.text())).toEqual([ + 'This question is required.', + 'This question is required.', + 'This question is required.', + ]) + expect(document.activeElement).toBe(inputOf(wrapper, 'Full name').element) + expect(wrapper.find('[role="status"]').text()).toBe('3 questions need attention.') + }) + + it('reports an invalid email on its own field, linked to the real input, and does not echo the value', async () => { + const wrapper = mountForm() + await fillValid(wrapper) + await inputOf(wrapper, 'Email').setValue('not-an-email') + await wrapper.find('form').trigger('submit') + await wrapper.vm.$nextTick() + await wrapper.vm.$nextTick() + + expect(wrapper.emitted('submit')).toBeUndefined() + const mail = inputOf(wrapper, 'Email') + const error = wrapper.find('ul') + expect(error.text()).toBe('Enter a valid email address.') + expect(mail.attributes('aria-describedby')).toBe(error.attributes('id')) + expect(mail.attributes('aria-invalid')).toBe('true') + expect(document.activeElement).toBe(mail.element) + expect(wrapper.find('[role="status"]').text()).toBe('1 question needs attention.') + expect(wrapper.text()).not.toContain('not-an-email') + }) + + it('names an unparseable typed date instead of dropping it', async () => { + const wrapper = mountForm() + await fillValid(wrapper) + await inputOf(wrapper, 'Delivery date').setValue('next tuesday') + await wrapper.find('form').trigger('submit') + await wrapper.vm.$nextTick() + + expect(wrapper.emitted('submit')).toBeUndefined() + expect(wrapper.find('ul').text()).toBe('Enter a valid date as YYYY-MM-DD.') + }) + + it('names a typed date outside the allowed range', async () => { + const state: FormState = { ...v2State, definition: { title: 'T', fields: [{ id: 'day', type: 'date', label: 'Delivery date', required: false, min: '2026-10-01', max: '2026-12-31' }] } } + const wrapper = mountForm(state) + await inputOf(wrapper, 'Delivery date').setValue('2026-09-30') + await wrapper.find('form').trigger('submit') + await wrapper.vm.$nextTick() + expect(wrapper.emitted('submit')).toBeUndefined() + expect(wrapper.find('ul').text()).toBe('Choose 2026-10-01 or later.') + + await inputOf(wrapper, 'Delivery date').setValue('2027-01-01') + await wrapper.find('form').trigger('submit') + await wrapper.vm.$nextTick() + expect(wrapper.find('ul').text()).toBe('Choose 2026-12-31 or earlier.') + + await inputOf(wrapper, 'Delivery date').setValue('2026-10-01') + await wrapper.find('form').trigger('submit') + expect(wrapper.emitted('submit')?.[0]).toEqual([{ day: '2026-10-01' }]) + }) + + it('names a number that is not strictly a number', async () => { + const wrapper = mountForm() + await fillValid(wrapper) + await inputOf(wrapper, 'Parcels').setValue('1,5') + await wrapper.find('form').trigger('submit') + await wrapper.vm.$nextTick() + + expect(wrapper.emitted('submit')).toBeUndefined() + expect(wrapper.find('ul').text()).toContain('Enter a number') + }) + + it('submits the exact wire values: text as typed, number as a number, trimmed email, explicit booleans', async () => { + const wrapper = mountForm() + await fillValid(wrapper) + await inputOf(wrapper, 'Parcels').setValue('3') + await inputOf(wrapper, 'Delivery date').setValue('2026-10-15') + await inputOf(wrapper, 'Notes').setValue('line one\nline two') + await wrapper.find('form').trigger('submit') + + expect(wrapper.emitted('submit')?.[0]).toEqual([{ + name: ' Ada Lovelace ', + mail: 'ada@example.com', + qty: 3, + day: '2026-10-15', + notes: 'line one\nline two', + consent: true, + }]) + }) + + it('omits blank optional fields', async () => { + const wrapper = mountForm() + await fillValid(wrapper) + await wrapper.find('form').trigger('submit') + expect(wrapper.emitted('submit')?.[0]).toEqual([{ name: ' Ada Lovelace ', mail: 'ada@example.com', consent: true }]) + }) + + it('sends an untouched optional checkbox as false', async () => { + const state: FormState = { ...v2State, definition: { title: 'T', fields: [{ id: 'news', type: 'checkbox', label: 'Newsletter', required: false }] } } + const wrapper = mountForm(state) + await wrapper.find('form').trigger('submit') + expect(wrapper.emitted('submit')?.[0]).toEqual([{ news: false }]) + }) + + it('will not submit a required checkbox left unchecked', async () => { + const wrapper = mountForm() + await inputOf(wrapper, 'Full name').setValue('Ada') + await inputOf(wrapper, 'Email').setValue('ada@example.com') + await wrapper.find('form').trigger('submit') + await wrapper.vm.$nextTick() + + expect(wrapper.emitted('submit')).toBeUndefined() + const consent = inputOf(wrapper, 'I agree') + expect(consent.attributes('aria-invalid')).toBe('true') + }) + + it('clears a field\'s error once the user changes that field', async () => { + const wrapper = mountForm() + await wrapper.find('form').trigger('submit') + expect(wrapper.findAll('ul')).toHaveLength(3) + await inputOf(wrapper, 'Full name').setValue('Ada') + expect(wrapper.findAll('ul')).toHaveLength(2) + expect(inputOf(wrapper, 'Full name').attributes('aria-describedby')).toBeUndefined() + }) + + it('shows a server field error on the right field, linked and focused', async () => { + const wrapper = mountForm() + await fillValid(wrapper) + const vm = wrapper.vm as unknown as { setServerErrors: (e?: Record, f?: { code: string, message: string }[]) => void } + vm.setServerErrors({ mail: [{ code: 'invalid_email', message: 'That address is not accepted.' }] }, []) + await wrapper.vm.$nextTick() + await wrapper.vm.$nextTick() + + const mail = inputOf(wrapper, 'Email') + expect(wrapper.find('ul').text()).toBe('That address is not accepted.') + expect(mail.attributes('aria-describedby')).toBe(wrapper.find('ul').attributes('id')) + expect(document.activeElement).toBe(mail.element) + }) +}) + +describe('ActiveForm — defaults', () => { + const withDefaults: FormState = { + ...v2State, + definition: { + title: 'Defaults', + fields: [ + { id: 'city', type: 'text', label: 'City', required: false, default: 'Berlin' }, + { id: 'qty', type: 'number', label: 'Quantity', required: false, default: 3 }, + { id: 'agree', type: 'checkbox', label: 'Agree', required: false, default: false }, + ], + }, + } + + it('prefills the controls from the definition', () => { + const wrapper = mountForm(withDefaults) + expect((inputOf(wrapper, 'City').element as HTMLInputElement).value).toBe('Berlin') + expect((inputOf(wrapper, 'Quantity').element as HTMLInputElement).value).toBe('3') + expect((inputOf(wrapper, 'Agree').element as HTMLInputElement).checked).toBe(false) + }) + + it('submits a prefilled default as it stands', async () => { + const wrapper = mountForm(withDefaults) + await wrapper.find('form').trigger('submit') + expect(wrapper.emitted('submit')?.[0]).toEqual([{ city: 'Berlin', qty: 3, agree: false }]) + }) + + it('submits nothing for an optional prefilled value the user cleared', async () => { + const wrapper = mountForm(withDefaults) + await inputOf(wrapper, 'City').setValue('') + await inputOf(wrapper, 'Quantity').setValue('') + await wrapper.find('form').trigger('submit') + expect(wrapper.emitted('submit')?.[0]).toEqual([{ agree: false }]) + }) +}) + +describe('ActiveForm — hasUnsentInput', () => { + function unsent(wrapper: ReturnType) { + return (wrapper.vm as unknown as { hasUnsentInput: () => boolean }).hasUnsentInput() + } + + it('is false for an untouched form, even one whose controls start with a value', () => { + expect(unsent(mountForm())).toBe(false) + }) + + it('is true once the user typed something, even something invalid', async () => { + const wrapper = mountForm() + await inputOf(wrapper, 'Email').setValue('half-typed') + expect(unsent(wrapper)).toBe(true) + }) + + it('is true once a checkbox was ticked, and false again when it is put back', async () => { + const wrapper = mountForm() + await inputOf(wrapper, 'I agree').setValue(true) + expect(unsent(wrapper)).toBe(true) + await inputOf(wrapper, 'I agree').setValue(false) + expect(unsent(wrapper)).toBe(false) + }) +}) + +describe('ActiveForm — styling API', () => { + it('names the form, its title, description, fields and submit button as parts and w-form__ classes', () => { + const wrapper = mountForm() + const root = wrapper.find('form') + expect(root.attributes('part')?.split(' ')).toContain('fields') + expect(root.classes()).toEqual(expect.arrayContaining(['w-form', 'w-form__fields'])) + expect(wrapper.find('[part~="fields-title"]').text()).toBe('Delivery details') + expect(wrapper.find('[part~="form-description"]').text()).toBe('Where should it go?') + expect(wrapper.findAll('[part~="field"]')).toHaveLength(6) + expect(wrapper.find('button[type="submit"]').attributes('part')).toContain('send') + }) +}) diff --git a/frontend/wippy-form/src/app/active-form.test.ts b/frontend/wippy-form/src/app/active-form.test.ts index 364b66d..38066fa 100644 --- a/frontend/wippy-form/src/app/active-form.test.ts +++ b/frontend/wippy-form/src/app/active-form.test.ts @@ -94,7 +94,7 @@ describe('ActiveForm — server-side validation errors', () => { const status = wrapper.find('[role="status"]') expect(status.exists()).toBe(true) - expect(status.text()).toBe('1 question needs an answer.') + expect(status.text()).toBe('1 question needs attention.') }) it('mirrors a submit-failure banner into the polite status region', async () => { @@ -150,7 +150,7 @@ describe('ActiveForm — blank Other answer', () => { expect(wrapper.emitted('submit')).toBeUndefined() expect(wrapper.text()).toContain('Enter the other answer.') - expect(wrapper.find('[role="status"]').text()).toContain('1 question needs an answer.') + expect(wrapper.find('[role="status"]').text()).toContain('1 question needs attention.') wrapper.unmount() }) }) diff --git a/frontend/wippy-form/src/app/active-form.vue b/frontend/wippy-form/src/app/active-form.vue index 382c197..1d1fa9e 100644 --- a/frontend/wippy-form/src/app/active-form.vue +++ b/frontend/wippy-form/src/app/active-form.vue @@ -1,11 +1,23 @@ diff --git a/frontend/wippy-form/src/app/fields/date-field.vue b/frontend/wippy-form/src/app/fields/date-field.vue new file mode 100644 index 0000000..5943733 --- /dev/null +++ b/frontend/wippy-form/src/app/fields/date-field.vue @@ -0,0 +1,138 @@ + + + diff --git a/frontend/wippy-form/src/app/fields/field-shell.test.ts b/frontend/wippy-form/src/app/fields/field-shell.test.ts new file mode 100644 index 0000000..c284c93 --- /dev/null +++ b/frontend/wippy-form/src/app/fields/field-shell.test.ts @@ -0,0 +1,163 @@ +import type { FieldAnswer, FieldError, FormField } from '../../lib/definition' +import { PrimeVuePlugin } from '@wippy-fe/theme/primevue-plugin' +import { mount } from '@vue/test-utils' +import { describe, expect, it } from 'vitest' +import FieldShell from './field-shell.vue' + +const textField: FormField = { id: 'name', type: 'text', label: 'Full name', required: true, description: 'As on the parcel' } + +const CONTROL_SLOT = `` + +function mountShell(props: Partial<{ field: FormField, modelValue: FieldAnswer | undefined, errors: FieldError[], readonly: boolean, disabled: boolean, layout: 'group' | 'stacked' | 'inline' }> = {}) { + return mount(FieldShell, { + props: { field: textField, modelValue: undefined, errors: [], readonly: false, disabled: false, layout: 'stacked', ...props }, + slots: { default: CONTROL_SLOT }, + global: { plugins: [PrimeVuePlugin] }, + }) +} + +describe('FieldShell — stacked layout (one labelled control)', () => { + it('labels the real control: the label points at the id the slot receives', () => { + const wrapper = mountShell() + const input = wrapper.find('input') + const label = wrapper.find('label') + expect(label.attributes('for')).toBe(input.attributes('id')) + expect(label.text()).toContain('Full name') + }) + + it('shows the required marker as text on an editable required field', () => { + expect(mountShell().find('label').text()).toContain('(required)') + expect(mountShell({ field: { ...textField, required: false } }).find('label').text()).not.toContain('(required)') + }) + + it('describes the control with the description and, once there are errors, the error list', () => { + const wrapper = mountShell({ errors: [{ code: 'required', message: 'This question is required.' }] }) + const describedBy = wrapper.find('input').attributes('aria-describedby') ?? '' + const description = wrapper.find('p') + const errorList = wrapper.find('ul') + expect(description.text()).toBe('As on the parcel') + expect(describedBy.split(' ')).toEqual([description.attributes('id'), errorList.attributes('id')]) + expect(errorList.text()).toContain('This question is required.') + expect(wrapper.find('input').attributes('aria-invalid')).toBe('true') + }) + + it('does not point aria-describedby at an error list that is not rendered', () => { + const wrapper = mountShell({ field: { ...textField, description: undefined } }) + expect(wrapper.find('input').attributes('aria-describedby')).toBeUndefined() + expect(wrapper.find('ul').exists()).toBe(false) + expect(wrapper.find('input').attributes('aria-invalid')).toBeUndefined() + }) + + it('derives the control, description and error ids from one base, all distinct', () => { + const wrapper = mountShell({ errors: [{ code: 'required', message: 'This question is required.' }] }) + const ids = [wrapper.find('input').attributes('id'), wrapper.find('p').attributes('id'), wrapper.find('ul').attributes('id')] + expect(ids.every(id => !!id)).toBe(true) + expect(new Set(ids).size).toBe(3) + }) + + it('never uses role="alert" (one polite live region announces the summary instead)', () => { + const wrapper = mountShell({ errors: [{ code: 'required', message: 'This question is required.' }] }) + expect(wrapper.find('[role="alert"]').exists()).toBe(false) + }) + + it('exposes focusFirstControl, which focuses the slotted control', async () => { + const wrapper = mount(FieldShell, { + props: { field: textField, modelValue: undefined, errors: [], readonly: false, disabled: false, layout: 'stacked' }, + slots: { default: CONTROL_SLOT }, + global: { plugins: [PrimeVuePlugin] }, + attachTo: document.body, + }) + ;(wrapper.vm as unknown as { focusFirstControl: () => void }).focusFirstControl() + expect(document.activeElement).toBe(wrapper.find('input').element) + }) +}) + +describe('FieldShell — inline layout (checkbox)', () => { + it('puts the control first and a clickable label that points at it', () => { + const wrapper = mountShell({ layout: 'inline', field: { id: 'ok', type: 'checkbox', label: 'I agree', required: true } }) + const input = wrapper.find('input') + const label = wrapper.find('label') + expect(label.attributes('for')).toBe(input.attributes('id')) + expect(label.text()).toContain('I agree') + const html = wrapper.html() + expect(html.indexOf(' { + it('is a fieldset with a legend and describes the fieldset itself', () => { + const wrapper = mountShell({ layout: 'group', errors: [{ code: 'required', message: 'This question is required.' }] }) + const fieldset = wrapper.find('fieldset') + expect(fieldset.exists()).toBe(true) + expect(fieldset.find('legend').text()).toContain('Full name') + expect(fieldset.attributes('aria-describedby')).toContain(wrapper.find('ul').attributes('id') as string) + expect(wrapper.find('label').exists()).toBe(false) + }) +}) + +describe('FieldShell — read-only', () => { + it('shows the value instead of the control, with no required marker and no errors', () => { + const wrapper = mountShell({ readonly: true, modelValue: 'Ada', errors: [{ code: 'required', message: 'x' }] }) + expect(wrapper.find('input').exists()).toBe(false) + expect(wrapper.text()).toContain('Ada') + expect(wrapper.text()).not.toContain('(required)') + expect(wrapper.find('ul').exists()).toBe(false) + expect(wrapper.find('label').exists()).toBe(false) + }) + + it('keeps a text value\'s line breaks as real line breaks', () => { + const wrapper = mountShell({ readonly: true, field: { id: 'n', type: 'textarea', label: 'Notes', required: false }, modelValue: 'one\ntwo' }) + expect(wrapper.find('[part~="field-value"]').html()).toContain('one
two') + }) + + it('words an unanswered field "Skipped"', () => { + const wrapper = mountShell({ readonly: true, modelValue: undefined }) + expect(wrapper.find('[part~="field-value"]').text()).toBe('Skipped') + expect(wrapper.find('[part~="field-value-empty"]').exists()).toBe(true) + }) + + it('shows a checkbox as Yes or No and a number as stored', () => { + const yes = mountShell({ readonly: true, field: { id: 'c', type: 'checkbox', label: 'C', required: false }, modelValue: true }) + expect(yes.find('[part~="field-value"]').text()).toBe('Yes') + const no = mountShell({ readonly: true, field: { id: 'c', type: 'checkbox', label: 'C', required: false }, modelValue: false }) + expect(no.find('[part~="field-value"]').text()).toBe('No') + const num = mountShell({ readonly: true, field: { id: 'n', type: 'number', label: 'N', required: false }, modelValue: 0.5 }) + expect(num.find('[part~="field-value"]').text()).toBe('0.5') + }) + + it('shows a choice answer with its note and an Other entry', () => { + const choice: FormField = { id: 'p', type: 'choice', label: 'Speed', required: false, options: [{ value: 'a', label: 'Alpha' }], multiple: true, allow_other: true } + const wrapper = mountShell({ readonly: true, layout: 'group', field: choice, modelValue: [{ value: 'a', note: 'because' }, { other: 'mine' }] }) + const text = wrapper.find('[part~="field-value"]').text() + expect(text).toContain('Alpha') + expect(text).toContain('because') + expect(text).toContain('Other: mine') + }) +}) + +describe('FieldShell — styling API', () => { + it('names the field and its states as parts and w-form__ classes', () => { + const wrapper = mountShell({ errors: [{ code: 'required', message: 'x' }], disabled: true }) + const root = wrapper.element as HTMLElement + expect(root.getAttribute('part')?.split(' ')).toEqual(expect.arrayContaining(['field', 'field-required', 'field-invalid', 'field-disabled'])) + expect(root.className).toContain('w-form__field') + expect(root.className).toContain('w-form__field--required') + expect(root.className).toContain('w-form__field--invalid') + for (const part of ['field-label', 'field-marker', 'field-description', 'field-control', 'field-error']) + expect(wrapper.find(`[part~="${part}"]`).exists(), part).toBe(true) + expect(wrapper.find('[part~="field-label"]').classes()).toContain('w-form__field-label') + }) + + it('marks a read-only field with the readonly state part', () => { + const root = mountShell({ readonly: true }).element as HTMLElement + expect(root.getAttribute('part')?.split(' ')).toContain('field-readonly') + expect(root.className).toContain('w-form__field--readonly') + }) + + it('marks the layout as a state so styling can target it', () => { + expect((mountShell({ layout: 'group' }).element as HTMLElement).getAttribute('part')?.split(' ')).toContain('field-group') + expect((mountShell({ layout: 'inline' }).element as HTMLElement).getAttribute('part')?.split(' ')).toContain('field-inline') + }) +}) diff --git a/frontend/wippy-form/src/app/fields/field-shell.vue b/frontend/wippy-form/src/app/fields/field-shell.vue new file mode 100644 index 0000000..6f86844 --- /dev/null +++ b/frontend/wippy-form/src/app/fields/field-shell.vue @@ -0,0 +1,227 @@ + + + diff --git a/frontend/wippy-form/src/app/fields/form-field.test.ts b/frontend/wippy-form/src/app/fields/form-field.test.ts new file mode 100644 index 0000000..a463758 --- /dev/null +++ b/frontend/wippy-form/src/app/fields/form-field.test.ts @@ -0,0 +1,285 @@ +import type { FieldAnswer, FieldError, FormField } from '../../lib/definition' +import { PrimeVuePlugin } from '@wippy-fe/theme/primevue-plugin' +import { mount } from '@vue/test-utils' +import { describe, expect, it } from 'vitest' +import FormFieldControl from './form-field.vue' + +function mountField(field: FormField, props: Partial<{ modelValue: FieldAnswer | undefined, errors: FieldError[], disabled: boolean, readonly: boolean }> = {}) { + return mount(FormFieldControl, { + props: { field, modelValue: undefined, errors: [], disabled: false, readonly: false, ...props }, + global: { plugins: [PrimeVuePlugin] }, + attachTo: document.body, + }) +} + +const REQUIRED_ERROR: FieldError[] = [{ code: 'required', message: 'This question is required.' }] + +/** The last value a control reported through v-model. */ +function lastValue(wrapper: ReturnType): unknown { + const events = wrapper.emitted('update:modelValue') ?? [] + return events[events.length - 1]?.[0] +} + +describe('text field', () => { + const field: FormField = { id: 'name', type: 'text', label: 'Full name', required: true, description: 'As on the parcel', placeholder: 'Ada Lovelace' } + + it('is an InputText labelled by its own label element', () => { + const wrapper = mountField(field) + const input = wrapper.find('input') + expect(input.attributes('type')).toBe('text') + expect(input.attributes('placeholder')).toBe('Ada Lovelace') + expect(wrapper.find('label').attributes('for')).toBe(input.attributes('id')) + }) + + it('links the error and the description to the real input, not a wrapper', () => { + const wrapper = mountField(field, { errors: REQUIRED_ERROR }) + const input = wrapper.find('input') + const describedBy = (input.attributes('aria-describedby') ?? '').split(' ') + expect(describedBy).toContain(wrapper.find('p').attributes('id')) + expect(describedBy).toContain(wrapper.find('ul').attributes('id')) + expect(input.attributes('aria-invalid')).toBe('true') + expect(input.attributes('aria-required')).toBe('true') + }) + + it('reports what is typed through v-model, untrimmed', async () => { + const wrapper = mountField(field) + await wrapper.find('input').setValue(' Ada ') + expect(lastValue(wrapper)).toBe(' Ada ') + }) + + it('shows the draft it is given', () => { + expect((mountField(field, { modelValue: 'Berlin' }).find('input').element as HTMLInputElement).value).toBe('Berlin') + }) + + it('is disabled while the form is submitting', () => { + expect(mountField(field, { disabled: true }).find('input').attributes('disabled')).toBeDefined() + }) + + it('focuses its input on focusFirstControl', () => { + const wrapper = mountField(field) + ;(wrapper.vm as unknown as { focusFirstControl: () => void }).focusFirstControl() + expect(document.activeElement).toBe(wrapper.find('input').element) + }) +}) + +describe('textarea field', () => { + const field: FormField = { id: 'notes', type: 'textarea', label: 'Notes', required: false } + + it('is a textarea whose newlines are kept', async () => { + const wrapper = mountField(field) + const textarea = wrapper.find('textarea') + expect(textarea.exists()).toBe(true) + await textarea.setValue('one\ntwo') + expect(lastValue(wrapper)).toBe('one\ntwo') + }) + + it('links its error to the textarea itself', () => { + const wrapper = mountField(field, { errors: [{ code: 'too_long', message: 'Use at most 8000 characters.' }] }) + expect(wrapper.find('textarea').attributes('aria-describedby')).toBe(wrapper.find('ul').attributes('id')) + expect(wrapper.find('textarea').attributes('aria-invalid')).toBe('true') + }) + + it('does not mark an optional field as required', () => { + expect(mountField(field).find('textarea').attributes('aria-required')).toBeUndefined() + }) +}) + +describe('email field', () => { + const field: FormField = { id: 'mail', type: 'email', label: 'Email', required: true } + + it('is an InputText of type email with the email keyboard hint and autofill token', () => { + const input = mountField(field).find('input') + expect(input.attributes('type')).toBe('email') + expect(input.attributes('inputmode')).toBe('email') + expect(input.attributes('autocomplete')).toBe('email') + }) + + it('links its error to the input', () => { + const wrapper = mountField(field, { errors: [{ code: 'invalid_email', message: 'Enter a valid email address.' }] }) + expect(wrapper.find('input').attributes('aria-describedby')).toBe(wrapper.find('ul').attributes('id')) + }) +}) + +describe('number field', () => { + const field: FormField = { id: 'qty', type: 'number', label: 'Parcels', required: false, integer: true } + + it('is a text input with the decimal keyboard hint, not a native number input or a spinner', () => { + const input = mountField(field).find('input') + expect(input.attributes('type')).toBe('text') + expect(input.attributes('inputmode')).toBe('decimal') + expect(mountField(field).find('[role="spinbutton"]').exists()).toBe(false) + }) + + it('reports exactly the text typed, so the strict parser sees it (no rounding, no clamping, no group stripping)', async () => { + const wrapper = mountField(field) + await wrapper.find('input').setValue('1,5') + expect(lastValue(wrapper)).toBe('1,5') + await wrapper.find('input').setValue('12345678.123456789') + expect(lastValue(wrapper)).toBe('12345678.123456789') + }) + + it('links its error to the input', () => { + const wrapper = mountField(field, { errors: [{ code: 'invalid_number', message: 'Enter a number.' }] }) + expect(wrapper.find('input').attributes('aria-describedby')).toBe(wrapper.find('ul').attributes('id')) + }) +}) + +describe('checkbox field', () => { + const field: FormField = { id: 'ok', type: 'checkbox', label: 'I agree to the terms', required: true, description: 'Required to continue' } + + it('is a real checkbox with a clickable label, control first', () => { + const wrapper = mountField(field) + const input = wrapper.find('input[type="checkbox"]') + expect(input.exists()).toBe(true) + expect(wrapper.find('label').attributes('for')).toBe(input.attributes('id')) + }) + + it('reports an explicit boolean when the box or its label is clicked', async () => { + const wrapper = mountField(field) + await wrapper.find('input[type="checkbox"]').setValue(true) + expect(lastValue(wrapper)).toBe(true) + await wrapper.find('input[type="checkbox"]').setValue(false) + expect(lastValue(wrapper)).toBe(false) + await wrapper.find('label').trigger('click') + expect(lastValue(wrapper)).toBe(true) + }) + + it('links description and error to the checkbox input itself, not to the wrapper div', () => { + const wrapper = mountField(field, { errors: REQUIRED_ERROR }) + const input = wrapper.find('input[type="checkbox"]') + const describedBy = (input.attributes('aria-describedby') ?? '').split(' ') + expect(describedBy).toContain(wrapper.find('p').attributes('id')) + expect(describedBy).toContain(wrapper.find('ul').attributes('id')) + expect(input.attributes('aria-invalid')).toBe('true') + expect(input.attributes('aria-required')).toBe('true') + // The PrimeVue root div must not carry a stray aria-describedby. + expect(wrapper.find('.p-checkbox').attributes('aria-describedby')).toBeUndefined() + }) + + it('shows a checked draft and can be toggled with the keyboard', async () => { + const wrapper = mountField(field, { modelValue: true }) + expect((wrapper.find('input[type="checkbox"]').element as HTMLInputElement).checked).toBe(true) + }) + + it('focuses the checkbox on focusFirstControl', () => { + const wrapper = mountField(field) + ;(wrapper.vm as unknown as { focusFirstControl: () => void }).focusFirstControl() + expect(document.activeElement).toBe(wrapper.find('input[type="checkbox"]').element) + }) +}) + +describe('date field', () => { + const field: FormField = { id: 'day', type: 'date', label: 'Delivery date', required: true, min: '2026-10-01', max: '2026-12-31' } + + it('is a date picker input labelled by the field label', () => { + const wrapper = mountField(field) + const input = wrapper.find('input') + expect(input.exists()).toBe(true) + expect(wrapper.find('label').attributes('for')).toBe(input.attributes('id')) + expect(input.attributes('placeholder')).toBe('YYYY-MM-DD') + }) + + it('links the error and description to the real input, not the picker wrapper', () => { + const wrapper = mountField({ ...field, description: 'Pick a weekday' }, { errors: [{ code: 'invalid_date', message: 'Enter a valid date as YYYY-MM-DD.' }] }) + const input = wrapper.find('input') + const describedBy = (input.attributes('aria-describedby') ?? '').split(' ') + expect(describedBy).toContain(wrapper.find('p').attributes('id')) + expect(describedBy).toContain(wrapper.find('ul').attributes('id')) + expect(input.attributes('aria-invalid')).toBe('true') + expect(input.attributes('aria-required')).toBe('true') + }) + + it('reports a typed date in the wire format', async () => { + const wrapper = mountField(field) + await wrapper.find('input').setValue('2026-10-15') + expect(lastValue(wrapper)).toBe('2026-10-15') + }) + + it('reports typed text that is not a date instead of dropping it', async () => { + const wrapper = mountField(field) + await wrapper.find('input').setValue('next tuesday') + expect(lastValue(wrapper)).toBe('next tuesday') + }) + + it('reports a typed date outside min and max as typed, for the client check to name', async () => { + const wrapper = mountField(field) + await wrapper.find('input').setValue('2026-09-30') + expect(lastValue(wrapper)).toBe('2026-09-30') + }) + + it('reports an emptied field as empty text', async () => { + const wrapper = mountField(field, { modelValue: '2026-10-15' }) + await wrapper.find('input').setValue('') + expect(lastValue(wrapper)).toBe('') + }) + + it('shows a stored date', () => { + const wrapper = mountField(field, { modelValue: '2026-10-15' }) + expect((wrapper.find('input').element as HTMLInputElement).value).toBe('2026-10-15') + }) + + it('keeps the typed text in the box after leaving it, so the user can correct it', async () => { + const wrapper = mountField(field) + const input = wrapper.find('input') + await input.setValue('next tuesday') + await input.trigger('blur') + await wrapper.vm.$nextTick() + expect((input.element as HTMLInputElement).value).toBe('next tuesday') + }) + + it('opens no calendar on focus: it opens from its button, so keyboard users can just type', async () => { + const wrapper = mountField(field) + await wrapper.find('input').trigger('focus') + expect(document.querySelector('.p-datepicker-panel')).toBeNull() + expect(wrapper.find('button[aria-haspopup="dialog"]').exists()).toBe(true) + }) + + it('opens the calendar from the keyboard with ArrowDown and closes it with Escape', async () => { + const wrapper = mountField(field) + const input = wrapper.find('input') + await input.trigger('keydown', { code: 'ArrowDown' }) + await wrapper.vm.$nextTick() + expect(input.attributes('aria-expanded')).toBe('true') + await input.trigger('keydown', { code: 'Escape' }) + await wrapper.vm.$nextTick() + expect(input.attributes('aria-expanded')).toBe('false') + }) + + it('opens the calendar from its button', async () => { + const wrapper = mountField(field) + await wrapper.find('button[aria-haspopup="dialog"]').trigger('click') + await wrapper.vm.$nextTick() + expect(wrapper.find('input').attributes('aria-expanded')).toBe('true') + }) +}) + +describe('choice field through the dispatcher', () => { + it('still renders the V1 radio group', () => { + const wrapper = mountField({ id: 'c', type: 'choice', label: 'Speed', required: false, options: [{ value: 'a', label: 'A' }, { value: 'b', label: 'B' }], multiple: false, allow_other: false }) + expect(wrapper.findAll('input[type="radio"]')).toHaveLength(2) + }) + + it('forwards focusOtherText to the choice field', () => { + const wrapper = mountField({ id: 'c', type: 'choice', label: 'Speed', required: false, options: [{ value: 'a', label: 'A' }], multiple: false, allow_other: true }, { modelValue: { other: '' } }) + ;(wrapper.vm as unknown as { focusOtherText: () => void }).focusOtherText() + expect(document.activeElement).toBe(wrapper.find('input[type="text"]').element) + }) +}) + +describe('read-only through the dispatcher', () => { + it('renders no control for any type and shows the stored value', () => { + const cases: Array<[FormField, FieldAnswer | undefined, string]> = [ + [{ id: 'a', type: 'text', label: 'A', required: true }, 'Ada', 'Ada'], + [{ id: 'b', type: 'email', label: 'B', required: false }, 'a@b.co', 'a@b.co'], + [{ id: 'c', type: 'number', label: 'C', required: false }, 3, '3'], + [{ id: 'd', type: 'date', label: 'D', required: false }, '2026-10-01', '2026-10-01'], + [{ id: 'e', type: 'checkbox', label: 'E', required: false }, true, 'Yes'], + [{ id: 'f', type: 'textarea', label: 'F', required: false }, 'x', 'x'], + ] + for (const [field, value, shown] of cases) { + const wrapper = mountField(field, { readonly: true, disabled: true, modelValue: value }) + expect(wrapper.find('input, textarea').exists(), field.type).toBe(false) + expect(wrapper.find('[part~="field-value"]').text(), field.type).toBe(shown) + } + }) +}) diff --git a/frontend/wippy-form/src/app/fields/form-field.vue b/frontend/wippy-form/src/app/fields/form-field.vue new file mode 100644 index 0000000..5665623 --- /dev/null +++ b/frontend/wippy-form/src/app/fields/form-field.vue @@ -0,0 +1,95 @@ + + + diff --git a/frontend/wippy-form/src/app/fields/input-field.vue b/frontend/wippy-form/src/app/fields/input-field.vue new file mode 100644 index 0000000..c2b13cd --- /dev/null +++ b/frontend/wippy-form/src/app/fields/input-field.vue @@ -0,0 +1,74 @@ + + + diff --git a/frontend/wippy-form/src/app/fields/textarea-field.vue b/frontend/wippy-form/src/app/fields/textarea-field.vue new file mode 100644 index 0000000..9cd766d --- /dev/null +++ b/frontend/wippy-form/src/app/fields/textarea-field.vue @@ -0,0 +1,60 @@ + + +