Skip to content

Commit

Permalink
1.6.0
Browse files Browse the repository at this point in the history
`soft` versions now include a [typed error property `GenericError { issues?: GenericIssue[] }`](./src/lib.ts#L73), which is the error thrown by the validator. It is based on a Zod error, but you may type it as something else if your parser throws a different error type. This is helpful for type narrowing (ie. you don't have to type `(result.error as any).issues` to get to the issues).

If you're not using Zod, to type the error property, pass it as the second generic type argument to `soft` methods. For example, `softForm<typeof myschema.parse, MyErrorType>(obj, myschema.parse)`. Note that you have to also manually type the parser if you want type its error differently.
  • Loading branch information
miunau committed Apr 5, 2024
1 parent 2362e60 commit 1dd1365
Show file tree
Hide file tree
Showing 7 changed files with 500 additions and 78 deletions.
18 changes: 12 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
# Changelog

## 1.6.0 (2024-04-05)

`soft` versions now include a [typed error property `GenericError { issues?: GenericIssue[] }`](./src/lib.ts#L73), which is the error thrown by the validator. It is based on a Zod error, but you may type it as something else if your parser throws a different error type. This is helpful for type narrowing (ie. you don't have to type `(result.error as any).issues` to get to the issues).

If you're not using Zod, to type the error property, pass it as the second generic type argument to `soft` methods. For example, `softForm<typeof myschema.parse, MyErrorType>(obj, myschema.parse)`. Note that you have to also manually type the parser if you want type its error differently.

## 1.5.2 (2024-04-03)

- `soft` versions can now also return the parsed value if there was one returned but it did not pass the validation. This is useful when you want to show the user the parsed value in the form after a validation error.
`soft` versions can now also return the parsed value if there was one returned but it did not pass the validation. This is useful when you want to show the user the parsed value in the form after a validation error.

## 1.5.1 (2024-04-03)

- Added missing export for `BodyguardFormConfig` interface
Added missing export for `BodyguardFormConfig` interface

## 1.5.0 (2024-02-20)

### New Features

- **File upload support in multipart forms.** `form()` and `softForm()` will return uploaded files as [File objects](https://developer.mozilla.org/en-US/docs/Web/API/File).
**File upload support in multipart forms.** `form()` and `softForm()` will return uploaded files as [File objects](https://developer.mozilla.org/en-US/docs/Web/API/File).

## 1.4.0 (2024-02-19)

### Breaking Changes

- The `error` returned from `soft*` methods is now the error thrown by the handler without coercing it into a string. If you need further type narrowing, you can use the `as` operator in your catch block. Bodyguard errors are regular `Error` instances with the message as one of the consts from `ERRORS`.
The `error` returned from `soft*` methods is now the error thrown by the handler without coercing it into a string. If you need further type narrowing, you can use the `as` operator in your catch block. Bodyguard errors are regular `Error` instances with the message as one of the consts from `ERRORS`.

### New Features

- You can pass `convertPluses` as an option to the form methods to convert `+` to spaces in the form data when it's submitted in URL-encoded format. It won't affect multipart form data.
You can pass `convertPluses` as an option to the form methods to convert `+` to spaces in the form data when it's submitted in URL-encoded format. It won't affect multipart form data.

### Updates

- Updated dependencies.
Updated dependencies.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ Below are the methods and types available in the Bodyguard class.

### Form parsing

#### `Bodyguard.softForm(input, validator, options): Promise<BodyguardResult<ReturnType<ValidatorType>>>`
#### `Bodyguard.softForm<ValidatorType, ErrorType>(input, validator, options): Promise<BodyguardResult<ReturnType<ValidatorType>, ErrorType>>`

Parses an urlencoded or multipart form data stream into a JavaScript object. If an error occurs, it is returned instead of throwing.

Expand Down Expand Up @@ -372,7 +372,7 @@ Returns the parsed object (not a `BodyguardResult`).

### JSON parsing

#### `Bodyguard.softJson(input, validator, options): Promise<BodyguardResult<ReturnType<ValidatorType>>>`
#### `Bodyguard.softJson<ValidatorType, ErrorType>(input, validator, options): Promise<BodyguardResult<ReturnType<ValidatorType>, ErrorType>>`

Parses a JSON stream into a JavaScript object. If an error occurs, it is returned instead of throwing.

Expand Down Expand Up @@ -404,7 +404,7 @@ Returns the parsed object (not a `BodyguardResult`).

### Text parsing

#### `Bodyguard.softText(input, validator, options): Promise<BodyguardResult<ReturnType<ValidatorType>>>`
#### `Bodyguard.softText<ValidatorType, ErrorType>(input, validator, options): Promise<BodyguardResult<ReturnType<ValidatorType>, ErrorType>>`

Parses raw UTF-8 text into a string. The byte limit is enforced but no key or depth limits are enforced as there is no way to know what the structure of the text is. If an error occurs, it is returned instead of throwing.

Expand All @@ -424,7 +424,7 @@ Parses raw UTF-8 text into a string. The byte limit is enforced but no key or de

### Automatic content type detection

#### `Bodyguard.softPat(input, validator, options): Promise<BodyguardResult<ReturnType<ValidatorType>>>`
#### `Bodyguard.softPat<ValidatorType, ErrorType>(input, validator, options): Promise<BodyguardResult<ReturnType<ValidatorType>, ErrorType>>`

Parses a request or response body into a JavaScript object. Internally uses `softJson()` or `softForm()` depending on the content type. If an error occurs, it is returned instead of throwing.

Expand Down
Loading

0 comments on commit 1dd1365

Please sign in to comment.