@doubter/plugin-string-format
v3.1.0
Published
String format validation plugin for Doubter.
Downloads
105
Maintainers
Readme
@doubter/plugin-string-format
String format validation plugin for Doubter.
- ASCII
- BIC
- Fully qualified domain name
- IMEI number
- IP
- ISIN
- Luhn algorithm
- MIME type
- UUID
npm install --save-prod doubter @doubter/plugin-string-format
How to use?
Import and enable the plugin:
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:
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.*"
code.
d.string().email().try('foo');
The code above would return an Err
result:
{
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
option
to parsing methods:
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:
d.string().email('Not an email').parse('foo');
// ❌ ValidationError: string.format.email at /: Not an email
More details in the Localization section of Doubter docs.