checks

package
v0.0.0-...-bd79248 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package checks provides helpers for checking values in linebased scripts.

Example usage with a JSON check:

expr := linebased.Expanded{
	Expression: linebased.Expression{
		Name: "json",
		Body: "/name == \"Alice\"",
	},
}
body := `{"name": "Alice", "age": 30}`
if msg := checks.JSON(expr, body); msg != "" {
	log.Fatal(msg)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTML

func HTML(expr linebased.Expanded, body string) string

HTML checks the inner HTML of elements matching a CSS selector.

It uses Text for comparison, supporting operators like ==, !=, ~, !~, contains, and !contains.

An additional "count" operator compares the number of matched elements against the expected value.

The expression body should contain: selector op want. For example:

div.content == Hello World
h1 contains Welcome
ul>li count 5

Selectors

Selectors must not contain spaces. CSS provides several combinators that can be used without spaces:

  • "parent>child" selects direct children (e.g., "ul>li")
  • "a~b" selects siblings of a that are b (general sibling)
  • "a+b" selects the immediate sibling b after a (adjacent sibling)
  • "a,b" selects elements matching either a or b

For descendant selection (which normally uses a space), use the direct child combinator ">" when applicable, or compose multiple checks.

No Match Behavior

If no elements match the selector, it returns an error saying "no elements match selector {selector}" (except for count operator, which returns 0 and only errors if the expected count is non-zero).

Returns empty string on success, error message on failure.

func JSON

func JSON(expr linebased.Expanded, body string) string

JSON checks a JSON value at an RFC 6901 pointer path.

It uses Text for comparison, supporting operators like ==, !=, ~, !~, contains, and !contains.

The expression body should contain: path op want. For example:

/foo/bar == "baz"

Values are compared as strings. Strings include their quotes, making it easy to check types using string comparison operators:

/foo ~ ^"                            # value is a string
/foo ~ ^\[                           # value is an array
/foo ~ ^\{                           # value is an object
/foo == true                         # boolean true
/foo == null                         # null
/foo == 42                           # integer
/foo == 3.14                         # float
/foo == []                           # empty array
/foo == {}                           # empty object

Undefined

If a path does not exist, the value is "undefined". This is distinct from any valid JSON value, making it safe to test for missing keys:

/missing == undefined

Composing checks

Compose checks to express complex constraints:

/foo ~ ^\[                           # is an array
/foo/9 != undefined                  # has at least 10 items
/foo/10 == undefined                 # has at most 10 items

Returns empty string on success, error message on failure.

func Text

func Text(what, op, got, want string) (msg string, valid bool)

Text compares got against want using the specified operator op and returns a failure message when the comparison does not hold. An empty string means the check passed.

Supported operators:

  • "==": equality
  • "!=": inequality
  • "~": regex match
  • "!~": regex non-match
  • "contains": substring presence
  • "!contains": substring absence

If valid is false, the message indicates an error in the check itself. If valid is true, the message indicates a failed check.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL