express-validator-schema-inference
v0.0.2
Published
type helper to infer express-validator's schema type
Maintainers
Readme
express-validator-schema-inference
TypeScript type helper to infer the resulting data type from an express-validator schema.
This utility allows you to statically infer the shape of validated request data, improving type safety and developer experience in Express projects using express-validator.
Features
- Infers nested object and array types from express-validator schema keys (e.g.,
user.name.first,user.phones.*.number). - Supports common express-validator options (see below).
Supported schema options
The following express-validator schema options are currently supported for type inference:
Validators
isBooleanisDateisFloatisIntisStringisULIDisAlphaisAlphanumericisAsciiisBase32isBase58isBase64isBtcAddressisCreditCardisCurrencyisEmailisISO6346isISO4217isISO8601isObjectnotEmptyisEmptyisNumericisJSONisURLisUUIDisMobilePhoneisLengthcontainsequalsmatchesisIPisHexadecimalisMongoIdisMD5isDecimalisEthereumAddressisFQDNisHashisHexColorisIBANisISBNisISSNisISINisJWTisLatLongisLocaleisLowercaseisMimeTypeisPassportNumberisPortisPostalCodeisRFC3339isSemVerisSlugisUppercaseisDataURIisMagnetURIisMailtoURIisMACAddressisOctalisRgbColorisTimeisVATisWhitelistedisArrayisIn
Sanitizers
toBooleantoDatetoFloattoIntltrimrtrimtrimtoLowerCasetoUpperCaseescapeunescapeblacklistwhitelistnormalizeEmailstripLowtoArray
Special options
optional(marks a field as optional, reflected in the output type)custom(supports custom validators with type guards and assertions)customSanitizer(supports custom sanitizers with return type inference)
Other options may be partially supported if they do not affect the output type. See Limitations below for unsupported or advanced features.
Limitations
- Recursive fields using
**.field(double star for deep/recursive matching) are not supported. - Multiple custom validators/sanitizers using arbitrary keys (as described in the express-validator documentation) are not supported. Only single
customandcustomSanitizerproperties are supported per field. - Not all express-validator features are supported. Please see the source or open an issue if you need support for additional features.
Install
npm install --save-dev express-validator-schema-inferenceUsage example
import type { Request, Response } from 'express';
import type { Schema } from 'express-validator';
import type { Infer } from 'express-validator-schema-inference';
function makeController<T extends Schema>(
schema: T,
handler: (req: Request & { data: Infer<T> }, res: Response) => any
) {
// You should implement makeController so that req.data is set to the return value of matchedData.
}
makeController(
{
'user.name.first': {
isString: true,
trim: true,
},
'user.name.last': {
isString: true,
trim: true,
},
'user.emails': {
isArray: true,
isEmail: true,
},
'user.phones': {
isArray: true,
optional: true,
},
'user.phones.*.number': {
isNumeric: true,
trim: true,
},
'user.phones.*.code': {
isNumeric: true,
trim: true,
},
'user.age': {
isInt: true,
toInt: true,
},
role: {
isIn: { options: [['owner', 'admin', 'user'] as const] },
optional: { options: { values: 'null' } },
},
},
(req, res) => {
req.data;
// {
// user: {
// name: {
// first: string;
// last: string;
// }
// emails: string[];
// phones: {
// number: string;
// code: string;
// }[] | undefined;
// age: number;
// }
// role: "owner" | "admin" | "user" | undefined | null;
// }
}
);
License
MIT License
