Skip to content

Commit

Permalink
Show example of function message (#1674)
Browse files Browse the repository at this point in the history
  • Loading branch information
karlhorky committed May 29, 2022
1 parent 7641935 commit fd8d602
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1087,11 +1087,13 @@ let jimmySchema = string().test(
);

// or make it async by returning a promise
let asyncJimmySchema = string().test(
'is-jimmy',
'${path} is not Jimmy',
async (value, testContext) => (await fetch('/is-jimmy/' + value)).responseText === 'true',
});
let asyncJimmySchema = string()
.label('First name')
.test(
'is-jimmy',
({ label }) => `${label} is not Jimmy`, // a message can also be a function
async (value, testContext) => (await fetch('/is-jimmy/' + value)).responseText === 'true',
});

await schema.isValid('jimmy'); // => true
await schema.isValid('john'); // => false
Expand Down

0 comments on commit fd8d602

Please sign in to comment.