Skip to content

Commit

Permalink
Add validateWith example to README
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Lefkowitz committed Jan 27, 2024
1 parent fad3019 commit 9790145
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ I want to validate an object and get failure messages for each field:
import { errors, isNaturalNumberString } from "reviewed";

const pagination = (url: URL): void => {
const page = url.searchParams.get("page");
const size = url.searchParams.get("size");

const { valid, parsed, error } = merge({
page: isNaturalNumberString(page),
size: isNaturalNumberString(size),
});
const { valid, parsed, error } = validateWith(
{
page: isNaturalNumberString,
size: isNaturalNumberString,
},
{
page: url.searchParams.get("page"),
size: url.searchParams.get("size"),
}
);

if (valid) {
console.log(parsed);
Expand Down Expand Up @@ -126,7 +129,7 @@ export const isNaturalNumber: Validator<number> = (input: unknown) => {
isIntegerCheck.parsed > 0,
input,
input,
"Not a natural number",
"Not a natural number"
);
};
```
Expand Down Expand Up @@ -196,7 +199,7 @@ const isNumber: Validator<number> = (input: unknown) =>
typeof input === "number" && isFinite(input),
input,
input,
"Not a number",
"Not a number"
);
```

Expand All @@ -215,7 +218,7 @@ const isObject: Validator<object> = (input: unknown) =>
typeof input === "object" && input !== null,
input,
input,
"Not an object",
"Not an object"
);
```

Expand All @@ -229,13 +232,13 @@ const isObject: Validator<object> = (input: unknown) =>

```ts
const isRecord: Validator<Record<string | number | symbol, unknown>> = (
input: unknown,
input: unknown
) =>
validateIf(
isObject(input).valid && !isArray(input).valid,
input,
input,
"Not a record",
"Not a record"
);
```

Expand Down
2 changes: 1 addition & 1 deletion src/models/regexes/RegexValidator.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import { Validated } from "../validation/Validated.model";
* @typeParam T - the validated type
*/
export type RegexValidator<T extends string = ""> = (
obj: unknown,
input: unknown,
) => Validated<ParsedRegex<T>>;
2 changes: 1 addition & 1 deletion src/models/validation/Validator.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { ValidationErrors } from "./ValidationErrors.model";
* @typeParam U - the validation errors type
*/
export type Validator<T, U extends ValidationErrors<T> = string> = (
obj: unknown,
input: unknown,
) => Validated<T, U>;

0 comments on commit 9790145

Please sign in to comment.