Skip to content

Commit

Permalink
Add humanise option to message format
Browse files Browse the repository at this point in the history
rossholdway committed Jan 13, 2024
1 parent aed9f55 commit 74dba2b
Showing 3 changed files with 39 additions and 20 deletions.
36 changes: 19 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -151,6 +151,22 @@ str({required_error: "must be provided"})

There is also a helper available to format error messages in a more user friendly way. See the [format helper](#format) for more information.

### Context

Context is available within all rules (and utils).

#### ctx.value
`ctx.value` contains the value.

#### ctx.path
`ctx.path` contains the path taken. This will be an array of property names and / or an index indicating the location of the value that caused the error.

#### ctx.error
`ctx.error(code, message, meta)` takes a code (an error code string), an error message and a meta object to hold additional details.

#### ctx.success
`ctx.success` returns a success object.

### Rules

#### any
@@ -365,7 +381,9 @@ union([
### Helpers
#### format
`format` can be used to generate a nice `Map` of error codes and messages. Path name will be appended to the start of the error message, sentance cased and any `.` or `_` replaced with a whitespace. It will also combine `union` error messages into a single message if the union is invalid.
`format` can be used to generate a nice `Map` of error codes and messages. Path name will be appended to the start of the error message and `union` errors are combined into a single message.
By default the message will be sentance cased and any `.` or `_` replaced with a whitespace. You can pass `{ humanise: false }` as a second argument to prevent this.
```typescript
const schema = obj({
@@ -424,22 +442,6 @@ Predefined error codes.
`invalid_length`
`regex_no_match`
### Context
Context is available within all rules and utils. It contains some useful information and helper methods.
#### ctx.value
`ctx.value` contains the value.
#### ctx.path
`ctx.path` contains the path taken. This will be an array of the object properties or position within the array of the rule.
#### ctx.error
`ctx.error(code, message, meta)` takes a code (an error code string), an error message and a meta object to hold additional details.
#### ctx.success
`ctx.success` returns a success object.
## Contributing
### Tests
7 changes: 5 additions & 2 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -190,10 +190,13 @@ export function isValid<Output>(
return typeof result[0] === "undefined";
}

export function format(errors: Map<string, Err[]>): Map<string, {code: string, message: string}[]> {
export function format(errors: Map<string, Err[]>, options: {
humanise?: boolean
} = {}): Map<string, {code: string, message: string}[]> {
const { humanise = true } = options;
const messages = new Map();
for (const [key, value] of errors) {
const name = (key[0].toUpperCase() + key.slice(1)).replace(/(\.|_)/g, " ");
const name = humanise ? (key[0].toUpperCase() + key.slice(1)).replace(/(\.|_)/g, " ") : key;
const output: {code: string, message: string}[] = [];

for (const e of value) {
16 changes: 15 additions & 1 deletion test/mod.test.ts
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ describe("mod", function () {
});

describe("format", function () {
it("should return expected response", function () {
it("should return expected defaults response", function () {
const error: Err = {
value: undefined,
name: "invalidRule",
@@ -41,6 +41,20 @@ describe("mod", function () {

expect(result).to.eql(new Map([["character.first_name", [{ code: "required", message: "Character first name is required" }]]]));
});

it("should return expected response when humanise is false", function () {
const error: Err = {
value: undefined,
name: "invalidRule",
path: [ "character", "first_name" ],
code: "required",
message: "is required",
meta: undefined
};
const result = format(new Map([["character.first_name", [error]]]), { humanise: false });

expect(result).to.eql(new Map([["character.first_name", [{ code: "required", message: "character.first_name is required" }]]]));
});
});

describe("parse", function () {

0 comments on commit 74dba2b

Please sign in to comment.