How to use the schema-inspector.validate function in schema-inspector

To help you get started, we’ve selected a few schema-inspector examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / BotBuilder-Samples / Node / blog-LUISActionBinding / core / index.js View on Github external
entities = entities.concat([entity]);
    }

    // resolve complete parameters from entities
    entities = crossMatchEntities(entities);

    // merge entities into parameters obj
    var parameters = _.reduce(entities, function (merged, entity) {
        merged[entity.type] = entity.entity;
        return merged;
    }, {});

    // validate and remove those parameters marked as invalid
    var schemaObject = wrap(schema);
    inspector.sanitize(schemaObject, parameters);
    var result = inspector.validate(schemaObject, parameters);
    if (!result.valid) {
        var invalidParameterNames = result.error.map(getParameterName);
        parameters = _.omit(parameters, invalidParameterNames);
    }

    return parameters;
}
github velopert / whotalk.us / whotalk-backend / src / routes / authentication.js View on Github external
};

    // do data validation
    const validation = {
        username: {
            type: 'string',
            pattern: /^[0-9a-z_]{4,20}$/
        },
        email: {
            type: 'string',
            pattern: 'email',
            optional: true
        }
    };

    if (inspector.validate(validation, info).length > 0) {
        return res
            .status(400)
            .json({code: 2, message: 'VALIDATION FAILED'});
    }

    // check username / email duplication
    const p1 = Account.findUser(info.username);
    const p2 = Account.findUserByEmail(info.email);

    // wait for all fulfillments
    Promise
        .all([p1/*, p2*/
        ])
        .then(values => {

            if (values[0]) {
github Darkle / MarkSearch / appmodules / db / pagesdb.js View on Github external
pagesdb.upsertRow = (rowDataObj) => {

  inspector.sanitize(schemas.upsertRowSanitization, rowDataObj)
  /*****
   * Check requestDataValidation.js for why we're making a copy (_.assign) of rowDataObj/req.body data
   * for inspector.validate().
   */
  var validatedPageDataObj = inspector.validate(schemas.upsertRowValidation, _.assign({}, rowDataObj))
  if(!validatedPageDataObj.valid){
    var errMessage = `Error, passed in row data did not pass validation.
                      Error(s): ${ validatedPageDataObj.format() }`
    global.devMode && console.error(errMessage)
    appLogger.log.error({err: errMessage})
    let errorToReturn = new Error(errMessage)
    /****
     * Note: we need to return a blubird promise here, because we use bluebird's
     * bind method in addPage.js and returning a native promise would cause an
     * uncaughtException error as native promise bind is a bit different.
     * Also, throwing an error here would also cause an uncaughtException error
     * because we wouldn't be returning a bluebird promise.
     */
    return Promise.reject(errorToReturn)
  }
  return pagesdb
github Darkle / MarkSearch / appmodules / server / requestDataValidation.js View on Github external
inspector.sanitize(schemas.reqParamsSanitization, reqParams)
  inspector.sanitize(schemas.reqBodySanitization, reqBody)

  req.body = reqBody
  req.params = reqParams

  var validReqParams = inspector.validate(schemas.reqParamsValidation, reqParams)
  if(!validReqParams.valid){
    let errMessage = `Error(s) with the reqParams data in requestDataValidation : ${ validReqParams.format() }`
    let err = new Error(errMessage)
    global.devMode && console.error(errMessage)
    appLogger.log.error({err, req, res})
    return res.status(500).json({errMessage})
  }

  var validReqBody = inspector.validate(schemas.reqBodyValidation, reqBody)
  if(!validReqBody.valid){
    let errMessage = `Error(s) with the reqBody data in requestDataValidation : ${ validReqBody.format() }`
    let err = new Error(errMessage)
    global.devMode && console.error(errMessage)
    appLogger.log.error({err, req, res})
    return res.status(500).json({errMessage})
  }

  if(validReqParams.valid && validReqBody.valid){
    return next()
  }
}
github blockstack / blockstack.js / src / profiles / profileSchemas / creativework.ts View on Github external
static validateSchema(profile: any, strict = false) {
    schemaDefinition.strict = strict
    return inspector.validate(schemaDefinition, profile)
  }
github velopert / whotalk.us / whotalk-backend / src / routes / authentication.js View on Github external
function validateLoginBody(body) {
    const validation = {
        type: 'object',
        properties: {
            username: {
                type: 'string',
                minLength: 1
            },
            password: {
                type: 'string',
                minLength: 1
            }
        }
    };

    return inspector.validate(validation, body);
}
github peterjcaulfield / raiden / lib / config.js View on Github external
Object.keys(config).forEach((key) => {
        let result = inspector.validate(schema, config[key]);
        if (!result.valid) invalidConfigs[key] = result;
    });
github microsoft / BotBuilder-Samples / Node / blog-LUISActionBinding / core / index.js View on Github external
function validate(schema, parameters, onValidationErrors, onValidationPass) {
    var schemaObject = wrap(schema);
    inspector.sanitize(schemaObject, parameters);
    var result = inspector.validate(schemaObject, parameters);
    if (result.valid) {
        onValidationPass(parameters);
    } else {
        var errors = result.error.map(function (fieldError) {
            var parameterName = getParameterName(fieldError);
            var errorMessage = schema[parameterName].message;
            return {
                parameterName: parameterName,
                message: errorMessage
            };
        });

        onValidationErrors(parameters, errors);
    }
}

schema-inspector

Schema-Inspector is a powerful tool to sanitize and validate JS objects.

MIT
Latest version published 1 year ago

Package Health Score

60 / 100
Full package analysis

Popular schema-inspector functions