Skip to content

Commit

Permalink
Fix pr
Browse files Browse the repository at this point in the history
  • Loading branch information
GentileFulvio committed May 7, 2020
1 parent 29319c5 commit b8e3231
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
26 changes: 18 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,28 +236,39 @@ ow(1, 'input', ow.number.validate(value => ({
//=> ArgumentError: Expected number `input` to be greater than 10, got 1
```

#### message(fn | string)
#### message(string | fn)

Provide a custom message
Provide a custom message:

```ts
ow('🌈', 'unicorn', ow.string.equals('🦄').message('Expected unicorn, got rainbow'));
//=> ArgumentError: Expected unicorn, got rainbow
```

You can also pass in a function as `message` value which accepts the label as argument.
You can also pass in a function which receives the value as the first parameter and the label as the second parameter and is expected to return the message.

```ts
ow('🌈', ow.string.minLength(5).message((value, label) => `Expected ${label}, to be have a minimum length of 5, got \`${value}\``));
ow('🌈', ow.string.minLength(5).message((value, label) => `Expected ${label}, to have a minimum length of 5, got \`${value}\``));
//=> ArgumentError: Expected string, to be have a minimum length of 5, got `🌈`
```

If you want to add a different message per validation it is possible
It's also possible to add a separate message per validation:

```ts
ow('1234', ow.string.minLength(5).message((value, label) => `Expected ${label}, to be have a minimum length of 5, got \`${value}\``).url.message('This is no url'));
ow(
'1234',
ow.string
.minLength(5).message((value, label) => `Expected ${label}, to be have a minimum length of 5, got \`${value}\``)
.url.message('This is no url')
);
//=> ArgumentError: Expected string, to be have a minimum length of 5, got `1234`
ow('12345', ow.string.minLength(5).message((value, label) => `Expected ${label}, to be have a minimum length of 5, got \`${value}\``).url.message('This is no url'));

ow(
'12345',
ow.string
.minLength(5).message((value, label) => `Expected ${label}, to be have a minimum length of 5, got \`${value}\``)
.url.message('This is no url')
);
//=> ArgumentError: This is no url
```

Expand All @@ -272,4 +283,3 @@ This can be useful for creating your own reusable validators which can be extrac

- [@sindresorhus/is](https://github.com/sindresorhus/is) - Type check values
- [ngx-ow](https://github.com/SamVerschueren/ngx-ow) - Angular form validation on steroids

7 changes: 5 additions & 2 deletions source/predicates/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {not} from '../operators/not';
import {BasePredicate, testSymbol} from './base-predicate';
import {Main} from '..';

/**
@hidden
*/
export type ValidatorMessage<T> = (value: T, label?: string) => string;

/**
Expand Down Expand Up @@ -167,9 +170,9 @@ export class Predicate<T = unknown> implements BasePredicate<T> {
}

/**
Override the message thrown
Provide an new error message to be thrown when the validation fails.
@param newMessage | Either a string containing the new message or a function returning the new message
@param newMessage - Either a string containing the new message or a function returning the new message
*/
message(newMessage: string | ValidatorMessage<T>) {
const {validators} = this.context;
Expand Down

0 comments on commit b8e3231

Please sign in to comment.