Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@stoplight/spectral-core

Package Overview
Dependencies
Maintainers
2
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stoplight/spectral-core

  • 1.16.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
693K
increased by20.98%
Maintainers
2
Weekly downloads
 
Created

What is @stoplight/spectral-core?

@stoplight/spectral-core is a powerful JSON/YAML linter that helps you enforce a set of rules on your JSON/YAML documents. It is commonly used for validating OpenAPI, AsyncAPI, and other API description formats. The package allows you to define custom rules, use built-in rulesets, and extend existing rulesets to ensure your documents adhere to specific standards.

What are @stoplight/spectral-core's main functionalities?

Linting JSON/YAML documents

This feature allows you to lint JSON/YAML documents against a set of predefined or custom rules. In this example, the document is validated against the OpenAPI v2 ruleset.

const { Spectral } = require('@stoplight/spectral-core');
const { isOpenApiv2 } = require('@stoplight/spectral-rulesets');

const spectral = new Spectral();
spectral.setRuleset(isOpenApiv2);

const document = {
  openapi: '2.0.0',
  info: {
    title: 'Sample API',
    version: '1.0.0'
  },
  paths: {}
};

spectral.run(document).then(results => {
  console.log(results);
});

Custom Rules

This feature allows you to define custom rules for your JSON/YAML documents. In this example, a custom rule is created to ensure that the 'info' object must have a 'contact' field.

const { Spectral, RuleType } = require('@stoplight/spectral-core');

const spectral = new Spectral();
spectral.setRules({
  'info-must-have-contact': {
    given: '$.info',
    then: {
      field: 'contact',
      function: 'truthy'
    },
    type: RuleType.STYLE,
    severity: 'error'
  }
});

const document = {
  openapi: '3.0.0',
  info: {
    title: 'Sample API',
    version: '1.0.0'
  },
  paths: {}
};

spectral.run(document).then(results => {
  console.log(results);
});

Extending Existing Rulesets

This feature allows you to extend existing rulesets with additional custom rules. In this example, the OpenAPI v3 ruleset is extended with a custom rule to ensure that the 'info' object must have a 'contact' field.

const { Spectral } = require('@stoplight/spectral-core');
const { isOpenApiv3 } = require('@stoplight/spectral-rulesets');

const spectral = new Spectral();
spectral.setRuleset(isOpenApiv3);

spectral.mergeRules({
  'info-must-have-contact': {
    given: '$.info',
    then: {
      field: 'contact',
      function: 'truthy'
    },
    severity: 'error'
  }
});

const document = {
  openapi: '3.0.0',
  info: {
    title: 'Sample API',
    version: '1.0.0'
  },
  paths: {}
};

spectral.run(document).then(results => {
  console.log(results);
});

Other packages similar to @stoplight/spectral-core

FAQs

Package last updated on 31 Jan 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc