Parameter Validation with _$()

_$(value, "name") returns a validation builder. Call .$_() to require and return a value, or .default(fallback) to return a fallback when the input is undefined. Conversions also return the builder, so they need a terminal call.

This guide follows the pinned source snapshot. Its validation implementation has compatibility quirks described below. The examples use one complete predicate where several conditions must all hold.

Basic Usage

function greet(name) {
  name = _$(name, "name").isString().default("Guest");
  return "Hello, " + name + "!";
}

print(greet("Alice"));    // Hello, Alice!
print(greet(undefined));  // Hello, Guest!
// greet(123) throws; a default does not replace an invalid defined value.

Type Validators

Use isString, isNumber, isTNumber, isBoolean, isArray, isMap, isObject, isDate, isRegExp, isFunction, isJavaObject, or isInstanceOf(Type) followed by a terminal call. Global isDef(value) and isUnDef(value) test presence; they are not methods on this builder. .$_() requires a defined value.

var config = _$({ host: "localhost" }, "config").isMap().$_();
var count = _$("42", "count").toNumber().check(v => isNumber(v) && isFinite(v)).$_();

Value Constraints

For a condition that must satisfy several rules, place all rules inside a single check predicate. Guard access to string or map properties with a type check in that predicate.

function validPort(value) {
  return _$(value, "port").toNumber()
    .check(v => isNumber(v) && isFinite(v) && Math.floor(v) === v && v >= 1 && v <= 65535,
           "port must be an integer from 1 to 65535")
    .default(8080);
}

print(validPort("443"));     // 443
print(validPort(undefined)); // 8080
// validPort("70000") throws.

oneOf(values) checks a scalar against an allowed array. anyOf(values) checks elements of an input array. Validate the input shape as part of a combined predicate if callers may pass either shape.

var status = _$("active", "status")
  .check(v => isString(v) && ["active", "inactive"].indexOf(v) >= 0)
  .$_();
var username = _$("alice_1", "username")
  .check(v => isString(v) && /^[a-zA-Z0-9_]{3,20}$/.test(v))
  .$_();

The regex method is regexp(/pattern/), not match. Numeric comparison methods include less, lessEquals, greater, and greaterEquals; there is no inRange method.

Type Conversion

toNumber(), toString(), toBoolean(), toDate(), toArray() and toMap() convert the current value and return the builder. toArray() splits comma-separated text, and toMap() parses JSON text. toBoolean() interprets a string as true only when its trimmed lowercase value is true; other strings become false. Validate the original vocabulary first if arbitrary strings should be rejected.

var amount = _$("12.5", "amount").toNumber().check(v => isFinite(v)).$_();
var enabled = _$("true", "enabled").oneOf(["true", "false"]).$_() === "true";

Default Values

.default(value) terminates the chain. Do not call .$_() after it. An invalid defined input still throws. Predicates are evaluated even when the input is missing, so avoid dereferencing a missing value.

var title = _$(undefined, "title")
  .check(v => isString(v) && v.length > 0)
  .default("Untitled");

Custom Validation

check accepts either a function or an expression string whose input variable is v:

var total = _$(15, "total").check("isNumber(v) && v >= 0").$_();

For reusable validation, wrap the builder in a function and return the terminal result. Keep explicit checks for failure paths in your tests, particularly when converting user input.

oJob Integration

check.in and check.out use the same builder. The expression variable is v, not the argument’s field name. This complete job validates input and output:

todo:
- name: Calculate total
  args:
    items:
    - price: 5
      quantity: 3
jobs:
- name: Calculate total
  check:
    in:
      items: 'check(v => isArray(v) && v.length > 0)'
    out:
      total: 'check(v => isNumber(v) && isFinite(v) && v >= 0)'
  exec: |
    args.items.forEach(item => {
      _$(item, "item").check(v => isMap(v) && isNumber(v.price) && v.price >= 0
        && isNumber(v.quantity) && v.quantity > 0).$_();
    });
    args.total = args.items.reduce((sum, item) => sum + item.price * item.quantity, 0);
    print(args.total);

For optional fields, port: toNumber.check(v => isNumber(v) && v >= 1 && v <= 65535).default(8080) supplies a default. Array element validation is explicit in the job above; do not assume items[].price is a supported check path.

Snapshot Compatibility

In this source revision, a group of chained validators accepts a defined value when at least one check passes. Do not assume .isNumber().check(range) requires both checks. Use a single combined predicate, or make separate terminal validation calls for independent requirements.

There are also source defects in several helpers: equals and notEquals reject the opposite values to what their names suggest, between and betweenEquals do not return the builder, isUUID rejects a matching UUID, and starts / notStarts reference an undefined variable. Use predicates such as v => v === "active", v => isUUID(v), and v => isString(v) && v.startsWith("prefix-") instead.

These workarounds describe the pinned implementation, not a runtime fix. Review them when updating the baseline. Source: js/openafsigil.js.

See Also


This site uses Just the Docs, a documentation theme for Jekyll.