trainiac / express-jsonschema Goto Github PK
View Code? Open in Web Editor NEWexpress.js middleware for jsonschema validation.
express.js middleware for jsonschema validation.
Is there any way to check which array has error and then save it in a variable while saving the arrays that follow the rules on in a different variable.
Example
[
{
"ruleId":12345,
"sender":"amazon.de",
"subject":"Your order placed",
"body":"Your order acounting for your purchase...",
"_ID":12,
"createdAt":"2015-10-09T12:36:57+01:00",
"receivedAt":"2015-10-09T12:30:57+01:00"
},
{
"ruleId":1455,
"sender":"ebay.de",
"subject":"Order invoice",
"body":"Hereby attached is the invoice...",
"createdAt":"2015-10-09T12:36:57+01:00",
"receivedAt":"2015-10-09T12:36:57+01:00"
}
]
The first one will give no error so we could save it in a variable say processed. and the second doesn't follow the conditions thus save it in unprocessed along with the errors.
I must be doing something wrong as there is no issue reported for this but I've been using pretty much the vanilla README.md example/ what you seem to have in your tests and this happens. I'd be happy to do a PR to update your example to be a full app to maybe help someone else avoid this in the future once we find the err in my ways. Thanks!
$ curl -H "Content-Type: application/json" -X POST -d '{ "number": "12", "type": "Drive"}' http://localhost:8080/street
[object Object]
//npm install express
//npm install express-jsonschema
//npm install body-parser
var express = require('express');
var app = express();
var validate = require('express-jsonschema').validate;
app.use(require('body-parser').json());
var StreetSchema = {
type: 'object',
properties: {
number: {
type: 'number',
required: true
},
name: {
type: 'string',
required: true
},
type: {
type: 'string',
required: true,
enum: ['Street', 'Avenue', 'Boulevard']
}
}
};
app.post('/street/', validate({body: StreetSchema}), function(req, res) {
res.json({message: 'success!'});
});
app.listen(8080, () => console.log('app is running'));
Validating request body over a post method works just fine.
But I also wanted to validate the same object(that I'm sending in request body) when passing through another function.
// I need to validate the "object" before passing through this function.
const result = someFunction(object);
Referring another similar package(jsonschema
), I tried doing
console.log(validate(4, {"type": "number"}));
using express-jsonschema
but this returns a value [Function]
.
Could we not do the same thing using express-jsonschema
?
I am wondering if data that is supplied in addition to the schema can be dropped?
Similar to mongoose where you can supply any data you want, but it will only store that which matches the schema.
Using the example from the README if I supply the following object:
{
number: 12,
name: 'Sycamore',
type: 'Street',
extra: 'Dodgy data'
}
How can I stop that extra property being passed to my controller with this module?
In my payload I am looking to accept a UTC date time string to express an expiration date of the resource.
How do I specify that validation type?
Should I just accept a string and validate the string is a valid ISO 8601 date time myself?
Any advice and alternatives? Keeping the same error format as this module would be perfect
Thanks for this wonderful library. I was wondering if it is possible to somehow convert a Swagger schema definition to something that we can use with this library. If, so can you please point me in the right direction
Given an API route:
router.get('/users/:id', function(req, res, next) {
res.send('respond with a resource');
});
How would I write the schema to validate req.params.id? The docs only show the lib validating the body. Thanks
jsonschema is incredibly slow compared to ajv (https://github.com/epoberezkin/ajv).
I have three [simplified, for demonstrative purposes] schema objects defined.
Quiz
var quiz_schema = {
"id": "/QuizSchema",
"type": "object",
"properties": {
"name": {
"type": 'string',
"required": true
},
"questions": {
"type": "array",
"minItems": 1,
"required": true,
"items": {
"$ref": "/QuestionSchema"
}
}
}
};
Question
var question_schema = {
"id": "/QuestionSchema",
"type": "object",
"properties": {
"choices": {
"type": "array",
"minItems": 2,
"required": true,
"items": {
"$ref": "/ChoiceSchema"
}
},
"correct_choice": {
"type": "string",
"required": true
},
"text": {
"type": 'string',
"required": true
},
"img": {
"type": 'string',
"required": false
},
"img_alt": {
"type": 'string',
"required": false
}
}
};
Choice
var choice_schema = {
"id": "/ChoiceSchema",
"type": "object",
"properties": {
"text": {
"type": "string",
"required": true
}
}
};
The goal of which is to validate simple quizzes in JSON format of the following form:
{
"name": "CS 101 - What's a computer?",
"questions": [
{
"text": "Which of the following is not a form of storage media?",
"choices": [
{
"text": "Betamax"
},
{
"text": "CD-ROM"
},
{
"text": "BD-ROM"
},
{
"text": "Abraham Lincoln"
}
],
"correct_choice": "Abraham Lincoln"
},
{
"text": "Which of the following computer scientists is not one of original developers of Unix?",
"img": "https://upload.wikimedia.org/wikipedia/commons/1/1b/Abraham_Lincoln_November_1863.jpg",
"img_alt": "Who is in this picture?",
"choices": [
{
"text": "Macho Man Randy Savage"
},
{
"text": "Ken Thompson"
},
{
"text": "Dennis Ritchie"
},
{
"text": "Brian Kernighan"
}
],
"correct_choice": "Macho Man Randy Savage"
}
]
}
In my quizzes.js
router, I attempt to perform this validation in the following way per this example:
router.post('/', validate({ body: quizSchema }, [questionSchema, choiceSchema]), function (req, res) {
// omitted
});
However, when I
npm start
I get:
url.js:110
throw new TypeError("Parameter 'url' must be a string, not " + typeof url)
^
TypeError: Parameter 'url' must be a string, not number
at Url.parse (url.js:110:11)
at urlParse (url.js:104:5)
at Object.urlResolve [as resolve] (url.js:437:10)
at Validator.addSubSchema (/<PATH_TO_PROJECT>/node_modules/express-jsonschema/node_modules/jsonschema/lib/validator.js:61:36)
at Validator.addSchema (/<PATH_TO_PROJECT>/node_modules/express-jsonschema/node_modules/jsonschema/lib/validator.js:42:8)
at Array.forEach (native)
at validate (/<PATH_TO_PROJECT>/node_modules/express-jsonschema/index.js:164:28)
at Object.<anonymous> (/<PATH_TO_PROJECT>/routes/quizzes.js:29:18)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
npm ERR! Darwin 14.5.0
npm ERR! argv "node" "/usr/local/bin/npm" "start"
npm ERR! node v0.12.7
npm ERR! npm v2.13.5
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 1
I suspect that I'm running into trouble with the use of the id
field - it looks like there's conceivably a great deal that this property is used for on the backend. Is there the expectation that all the schema files should be accessible over HTTP from http[s]://<domain>/object.json
?
Any help appreciated! ๐
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.