-
Notifications
You must be signed in to change notification settings - Fork 0
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
)
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 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.
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 to0
) -
successMsg
(defaults to""
) -
errorFunction
(defaults toerrorFunctionDud
) -
successFunction
(defaults tosuccessFunctionDud
)
You can create an object containing these arguments as follows:
import { SmallCheckProps } from "@samuel-risner/json-contents-checker";
const props: SmallCheckProps = { key: "", objectToCheck: mockObject };
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 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.
There are four different options for evaluating the checks:
- Using
.combine()
- Using
.evaluate()
- Calling the object
- Using
.combineChain()
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.
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 });
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();
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();
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.
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})
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})
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({})
Directly calling the object does the same as evaluate with the only difference being that .evaluate
is left out.
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})
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})
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)
({})
This function is needed when chaining multiple checks together, see the check chaining page for more information.