
Product
Rubygems Ecosystem Support Now Generally Available
Socket's Rubygems ecosystem support is moving from beta to GA, featuring enhanced security scanning to detect supply chain threats beyond traditional CVEs in your Ruby dependencies.
zod-validation-error
Advanced tools
Wrap zod validation errors in user-friendly readable messages.
error.details
;npm install zod-validation-error
// create zod schema
const zodSchema = zod.object({
id: zod.number().int().positive(),
email: zod.string().email(),
});
// parse some invalid value
try {
zodSchema.parse({
id: 1,
email: 'foobar', // note: invalid email
});
} catch (err) {
const validationError = fromZodError(err);
// the error now is readable by the user
// you may print it to console
// or return it via an API
console.log(validationError);
}
Zod errors are difficult to consume for the end-user. This library wraps Zod validation errors in user-friendly readable messages that can be exposed to the outer world, while maintaining the original errors in an array for dev use.
[
{
"code": "too_small",
"inclusive": false,
"message": "Number must be greater than 0",
"minimum": 0,
"path": ["id"],
"type": "number"
},
{
"code": "invalid_string",
"message": "Invalid email",
"path": ["email"],
"validation": "email"
}
]
Validation error: Number must be greater than 0 at "id"; Invalid email at "email"
zod-validation-error
exposes two type-guard utilities that are used to indicate whether the supplied argument is a ValidationError
.
isValidationError(err: unknown): err is ValidationError
isValidationErrorLike(err: unknown): err is ValidationError
isValidationError
is based on an instanceof
comparison, whereas isValidationErrorLike
is using a heuristics-based approach.
In most cases, it is recommended to use
isValidationErrorLike
to avoid multiple-version inconsistencies. For instance, it's possible that a dependency is using an olderzod-validation-error
version internally. In such case, theinstanceof
comparison will yield invalid results because module deduplication does not apply at npm/yarn level and the prototype is different.
import { ValidationError, isValidationErrorLike } from 'zod-validation-error';
const err = new ValidationError('foobar', { details: [] });
isValidationErrorLike(err); // returns true
const invalidErr = new Error('foobar');
isValidationErrorLike(err); // returns false
Source code contributions are most welcome. Please open a PR, ensure the linter is satisfied and all tests pass.
Causaly is building the world's largest biomedical knowledge platform, using technologies such as TypeScript, React and Node.js. Find out more about our openings at https://apply.workable.com/causaly/.
MIT
0.3.0
FAQs
Wrap zod validation errors in user-friendly readable messages
The npm package zod-validation-error receives a total of 3,358,048 weekly downloads. As such, zod-validation-error popularity was classified as popular.
We found that zod-validation-error demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rubygems ecosystem support is moving from beta to GA, featuring enhanced security scanning to detect supply chain threats beyond traditional CVEs in your Ruby dependencies.
Research
The Socket Research Team investigates a malicious npm package that appears to be an Advcash integration but triggers a reverse shell during payment success, targeting servers handling transactions.
Security Fundamentals
The Socket Threat Research Team uncovers how threat actors weaponize shell techniques across npm, PyPI, and Go ecosystems to maintain persistence and exfiltrate data.