/** * Update a single thing * @param {String} name - the name of the Thing to update */ async function updateThing(request, response, next) { const { name } = request.params; const validation = validate(request.body, thingUpdateSchema); if (!validation.valid) { return next( new APIError( 400, "Bad Request", validation.errors.map(e => e.stack).join(". ") ) ); } try { const thing = await Thing.updateThing(name, request.body); return response.json(thing); } catch (err) { return next(err); } }
router.put('/:namespace/:id/env/:env', bodyParser.json({ strict: false, limit: bodyParserLimit }), async (req, res) => { const validationResult = new Validator().validate(req.body, schemas['functions/env']); const memoryStorage = req.app.get('memoryStorage'); if (!validationResult.valid) { const error = 'Invalid instance'; const details = validationResult.errors.map(e => e.toString()); res.status(400).json({ error, details }); return; } const { namespace, id, env, } = req.params; try { await memoryStorage .putCodeEnviromentVariable(namespace, id, env, req.body); res.status(204).end(); } catch (err) { log.error(`[${namespace}:${id}] Failed to set enviroment variable ${env}, error: ${err}`); res.status(err.statusCode || 500).json({ error: err.message }); } });
const validate = (json) => { const v = new Validator(); const list = fileReader.readAsJson(json); const errors = []; for (const l in list) { const error = v.validate(list[l], schema) .errors.map(e => `Item: ${e.instance} - ${e.stack}`); if (Object.keys(list[l]).length !== 8) { errors.push(`Item: ${l} - Don't have the correct number of attributes.`); } errors.push(error); } const errorsFlattened = errors.reduce((a, b) => a.concat(b), []); if (errorsFlattened.length > 0) { console.log('The unified list you provided has errors:'); errorsFlattened.forEach(e => { console.error(e); }); } }
/** * Validate the POST request body and create a new Thing */ async function createThing(request, response, next) { const validation = validate(request.body, thingNewSchema); if (!validation.valid) { return next( new APIError( 400, "Bad Request", validation.errors.map(e => e.stack).join(". ") ) ); } try { const newThing = await Thing.createThing(new Thing(request.body)); return response.status(201).json(newThing); } catch (err) { return next(err); } }
/** * Validates a ShippingAdress using a schema. This implementation either returns false * (no validation errors) or a non-empty array of { field, message } objects which * represent field-level errors. * * @param {ShippingAddress} address */ static validateAddress(address) { const validation = validateShippingAddress(address); let validationResult = false; if (!validation.valid) { validationResult = validation.errors.map((error) => { // jsonschema returns inconsistent properties depending on the validation error - try to make them consistent switch (error.name) { case 'type': return { field: error.property.split('.').slice(-1)[0], message: 'incorrect.type' }; case 'required': return { field: error.argument, message: 'required' }; case 'additionalProperties': return { field: error.argument, message: 'not.allowed' }; default: return { field: error.argument, message: error.name } } }); } return validationResult; }
const details = validationResult.errors.map(e => e.toString());
router.put('/:namespace', bodyParser.json({ limit: bodyParserLimit }), async (req, res) => { try { const validationResult = new Validator().validate(req.body, schemas['namespaces/item']); const memoryStorage = req.app.get('memoryStorage'); if (!validationResult.valid) { const error = 'Invalid instance'; const details = validationResult.errors.map(e => e.toString()); res.status(400).json({ error, details }); return; } const { namespace, } = req.params; const data = Object.assign({}, req.body, { namespace }); await memoryStorage.putNamespace(namespace, data); const functionsRequest = new FunctionsRequest(req); new SchemaResponse(functionsRequest, res, 'namespaces/item').json(data); } catch (err) { log.error(err.message); log.error(err.stack); res.status(500).json({ error: err.message }); } });