Installation for Node.js & Command Line
To install the YAML Validator via Node.js, run:
npm install yaml-validator --save-dev
For command-line installation, use:
npm install --global yaml-validator
Usage with Node.js
Using the YAML Validator as part of your Node.js script is simple. Here’s an example:
const YamlValidator = require('yaml-validator');const options = {log: false,structure: false,onWarning: null,writeJson: false};const files = ['file1.yaml', 'file2.yaml'];const validator = new YamlValidator(options);validator.validate(files);validator.report();
Command Line Usage
If you prefer using the command line, simply pass the YAML file you want to validate:
yaml-validator file.yaml
To explore available options:
yaml-validator -h
Available Configuration Options
- log: Save output logs to a specified file.
- structure: Define and enforce the structure of your YAML files.
- onWarning: Capture and process parsing errors with a custom function.
- writeJson: Automatically convert YAML files to JSON format.
Typescript Support
Our YAML Validator includes built-in TypeScript declarations, so no need for additional installations.
Advanced Structure Validation
With YAML Validator, you can define complex structures for validation. For example, enforcing an array of classrooms where each entry must contain specific properties:
const options = {structure: {school: {'description?': 'string',code: 'number',principal: { name: 'string' },classRooms: [{name: 'string',id: 'number','location?': { floor: 'string', building: 'string' }}],teachers: ['string']}}};
JSON Conversion Option
YAML Validator allows you to convert YAML files into formatted JSON files. Enable this option easily:
const options = { writeJson: true };
Contributing to Jimni Nomics' YAML Validator
We encourage contributions to improve our YAML Validator. Please follow best practices such as linting with ESLint and ensuring over 90% code coverage with unit tests.