eslint-plugin-zod
Version:
Zod linting rules for ESLint.
70 lines (69 loc) • 2.87 kB
JavaScript
;
const defaultOptions = {
allowPassthrough: true,
};
const create = (context) => {
return {
CallExpression(node) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
if (((_a = node.callee.object) === null || _a === void 0 ? void 0 : _a.name) !== 'z') {
return;
}
if (node.callee.property.name !== 'object') {
return;
}
const { allowPassthrough, } = (_b = context.options[0]) !== null && _b !== void 0 ? _b : defaultOptions;
if (!node.parent.property) {
context.report({
fix: (fixer) => {
return fixer.insertTextAfter(node, '.strict()');
},
message: 'Add a strict() call to the schema.',
node,
});
}
else if (
// z.object().strict()
((_d = (_c = node.parent) === null || _c === void 0 ? void 0 : _c.property) === null || _d === void 0 ? void 0 : _d.name) !== 'strict' &&
// z.object().merge().strict()
((_h = (_g = (_f = (_e = node.parent) === null || _e === void 0 ? void 0 : _e.parent) === null || _f === void 0 ? void 0 : _f.parent) === null || _g === void 0 ? void 0 : _g.property) === null || _h === void 0 ? void 0 : _h.name) !== 'strict') {
if (((_k = (_j = node.parent) === null || _j === void 0 ? void 0 : _j.property) === null || _k === void 0 ? void 0 : _k.name) === 'passthrough' && allowPassthrough) {
return;
}
if (((_m = (_l = node.parent) === null || _l === void 0 ? void 0 : _l.property) === null || _m === void 0 ? void 0 : _m.name) === 'and') {
// Ignore .and() calls
return;
}
// As far as I can think, in cases where the property name is not-strict,
// e.g. passthrough, we should not add a strict() call.
context.report({
message: 'Add a strict() call to the schema.',
node,
});
}
},
};
};
module.exports = {
create,
meta: {
docs: {
description: 'Requires that objects are initialized with .strict().',
url: 'https://github.com/gajus/eslint-plugin-zod#eslint-plugin-zod-rules-require-strict',
},
fixable: 'code',
schema: [
{
additionalProperties: false,
properties: {
allowPassthrough: {
default: true,
type: 'boolean',
},
},
type: 'object',
},
],
type: 'problem',
},
};