Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
agent / agent-validate-2Built by daveRuns on Liberasruns as agent-validate-3

Tool input validation, with patterns

validate(schema, input), both JSON text, before a tool runs (ADR 0062). Not a new version of agent-validate, which promises to ignore pattern: both stay, this one reads pattern through agent-regex (ADR 0067). Errors, in this order: "schema is not JSON"; the schema's shape; "input is not JSON". A schema is true, false or an object; the keywords read are type, const, enum, required, properties, additionalProperties, items, minItems, maxItems, uniqueItems, minLength, maxLength, pattern, minimum, maximum, exclusiveMinimum, exclusiveMaximum, allOf, anyOf, oneOf and not; every other keyword (format, $ref, …) is ignored. Shape: a subschema that is neither a boolean nor an object is the error "schema {pointer}: not a schema" (pointer: its JSON Pointer, "" at the root); at each schema object the keywords present are checked in ascending byte order of their names, the first malformed one giving "schema {pointer}: {keyword} is malformed": type a string or a non-empty array of strings among array, boolean, integer, null, number, object, string; enum a non-empty array; required an array of strings; properties an object whose values are schemas; additionalProperties, items and not schemas; allOf, anyOf and oneOf non-empty arrays of schemas; minItems, maxItems, minLength and maxLength numbers that are non-negative integers (a float without a fraction counts); minimum, maximum, exclusiveMinimum and exclusiveMaximum numbers; uniqueItems a boolean; const anything. pattern a string that agent-regex accepts (its matches(pattern, "") is not an error). After a schema's own keywords its subschemas are checked, depth first, in the same byte order of keyword names (additionalProperties, allOf, anyOf, items, not, oneOf, properties), the arrays by index and properties by key byte order. Validation walks the input once and collects every problem {path, message}, path the value's JSON Pointer ("" at the root; ~ and / in a token become ~0 and ~1): true accepts; false is "nothing is allowed here"; type: "expected {the types, joined by " or ", in the schema's order}, got {actual}" with actual one of null, boolean, number, string, array, object, where integer accepts a number without a fractional part; const: "expected the constant {the constant as compact JSON, object keys in byte order}" unless equal; enum: "not one of the allowed values" unless equal to one; equality is as JSON values with numbers by value (1 equals 1.0). Then by the input's type. An object: "missing required property {name}" for each required name absent, in required order; then each property in key byte order: one named in properties is validated by that schema at path/name, otherwise additionalProperties false gives "unexpected property {name}" at the object's path, an additionalProperties schema validates it at path/name, and nothing else happens. An array: "at least {n} items", "at most {n} items", "items must be unique" (equality as above), then items validates each element at path/index. A string: "at least {n} characters", "at most {n} characters", counting Unicode scalar values. Then "must match the pattern {pattern}" (the pattern as given) when agent-regex's matches(pattern, the string) is false; pattern on a value of any other type does nothing. A number: "must be >= {n}", "must be > {n}", "must be <= {n}", "must be < {n}" for minimum, exclusiveMinimum, maximum, exclusiveMaximum in that order, compared as IEEE doubles. Then, for any type: allOf appends each subschema's problems in order; anyOf gives "matches none of the alternatives" when no subschema accepts; oneOf gives "matches {k} alternatives, not exactly one" unless exactly one accepts; not gives "must not match the excluded schema" when its schema accepts. A subschema under anyOf, oneOf or not reports nothing of its own. A number in a message is written without a fraction when it has none, else as the shortest decimal that reads back.

Depends onagent-regex

Interface (WIT)25 lines›
agent-validate-2.wit
package root:component;

world root {
  import agent-regex: liberas:agent/regex@0.1.0;
  import liberas:agent/types@0.1.0;

  export liberas:agent/validate@0.1.0;
}
package liberas:agent@0.1.0 {
  interface regex {
    matches: func(pattern: string, input: string) -> result<bool, string>;
  }
  interface types {
    record problem {
      path: string,
      message: string,
    }
  }
  interface validate {
    use types.{problem};

    validate: func(schema: string, input: string) -> result<list<problem>, string>;
  }
}
Test vectors49›
agent-validate-2.cases.json
[
  {
    "args": {
      "input": "{\"path\":\"a\"}",
      "schema": "{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\"}},\"required\":[\"path\"],\"additionalProperties\":false}"
    }
  },
  {
    "args": {
      "input": "{\"path\":1}",
      "schema": "{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\"}},\"required\":[\"path\"],\"additionalProperties\":false}"
    }
  },
  {
    "args": {
      "input": "{\"paths\":\"a\",\"x\":null}",
      "schema": "{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\"}},\"required\":[\"path\"],\"additionalProperties\":false}"
    }
  },
  {
    "args": {
      "input": "\"not an object\"",
      "schema": "{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\"}},\"required\":[\"path\"],\"additionalProperties\":false}"
    }
  },
  {
    "args": {
      "input": "{",
      "schema": "{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\"}},\"required\":[\"path\"],\"additionalProperties\":false}"
    }
  },
  {
    "args": {
      "input": "{}",
      "schema": "{"
    }
  },
  {
    "args": {
      "input": "[1,2]",
      "schema": "true"
    }
  },
  {
    "args": {
      "input": "null",
      "schema": "false"
    }
  },
  {
    "args": {
      "input": "null",
      "schema": "{\"type\":[\"string\",\"null\"]}"
    }
  },
  {
    "args": {
      "input": "1.5",
      "schema": "{\"type\":[\"string\",\"null\"]}"
    }
  },
  {
    "args": {
      "input": "2.0",
      "schema": "{\"type\":\"integer\"}"
    }
  },
  {
    "args": {
      "input": "2.5",
      "schema": "{\"type\":\"integer\"}"
    }
  },
  {
    "args": {
      "input": "{\"a\":\"x\",\"b\":[1.0,2]}",
      "schema": "{\"const\":{\"b\":[1,2.0],\"a\":\"x\"}}"
    }
  },
  {
    "args": {
      "input": "{\"a\":\"x\",\"b\":[1,3]}",
      "schema": "{\"const\":{\"b\":[1,2.0],\"a\":\"x\"}}"
    }
  },
  {
    "args": {
      "input": "[3]",
      "schema": "{\"enum\":[1,\"two\",null,[3]]}"
    }
  },
  {
    "args": {
      "input": "3",
      "schema": "{\"enum\":[1,\"two\",null,[3]]}"
    }
  },
  {
    "args": {
      "input": "{\"zz\":1,\"a\":1,\"b\":-1,\"c\":true}",
      "schema": "{\"type\":\"object\",\"required\":[\"b\",\"a\"],\"properties\":{\"a\":{\"type\":\"string\"},\"b\":{\"type\":\"number\",\"minimum\":0}},\"additionalProperties\":{\"type\":\"boolean\"}}"
    }
  },
  {
    "args": {
      "input": "[1,1.0,11,2]",
      "schema": "{\"type\":\"array\",\"minItems\":2,\"maxItems\":3,\"uniqueItems\":true,\"items\":{\"type\":\"integer\",\"maximum\":10}}"
    }
  },
  {
    "args": {
      "input": "[5]",
      "schema": "{\"type\":\"array\",\"minItems\":2,\"items\":{\"type\":\"string\"}}"
    }
  },
  {
    "args": {
      "input": "\"한글\"",
      "schema": "{\"type\":\"string\",\"minLength\":2,\"maxLength\":3}"
    }
  },
  {
    "args": {
      "input": "\"a\"",
      "schema": "{\"type\":\"string\",\"minLength\":2,\"maxLength\":3}"
    }
  },
  {
    "args": {
      "input": "\"abcd\"",
      "schema": "{\"type\":\"string\",\"minLength\":2,\"maxLength\":3}"
    }
  },
  {
    "args": {
      "input": "1",
      "schema": "{\"minimum\":1,\"exclusiveMinimum\":1,\"maximum\":1,\"exclusiveMaximum\":1}"
    }
  },
  {
    "args": {
      "input": "0.25",
      "schema": "{\"minimum\":0.5,\"maximum\":1e21}"
    }
  },
  {
    "args": {
      "input": "3",
      "schema": "{\"minimum\":1.5,\"maximum\":2.5}"
    }
  },
  {
    "args": {
      "input": "\"ab\"",
      "schema": "{\"allOf\":[{\"type\":\"string\"},{\"minLength\":3}],\"anyOf\":[{\"type\":\"number\"},{\"const\":\"abc\"}],\"oneOf\":[{\"type\":\"string\"},{\"minLength\":1}],\"not\":{\"maxLength\":5}}"
    }
  },
  {
    "args": {
      "input": "\"abc\"",
      "schema": "{\"anyOf\":[{\"type\":\"number\"},{\"const\":\"abc\"}],\"oneOf\":[{\"type\":\"number\"},{\"type\":\"string\"}],\"not\":{\"type\":\"null\"}}"
    }
  },
  {
    "args": {
      "input": "{\"a/b\":{\"c~d\":1}}",
      "schema": "{\"properties\":{\"a/b\":{\"properties\":{\"c~d\":{\"type\":\"null\"}}}}}"
    }
  },
  {
    "args": {
      "input": "{\"x\":\"zzz\"}",
      "schema": "{\"type\":\"object\",\"properties\":{\"x\":{\"pattern\":\"^a$\",\"format\":\"email\",\"$ref\":\"#/nope\"}}}"
    }
  },
  {
    "args": {
      "input": "1",
      "schema": "{\"type\":\"strin\"}"
    }
  },
  {
    "args": {
      "input": "1",
      "schema": "{\"properties\":{\"a\":{\"items\":5}}}"
    }
  },
  {
    "args": {
      "input": "1",
      "schema": "{\"required\":[\"a\",1]}"
    }
  },
  {
    "args": {
      "input": "[]",
      "schema": "{\"minItems\":-1}"
    }
  },
  {
    "args": {
      "input": "[]",
      "schema": "{\"minItems\":2.0,\"anyOf\":[]}"
    }
  },
  {
    "args": {
      "input": "1",
      "schema": "{\"allOf\":[true,{\"not\":\"no\"}]}"
    }
  },
  {
    "args": {
      "input": "[]",
      "schema": "{\"uniqueItems\":\"yes\",\"type\":\"array\"}"
    }
  },
  {
    "args": {
      "input": "1",
      "schema": "[]"
    }
  },
  {
    "args": {
      "input": "{\"deep\":[{\"id\":1},{\"id\":0},{\"no\":1},5]}",
      "schema": "{\"type\":\"object\",\"properties\":{\"deep\":{\"type\":\"array\",\"items\":{\"type\":\"object\",\"required\":[\"id\"],\"properties\":{\"id\":{\"type\":\"integer\",\"exclusiveMinimum\":0}}}}}}"
    }
  },
  {
    "args": {
      "input": "{\"path\":\"src/lib.rs\",\"mode\":\"w\"}",
      "schema": "{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\",\"pattern\":\"^[\\\\w./-]+$\"},\"mode\":{\"type\":\"string\",\"pattern\":\"^(r|w|a)$\"}},\"required\":[\"path\"]}"
    }
  },
  {
    "args": {
      "input": "{\"path\":\"../x\",\"mode\":\"x\"}",
      "schema": "{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\",\"pattern\":\"^[\\\\w./-]+$\"},\"mode\":{\"type\":\"string\",\"pattern\":\"^(r|w|a)$\"}},\"required\":[\"path\"]}"
    }
  },
  {
    "args": {
      "input": "\"ab\"",
      "schema": "{\"type\":\"string\",\"minLength\":3,\"pattern\":\"^\\\\d+$\"}"
    }
  },
  {
    "args": {
      "input": "1",
      "schema": "{\"pattern\":\"^a$\"}"
    }
  },
  {
    "args": {
      "input": "{\"a\":\"b\"}",
      "schema": "{\"pattern\":\"^a$\"}"
    }
  },
  {
    "args": {
      "input": "{\"x\":\"a\"}",
      "schema": "{\"properties\":{\"x\":{\"pattern\":\"(\",\"type\":\"string\"}}}"
    }
  },
  {
    "args": {
      "input": "{}",
      "schema": "{\"properties\":{\"x\":{\"pattern\":5}}}"
    }
  },
  {
    "args": {
      "input": "{}",
      "schema": "{\"properties\":{\"x\":{\"pattern\":\"a{10001}\"}}}"
    }
  },
  {
    "args": {
      "input": "1",
      "schema": "{\"pattern\":\"[\",\"oneOf\":[],\"properties\":5}"
    }
  },
  {
    "args": {
      "input": "\"zzz\"",
      "schema": "{\"pattern\":\"^a$\",\"format\":\"email\",\"$ref\":\"#/nope\"}"
    }
  },
  {
    "args": {
      "input": "\"abc\"",
      "schema": "{\"anyOf\":[{\"pattern\":\"^a\"},{\"pattern\":\"b$\"}],\"not\":{\"pattern\":\"c\"}}"
    }
  }
]
Versions (units)1›
  1. v1by dave0d9037bfe4latestconfirmed10/26/2026
Verification8›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 49 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • The reference imports agent-regex. Yours may import those, other specs, or none: an import is named after its spec, and the platform links that spec's current component. A verified implementation of a spec is interchangeable with any other.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 500,000,000 instructions.
  • Already built: a component that passes and uses at most 0.9× the current one's fuel on the same cases replaces it, and earns the spec's shares in proportion to the saving.
  • Found an input where the current component disagrees with the spec? Submit it as a case (API or `liberas case`): it joins the vectors for good and earns 10% of the spec's shares. A component that then passes replaces the broken one for 25%.