Skip to content

Small Checks

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

Small Checks

There are three different kinds of small checks, the difference between them is when you pass the specific check settings:

  • When evaluating the checks (SmallCheckOnCheck)
  • When combining the chained parts (SmallCheckOnCombine)
  • When creating the object (SmallCheckOnCreation)

Importing

You can import the three different check classes like this:

import { SmallCheckOnCheck, SmallCheckOnCombine, SmallCheckOnCreation } from "@samuel-risner/json-contents-checker";

Since you will probably only use one of the three classes you can import it using an alias:

import { SmallCheckOnCheck as SmallCheck } from "@samuel-risner/json-contents-checker";
import { SmallCheckOnCombine as SmallCheck } from "@samuel-risner/json-contents-checker";
import { SmallCheckOnCreation as SmallCheck } from "@samuel-risner/json-contents-checker";

Creating a small check object

Creating an object for SmallCheckOnCheck and SmallCheckOnCombine is as simple as it gets:

new SmallCheckOnCombine();
new SmallCheckOnCheck();

When using SmallCheckOnCreation you have to pass the arguments right away:

new SmallCheckOnCreation({ key: "key0", objectToCheck: mockObject })

See passing props for more information.

Passing props

When passing arguments to specific functions you sometimes need to pass arguments in squiggly braces ({}).

All of these occurrences in the small checks have the same six arguments:

  • key (mandatory)
  • objectToCheck (mandatory)
  • successCode (defaults to 0)
  • successMsg (defaults to "")
  • errorFunction (defaults to errorFunctionDud)
  • successFunction (defaults to successFunctionDud)

You can create an object containing these arguments as follows:

import { SmallCheckProps } from "@samuel-risner/json-contents-checker";
const props: SmallCheckProps = { key: "", objectToCheck: mockObject };

Non-mandatory "key" and "objectToCheck" arguments

Sometimes the key and objectToCheck arguments are not mandatory in this case you can create an object containing the arguments as follows:

import { SmallCheckPropsOptional } from "@samuel-risner/json-contents-checker";
const props: SmallCheckPropsOptional = {};

Adding checks

Adding checks is the same for all three classes:

new SmallCheck()
.isString(-1, "The thing that you are trying to check isn't a string.")
.maxLength(-2, "The thing is too long", 5)
.minLength(-3, "Ups! Too short.", 2)

When adding a check the first argument is always the error code and the second one is the error message. Sometimes a third argument is required, e.g. for the maximum and minimum length.

There are multiple different checks that you can use, the documentation, examples and things you need to look out for are all documented in the comments of the check functions.

Evaluating checks

There are four different options for evaluating the checks:

  • Using .combine()
  • Using .evaluate()
  • Calling the object
  • Using .combineChain()

.combine()

The combine function returns another function that you have to call to evaluate the checks.

You can use this when you want to set up a check in advance or when using CheckFunctionOnCheck you can reuse the check.

See passing props for more information.

SmallCheckOnCheck

let someCheck: CheckFunctionOnCheck = new SmallCheckOnCheck()
    .isString(-1, "The thing that you are trying to check isn't a string.")
    .maxLength(-2, "The string is too long", 5)
    .minLength(-3, "Ups! Too short.", 2)
    .combine()

someCheck({ key: "key0", objectToCheck: mockObject });

SmallCheckOnCombine

let someCheck: CheckFunctionPreCheck = new SmallCheckOnCombine()
    .isString(-1, "The thing that you are trying to check isn't a string.")
    .maxLength(-2, "The string is too long", 5)
    .minLength(-3, "Ups! Too short.", 2)
    .combine({ key: "key0", objectToCheck: mockObject })

someCheck();

SmallCheckOnCreation

let someCheck: CheckFunctionPreCheck = new SmallCheckOnCreation({ key: "key0", objectToCheck: mockObject })
    .isString(-1, "The thing that you are trying to check isn't a string.")
    .maxLength(-2, "The string is too long", 5)
    .minLength(-3, "Ups! Too short.", 2)
    .combine()

someCheck();

.evaluate()

The evaluate function immediately evaluates the checks. When compared to the combine function it skips the extra function call.

See passing props for more information and also look at the sub-point non-mandatory "key" and "objectToCheck" arguments.

SmallCheckOnCheck

const result: CheckResult = new SmallCheckOnCheck()
    .isString(-1, "The thing that you are trying to check isn't a string.")
    .maxLength(-2, "The string is too long", 5)
    .minLength(-3, "Ups! Too short.", 2)
    .evaluate({ key: "key0", objectToCheck: mockObject})

SmallCheckOnCombine

const result: CheckResult = new SmallCheckOnCombine()
    .isString(-1, "The thing that you are trying to check isn't a string.")
    .maxLength(-2, "The string is too long", 5)
    .minLength(-3, "Ups! Too short.", 2)
    .evaluate({ key: "key0", objectToCheck: mockObject})

SmallCheckOnCreation

const result: CheckResult = new SmallCheckOnCreation({ key: "key0", objectToCheck: mockObject })
    .isString(-1, "The thing that you are trying to check isn't a string.")
    .maxLength(-2, "The string is too long", 5)
    .minLength(-3, "Ups! Too short.", 2)
    .evaluate({})

Calling the object

Directly calling the object does the same as evaluate with the only difference being that .evaluate is left out.

SmallCheckOnCheck

const result: CheckResult = new SmallCheckOnCheck()
    .isString(-1, "The thing that you are trying to check isn't a string.")
    .maxLength(-2, "The string is too long", 5)
    .minLength(-3, "Ups! Too short.", 2)
    ({ key: "key0", objectToCheck: mockObject})

SmallCheckOnCombine

const result: CheckResult = new SmallCheckOnCombine()
    .isString(-1, "The thing that you are trying to check isn't a string.")
    .maxLength(-2, "The string is too long", 5)
    .minLength(-3, "Ups! Too short.", 2)
    ({ key: "key0", objectToCheck: mockObject})

SmallCheckOnCreation

const result3: CheckResult = new SmallCheckOnCreation({ key: "key0", objectToCheck: mockObject })
    .isString(-1, "The thing that you are trying to check isn't a string.")
    .maxLength(-2, "The string is too long", 5)
    .minLength(-3, "Ups! Too short.", 2)
    ({})

.combineChain()

This function is needed when chaining multiple checks together, see the check chaining page for more information.