Strickland
Search…
compare
The compare validator is quite similar to the letter validator we've built in our examples.
If the value being validated is null, false, an empty string, or another falsy value other than 0, then the result will be valid. This respects the rule of thumb described in the notes for the required validator.

Named Props

  • compare: The value compared against

Parameters

The compare validator supports three parameter signatures:
  1. 1.
    compare(value) where the value is used as the compare named prop
  2. 2.
    compare(propsObject) where the props object contains a compare named prop
  3. 3.
    compare(propsFunction) where the props function returns a props object with a compare named prop

Usage

1
import validate, {compare} from 'strickland';
2
3
// As a value parameter
4
const letterA = compare('A');
5
6
// As a named prop
7
const letterB = compare({
8
compare: 'B',
9
message: 'Must be the letter "B"'
10
});
11
12
// Using a function that resolves to have the named prop
13
const letterValidator = compare((context) => ({
14
compare: context.compare,
15
message: `Must match "${context.compare}"`
16
}));
Copied!
Last modified 1yr ago