🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

@stoplight/spectral-core

Package Overview
Dependencies
Maintainers
2
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stoplight/spectral-core

1.20.0
latest
Source
npm
Version published
Weekly downloads
906K
-1.81%
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 22 Apr 2025

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