
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
input-data-validator
Advanced tools
Valida los datos de entrada de una ruta de un servicio web creado con express.
Existen varias herramientas para validar los datos de entrada, sin embargo, cuando se trabaja con un ORM como Sequelize se definen modelos cuyos atributos tienen un tipo de dato definido y en algunos casos un validador, sería interesante poder reutilizar estos atributos evitando de esa forma una doble implementación.
Input Data Validator es un validador que crea esquemas de validación utilizando objetos de tipo FieldGroup los cuales se crean con la librería field-creator. Esta librería crea estos objetos a partir de modelos Sequelize, por lo que solamente será necesario incluir los validadores dentro de los modelos.
FieldGroup.Un esquema de validación, es un objeto que tiene las propiedades headers, params, query y body, los cuales son objetos de tipo FieldGroup.
const INPUT = {
headers : Field.group(LIBRO, { ... }),
params : Field.group(LIBRO, { ... }),
query : Field.group(LIBRO, { ... }),
body : Field.group(LIBRO, { ... })
}
Para crear el objeto input, se recomienda utilizar la librería field-creator.
const { Validator } = require('input-data-validator')
const app = express()
const LIBRO = sequelize.define('libro', { ... })
const INPUT = {
body: Field.group(LIBRO, {
titulo : THIS({ alowNull: false }),
precio : THIS({ alowNull: false })
})
}
const inputOptions = { remove: ['body'] }
app.post('/libros', Validator.validate(INPUT, inputOptions), (req, res, next) => {
return res.status(201).json({ status: 'OK', data: req.body })
})
// Para capturar los errores de validación.
app.use((err, req, res, next) => {
if (err.name === 'InputDataValidationError') {
// Error de validación
}
})
app.listen(4000)
ValidationError| Propiedad | Descripción |
|---|---|
name | Nombre del error. |
errors | Lista de errores. |
errors.path | Ruta del campo. |
errors.value | Valor actual del campo. |
errors.msg | Mensaje de error. |
Para instalar sobre un proyecto, ejecutar el siguiente comando:
$ npm install --save input-data-validator
const { Validator } = require('input-data-validator')
const { Field, THIS } = require('field-creator')
const express = require('express')
const LIBRO = sequelize.define('libro', {
id : Field.ID(),
titulo : Field.STRING({ allowNull: false, allowNullMsg: `Se requiere el título.` }),
precio : Field.FLOAT({ validate: { min: { args: [0], msg: `El precio debe ser mayor o igual a 0.` } } })
})
const INPUT = {
body: Field.group(LIBRO, {
titulo : THIS(),
precio : THIS()
})
}
const app = express()
app.post('/libros', Validator.validate(INPUT), (req, res, next) => {
res.status(201).json({ status: 'OK', data: req.body })
})
app.use((err, req, res, next) => {
if (err.name === 'InputDataValidationError') {
return res.status(400).json({ status: 'FAIL', error: err })
}
return res.status(500).json({ status: 'FAIL', error: err })
})
app.listen(4000)
curl -H "Content-Type: application/json" -X POST -d '{ "id": 123, "titulo": "El cuervo", "precio": 11.99 }' http://localhost:4000/libros
{
"status": "OK",
"data": {
"titulo": "El cuervo",
"precio": 11.99
}
}
curl -H "Content-Type: application/json" -X POST -d '{ "precio": -124 }' http://localhost:4000/libros
{
"status": "FAIL",
"error": {
"name": "InputDataValidationError",
"errors": [
{
"path": "body.titulo",
"value": null,
"msg": "Se requiere el título."
},
{
"path": "body.precio",
"value": -124,
"msg": "El precio debe ser mayor o igual a 0."
}
]
}
}
FAQs
Valida los datos de entrada de una ruta de un servicio web creado con express.
We found that input-data-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.