Skip to content

Commit

Permalink
Merge pull request #9 from vbudovski/feature/error-messages
Browse files Browse the repository at this point in the history
Feature/error messages
  • Loading branch information
vbudovski authored Dec 22, 2024
2 parents 9276ae5 + 81652ef commit 0bbf5a7
Show file tree
Hide file tree
Showing 68 changed files with 1,780 additions and 2,185 deletions.
10 changes: 5 additions & 5 deletions paseri-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"astro": "astro"
},
"devDependencies": {
"@astrojs/check": "^0.8.1",
"@astrojs/starlight": "^0.25.1",
"astro": "^4.10.2",
"@astrojs/check": "^0.9.4",
"@astrojs/starlight": "^0.30.1",
"astro": "^5.0.5",
"rehype-external-links": "^3.0.0",
"sharp": "^0.32.5",
"typescript": "^5.5.3"
"sharp": "^0.33.5",
"typescript": "^5.7.2"
},
"packageManager": "[email protected]+sha256.dae0f7e822c56b20979bb5965e3b73b8bdabb6b8b8ef121da6d857508599ca35"
}
2,999 changes: 1,313 additions & 1,686 deletions paseri-docs/pnpm-lock.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Schema"
title: "Common"
sidebar:
order: 0
---
Expand All @@ -11,7 +11,8 @@ All schemas contain some common methods.
### `parse`

The `parse` method takes a `value` parameter as input. If the `value` conforms to the schema, then the parsed value is
returned with the appropriate type, otherwise an exception is thrown.
returned with the appropriate type, otherwise an exception containing a `messages` method is thrown. See the
[messages](/reference/messages/) reference for details.

```typescript
import * as p from '@vbudovski/paseri';
Expand All @@ -28,7 +29,8 @@ try {
### `safeParse`

The `safeParse` method takes a `value` parameter as input. The result is a discriminated union with a `boolean` member
`ok`. You can use it to extract the parsed value on success, or a list of issues on failure.
`ok`. You can use it to extract the parsed value on success or get the error [messages](/reference/messages/) using
`result.messages()`.

```typescript
import * as p from '@vbudovski/paseri';
Expand All @@ -38,7 +40,7 @@ const result = schema.safeParse('foo');
if (result.ok) {
// Do something with the `result.value`.
} else {
// Do something with the `result.issue`.
// Do something with the `result.messages()`.
}
```

Expand Down
33 changes: 33 additions & 0 deletions paseri-docs/src/content/docs/reference/messages.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: "Messages"
---

In order to support localised error messages, each parsing error produces a unique error code. This is then looked up in
the messages object for the selected locale. When calling the `messages` method on either the `safeParse` result or the
exception thrown from `parse`, you can pass the desired messages object as the parameter. The default messages can be
found in `locales/en.ts`.

## Contributing new translations

You can copy the definition of the closest language to the one you are targeting as a starting point, if one exists.
If there isn't a suitable starting point, please create a new file. Be sure to follow the TypeScript type `Translations`
to ensure all standard messages are handled. The naming of the file should follow the pattern `<language>-<country>`,
where the language is a 2-letter [ISO 639](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes) code, and the
country is a 2-letter [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) code. For example, `en-GB` or
`pt-BR`.

# Custom error codes

You can extend the messages object to add your custom error codes and corresponding translations.

```typescript
import { en } from '@vbudovski/paseri/locales';

const enCustom = {
...en,
...{
foo: 'This is a `foo` error.',
bar: 'This is a `bar` error.',
}
} satisfies Translations;
```
6 changes: 5 additions & 1 deletion paseri-lib/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@vbudovski/paseri",
"license": "MIT",
"version": "0.1.0",
"exports": "./src/index.ts",
"imports": {
Expand All @@ -9,7 +10,7 @@
"esbuild": "npm:esbuild",
"expect-type": "npm:expect-type",
"fast-check": "npm:fast-check",
"type-fest": "npm:type-fest",
"type-fest": "npm:type-fest@4.30.0",
"typescript": "npm:typescript",
"zod": "npm:zod"
},
Expand All @@ -22,5 +23,8 @@
"rules": {
"exclude": ["no-explicit-any", "no-unused-vars"]
}
},
"publish": {
"include": ["src"]
}
}
186 changes: 0 additions & 186 deletions paseri-lib/src/issue.test.ts

This file was deleted.

Loading

0 comments on commit 0bbf5a7

Please sign in to comment.