Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/smikhalevski/doubter-plugin-string-format
๐คโString format validation plugin for Doubter.
https://github.com/smikhalevski/doubter-plugin-string-format
doubter format string validation
Last synced: 4 days ago
JSON representation
๐คโString format validation plugin for Doubter.
- Host: GitHub
- URL: https://github.com/smikhalevski/doubter-plugin-string-format
- Owner: smikhalevski
- License: mit
- Created: 2023-06-17T21:33:30.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-06-11T12:22:58.000Z (5 months ago)
- Last Synced: 2024-09-16T23:59:26.617Z (about 2 months ago)
- Topics: doubter, format, string, validation
- Language: TypeScript
- Homepage: https://smikhalevski.github.io/doubter-plugin-string-format/
- Size: 236 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# @doubter/plugin-string-format
String format validation [plugin for Doubter](https://github.com/smikhalevski/doubter).
- ASCII
- BIC
- Fully qualified domain name
- IMEI number
- IP
- ISIN
- Luhn algorithm
- MIME type
- UUID```shell
npm install --save-prod doubter @doubter/plugin-string-format
```๐ [Check out the API Docs](https://smikhalevski.github.io/doubter-plugin-string-format)
# How to use?
Import and enable the plugin:
```ts
import * as d from 'doubter';
import '@doubter/plugin-string-format';const emailShape = d.string().email();
emailShape.parse('[email protected]');
// โฎ '[email protected]'emailShape.parse('foo');
// โ ValidationError: string.format.email at /: Must be an email
```Or cherry-pick separate format checkers:
```ts
import * as d from 'doubter';
// ๐ก Import a single format module
import '@doubter/plugin-string-format/bic';const bicShape = d.string().bic();
bicShape.parse('BOFAUS3N');
// โฎ 'BOFAUS3N'bicShape.parse('QUX');
// โ ValidationError: string.format.bic at /: Must be a BIC or SWIFT code
```# Validation issues
Format checks raise issues with [`"string.format.*"`](./src/main/constants.ts) code.
```ts
d.string().email().try('foo');
```The code above would return an `Err` result:
```json5
{
ok: false,
issues: [
{
code: 'string.format.email',
input: 'foo',
message: 'Must be an email',
param: {
allowDisplayName: false,
allowIPDomain: false,
allowUTF8LocalPart: true,
blacklistedChars: '',
hostBlacklist: [],
hostWhitelist: [],
ignoreMaxLength: false,
requireDisplayName: false,
requireTLD: true
}
}
]
}
```# Localization
Provide [`messages`](https://smikhalevski.github.io/doubter/latest/interfaces/core.ParseOptions.html#messages) option
to parsing methods:```ts
const emailShape = d.string().email();emailShape.parse('foo', {
messages: {
'string.format.email': 'Invalid email'
}
});
// โ ValidationError: string.format.email at /: Invalid email
```Or pass a message directly to a plugin method:
```ts
d.string().email('Not an email').parse('foo');
// โ ValidationError: string.format.email at /: Not an email
```More details in the [Localization](https://github.com/smikhalevski/doubter?tab=readme-ov-file#localization) section of
Doubter docs.