npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

swagger-validate

v0.1.7

Published

Detailed validation of objects against a given swagger spec

Downloads

16

Readme

Validate Swagger Objects

Build Status

A detailed validation provider for Swagger objects.

Given a relevant Swagger spec, this tool will provide detailed information about any validation errors which can be caught automatically. This is useful for catching invalid requests to a server on the client-side before a call is ever issues. Currently, these objects can be validated according to their Swagger specification:

Basic Example

var catModel = {
    id: 'Cat',
    required: ['name'],
    properties: {
      name: { type: 'string' },
      age: { type: 'number' }
    }
};

var myCat = {
    name: 'Grumpy',
    age: 'blue'
};

var error = swaggerValidate.model(myCat, catModel);

console.error(error.toString());
// ValidationErrors: Cat is invalid:
//   age is invalid: "blue" is not a number (got a string instead)

Installation and Use

For nodejs: npm install swagger-validate then use var swaggerValidate = require('swagger-validate') to include it in a script.

For browsers: bower install swagger-validate or include the ./dist/swagger-validate.js file as a script tag to put the swaggerValidate function in the global scope. You may also require it with browserify or with Requirejs instead of including it as a script tag.

API

var error = swaggerValidate.model(object, model[, models])

Validate an object using a given model spec.

Parameters

  • object - the instance to validate against the defined model
  • model - the model to use when validating the object
  • models - optional map of model names to models to be used when dereferencing linked models (such as $refs or inherited properties).

Returns

  • error or undefined - if a validation error is found, a ValidationErrors object will be returned with the details of the error(s).

swaggerValidate.errors.ValidationErrors

The primary error object emitted by the validator with the following properties:

  • name - The name of the error (always 'ValidationErrors')
  • message - A human readable message of the error
  • specName - The name of the specification object used for the validation
  • spec - The specification used for the validation (such as a model or an operation object)
  • value - The object which failed the validation
  • errors - A list of ValidationError objects for each invalid field in the given object.

swaggerValidate.errors.ValidationError

This is the wrapper around individual validation errors. Each invalid field in a given object will have one ValidationError object within the ValidationErrors.errors list.

  • name - The name of the error (always 'ValidationError')
  • message - A human readable message of the error
  • specName - The name of the specification object used for the validation
  • spec - The specification used for the validation (such as a model property or an operation parameter)
  • error - A subtype of DataTypeValidationError object with specific error details.

swaggerValidate.errors.DataValidationError

This is a super class for the individual validation errors that can occur in properties. Here's a full list of the different types, all which are accessable via swaggerValidate.errors[name of error]:

  • NotAStringError - The value was expected to be a string but wasn't.
  • NotABooleanError - The value was expected to be a boolen but wasn't.
  • NotAnArrayError - The value was expected to be an array but wasn't.
  • NotVoidError - The value was expected to be void but wasn't.
  • NotANumberError - The value was expected to be a number but wasn't.
  • NotAnIntegerError - The value was a number but not an integer as expected.
  • NumberTooLargeError - The value was a number but over the maximum value allowed by the model.
  • NumberTooSmallError - The value was a number but under the minumum value allowed by the model.
  • DuplicateInSetError - The value is an array which has duplicates, which is not allowed by the model.
  • ErrorsInArrayElementsError - Errors occurred within the elements of an array. Depending on the type of an array these errors may be of ValidationErrors type or subtypes of DataValidationErrors.
  • MissingValueError - The value is required by the model but doesn't exist.

Developing

After installing nodejs execute the following:

git clone https://github.com/signalfx/swagger-validate.git
cd swagger-validate
npm install
npm run dev

The build engine will test and build everything, start a server hosting the example folder on localhost:3000, and watch for any changes and rebuild when nescessary.

To generate minified files in dist:

npm run dist