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

yaml-validator

v5.1.0

Published

Validate Yaml files and enforce a given structure

Downloads

26,750

Readme

yaml-validator

Validate Yaml files and enforce a given structure

Ubuntu Build Status Windows build status code coverage Code Smells

Yaml files are parsed via js-yaml and the structure defined in the configuration options is enforced with check-type.

Getting Started

Please note that the minimum supported version of Node.js is 18.12.0, which is the active Long Term Support (LTS) version.

This tool can be used in two ways, either via Node.js script, or as a command line tool. Note that when used via command line, custom structure cannot be validated.

Installation when used via Node.js script:

npm install yaml-validator --save-dev

Installation when used as a command line tool:

npm install --global yaml-validator

Usage as a part of a Node.js script:

const YamlValidator = require('yaml-validator');

// Default options
const options = {
  log: false,
  structure: false,
  onWarning: null,
  writeJson: false
};

const files = [
  'file paths',
  'that exists',
  'somewhere',
  'and are Yaml files'
];

const validator = new YamlValidator(options);
validator.validate(files);
validator.report();

Using via command line tool, the only argument would be the Yaml file which should be validated:

yaml-validator random_file.yml

The available options for command line use, can be seen with the help command yaml-validator -h, which results in output similar to:

yaml-validator [options] <files>

  -h, --help             Help and usage instructions
  -V, --version          Version number
  -w, --write-json       Write the contents of the Yaml file to a JSON file next to it
  -l, --log-file String  Log file where errors are written

Version 5.0.0

When used from the command line, the process exits with the number of invalid files.

Configuration options

All options are false by default which disables their use.

options.log

Type: string

Default value: false

In case the value is not false, the given string will be used as log file where all the task output is written.

options.structure

Type: object

Default value: false

The most complex style of checking validity.

options.onWarning

Type: function

Default value: null

One of the options passed to load method of js-yaml.

Please note that the onWarning callback is being used by this library and any method written for it, will be run after the one implemented in this library. The callback get called with two parameters, of which the first is the error in question, while the second is the file path of the given Yaml file.

options.writeJson

Type: boolean

Default: false

Write the given Yaml file as pretty printed JSON in the same path, just by changing the file extension to json.

Please note that any existing JSON files will be cruelly overwritten.

Typescript Support

YamlValidator ships with its own typing definition in the library, no need to use @types.

Examples

Structure validation options

In case an array is found, all its members are assumed to have the given structure. This can be seen in the classRooms property, which according to the configuration below, should be an array, for which all items are objects, which all should have a name and id properties, with the given types.

The teachers array is made of strings, thus all items in that array must be a string.

const options = {
  structure: {
    school: {
      'description?': 'string', //Optional, won't show in invalid array
      code: 'number',
      principal: {
        name: 'string'
      },
      classRooms: [
        {
          name: 'string',
          id: 'number',
          'location?':{
            floor: "string",
            building: "string",
          }
        }
      ],
      teachers: [
        'string'
      ]
    }
  }
};

Warning callback in Yaml parsing options

Using the options.onWarning callback, the possible parsing errors can be retrieved.

const options = {
  onWarning: function (error, filepath) {
    console.log(filepath + ' has error: ' + error);
  }
};

Write a JSON file option

It is possible to use the options.writeJson to have all the files processed, to be saved in JSON format, in the same file path as the original Yaml files.

const options = {
  writeJson: true
};

Contributing

"A Beginner's Guide to Open Source: The Best Advice for Making your First Contribution".

Also there is a blog post about "45 Github Issues Dos and Don’ts".

Linting is done with ESLint and can be executed with npm run lint. There should be no errors appearing after any JavaScript file changes.

Please note that any features or changed will not be merged without working unit tests.

Unit tests are written with tape and can be executed with npm test. Code coverage is inspected with nyc and can be executed with npm run coverage after running npm test. Please make sure it is over 90% at all times.

Version History

Changes happening across different versions and upcoming changes are tracked in the CHANGELOG.md file.

License

Copyright (c) Juga Paazmaya [email protected]

Licensed under the MIT license.