Skip to content

Big Checks

Samuel Risner edited this page Jul 11, 2023 · 3 revisions

Big Checks

There are three different kinds of big checks, the difference between them is what you want to check:

  • checkBoolean
  • checkNumber
  • checkString

Importing

You can import the three different check functions like this:

import { checkBoolean, checkNumber, checkString } from "@samuel-risner/json-contents-checker";

Creating a big check

Since the big checks are all functions creating a check with them is quite simple, simply call the function and save its return value for evaluating the check.

You can add different arguments after key in { key: "key0" } for setting different checks. There are multiple different checks that you can set, the documentation and examples are all documented in the comments of the functions.

checkBoolean

const check: CheckFunctionChain = checkBoolean({ key: "key0" });

checkNumber

const check: CheckFunctionChain = checkNumber({ key: "key0" });

checkString

const check: CheckFunctionChain = checkString({ key: "key0" });

Passing props

The three check function all take different props:

checkBoolean

const props: CheckBooleanProps = { key: "key0" };
const check: CheckFunctionChain = checkBoolean(props);

checkNumber

const props: CheckNumberProps = { key: "key0" };
const check: CheckFunctionChain = checkNumber(props);

checkString

const props: CheckStringProps = { key: "key0" };
const check: CheckFunctionChain = checkString(props);

Adding checks

To add checks simply add the appropriate arguments, e.g.:

const check: CheckFunctionChain = checkString({ key: "key0", maxLength: 5, minLength: 2 });

There are four arguments that all checks share:

  • errorCode (defaults to -1)
  • errorMsg (defaults to "")
  • successCode (defaults to 0)
  • successMsg (defaults to "")

Evaluating checks

To evaluate a check call the function that you got returned from creating the check, e.g.:

const check: CheckFunctionChain = checkString({ key: "key0", maxLength: 5, minLength: 2 });
const result: CheckResult = check(mockObject);

You can add an error and success function after const result: CheckResult = check(mockObject, otherwise they default to errorFunctionDud and successFunctionDud.

The result consists of three parts:

  • a boolean: true if the check was successful, otherwise false
  • a number: the success code if the check was successful, otherwise the error code
  • a string: the success message if the check was successful, otherwise the error message