Giter Club home page Giter Club logo

schema-validator's Introduction

YAML/JSON Schema Validator

Schema validation utility for YAML/JSON files against a pre defined schema

LICENSE VERSION DOWNLOADS VULNERABILITY ISSUES

Table of Contents

Description:

Validate is a utility used to check the structure of a yaml/json file against a predefined schema. The schema is expected to be a JSON or YAML file with a structure that defines type of each property. The object properties can be nested to as many levels as you like.

Usage

It's method validateSchema can be imported and used as below:

const validateSchema = require('yaml-schema-validator')

// validate a json OR yml file
validateSchema('path/to/target-file.yml', {
  schemaPath: '/path/to/required/schema.yml' // can also be schema.json
})

The method automatically detects if file format is JSON or YAML and process it accordingly.

Similarly, method can also be used to validate plain JS objects:

// validate an object
let person = { name: { first: 'Tom', last: 'Xoman' }, age: 45 }
vaidateSchema(person, {
  schemaPath: '/path/to/schema.yml' // can also be schema.json
})

// validate against a JS schema object
const requiredSchema = {
  name: {
    first: { type: String, required: true },
    last: { type: String, required: true }
  },
  age: { type : Number }
}
schemaErrors = validateSchema(person, { schema: requiredSchema })

Compare two objects' schema

If you don't have a schema object built, but you just want to compare if structure of two objects is same, then you can use schemaObj option to pass the expected object:

let person = { name: { first: 'Tom', last: 'Xoman' }, age: 'something' }
let idealPerson = { name: { first: 'Tom', last: 'Xoman' }, age: 45 }
vaidateSchema(person, {    // compares the structure of person object against
  schemaObj: idealPerson   // anotherPerson object.
})

Schema validator validates the target file against the passed schema and lists down the mismatches in the structure:


schema-validator-listing-errors


It returns an array of errors showing the mismatches:

[{path: 'person.id', message: 'person.id must be a String'}]

Custom validators

Custom validators can be defined by passing an object with named validators to .use:

// custom validation function checking value for a regex
const checkHexColor = val => {
  return /^#[0-9a-fA-F]$/.test(val)
}

const car = new Schema({
  color: {
    type: String,
    use: { checkHexColor }
  }
})

Custom error messages

Define a custom error message for the validator:

car.message({
  checkHexColor: path => `${path} must be a valid hex color.`
})

Options

options parameter passed as the second argument in the validate schema method.

validateSchema(targetObject, options);

It has following configurable options available:

  • schema: javascript object having the schema structure (eg: {name: { type: String, required: true })
  • schemaPath: path to the json/yaml file having the schema
  • schemaObj: Used when you directly want to compare the target with a structure of another object. Pass the expected object in this option to verify the target object is having the similar structure
  • logLevel: specify the level of logging while validating schem. Possible values ['none', 'error', 'warn', 'verbose'] . By default the logLevel is set to error

Schema properties

  • type: field that can be boolean | string | number to define type of value of that property
  • required: field can be set to true if the property is required in target file
  • length : feild can be used for string values to check minimum and max length of string. example length: { min: 3, max: 32 }
  • use : an object of custom validation methods to be checked for a particular field. Each function in use object should take value param as input and return a boolean.

Schema File Examples

YAML Schema

---
person:
  name:
    first_name:
      type: string
  age:
    type: number
    required: true
  employeed:
    type: boolean
  hobbies:
  - type: string

JSON Schema

{
  "person": {
    "names": {
      "first_name": { "type": "string", "length": { "min": 3, "max": 32 } },
      "last_name": { "type": "string" }
    },
    "id": { "type": "string" },
    "age": { "type": "number", "required": true },
    "employeed": { "type": "boolean" },
    "hobbies": [{"type": "string"}],
    "attributes": [{ "foo": { "type": "string" } }]
  }
}

Command Line Interface


This package also can be used as a command line utility.

Command Usage

  • Basic syntax: schema [command] [options]
  • In root folder of your project, use command: schema validate -f path/to/dummy.yml -s path/to/schema.yml
  • for help about the options use command: schema validate -h

Command Options

  • -f, --filePath : [Required param] path to the target file for validating
  • -s, --schema [schemaPath] : path to an external schema file. If not passed the schema is fetched from /examples/schema.json which is the defeault schema location.
  • -o, --schema-obj [schemaObj] : stringified JSON object whose structure you want the target object to be compared with.
  • -t, --target [targetObj] : stringified JSON object whose structure is to be verified
  • -j, --json : Passed if target file is in JSON format
  • -e, --exit-on-err : Exit process with nonzero status on errors or warnings

Command Alias

  • You can also make alias for the command in your package.json. Just add the script key:
"scripts": {
  "sc": "schema"
},

and then try sc validate -f path/to/dummy.yml -s path/to/schema.yml

schema-validator's People

Contributors

dependabot[bot] avatar ketansaxena avatar maritaria avatar rishankj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

schema-validator's Issues

multi level yml check

Required not working correctly on multi level file. For example
I define schema as:

person: 
   name:
      type: string
      required: true
   dob:
      type: string
car:
   name:
      type: string
      required: true

and my yml under test can be:

car:
    name: 'BMW'

So it should not complain person.name is required.
Currently it complain that person.name is missing. It should only complain if upper level object exist eg

car:
    name: 'BMW'
person:
   dob: 24-10

Logging should be optional

I have a request, can we have the logging as optional or add an optional level lets say, verbose, warn and error.

It would help reduce the amount of logs on the terminal.

`Schema` - ReferenceError: Schema is not defined

Thank you for writing this, I find your library to be super helpful!

ISSUE
Upon execution of your example, I get the following error:

ReferenceError: Schema is not defined

I was not able to locate your definition/import of Schema in your code.

CODE

// custom validation function checking value for a regex
const checkHexColor = val => {
  return /^#[0-9a-fA-F]$/.test(val)
}

const car = new Schema({
  color: {
    type: String,
    use: { checkHexColor }
  }
})

mocha --opts not working anymore

As soon as I include "yaml-schema-validator": "^1.1.9" in my package.json and npm install it, mocha --opts mocha.opts fails with this error message:

$ mocha --opts mocha.opts
error: unknown option `--opts'

My project has these dependencies:

"devDependencies": {
    "chai": "^4.2.0",
    "chai-as-promised": "^7.1.1",
    "eslint": "^6.5.1",
    "eslint-config-airbnb-base": "^14.0.0",
    "eslint-plugin-import": "^2.14.0",
    "fast-xml-parser": "3.13.0",
    "mocha": "^6.2.1",
    "nyc": "^14.1.1",
    "yaml-schema-validator": "^1.1.9"
  },
  "dependencies": {
    "app-root-path": "^2.2.1",
    "bluebird": "^3.7.0",
    "config": "^3.2.3",
    "glob": "^7.1.4",
    "js-yaml": "^3.13.1",
    "json5": "^2.1.1",
    "jszip": "^3.2.2",
    "lodash": "^4.17.11",
    "moment": "^2.22.2",
    "moment-range": "^4.0.1",
    "moment-timezone": "^0.5.23",
    "normalize-path": "^3.0.0",
    "ramda": "^0.26.1",
    "sql.js": "^1.0.0",
    "winston": "^3.2.1",
    "yargs": "^14.2.0"
  }

If I remove yaml-schema-validator, the command mocha --opts mocha.opts runs all tests as expected.

The problematic behavior is observed regardless of platform, it's the same on Linux, Windows, or macOS. Node.js version 10.16.3 was used.

Unable to run `schema` on MacOS due to wrong line endings

The schema bin file uses windows style line endings (\r\n) and this breaks attempts to use the package on non-windows systems.
The edge case here is that npm performs conversion of \r\n to \n for "bins" files, but yarn does not.

Validate Regex?

Hi,

I want to validate whether the given yaml parameter matches a certain regex. Is this possible somehow?

Example:

IP:
  type: string
  required: true
  regex: ^\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b$

CLI: return a nonzero status when validation fails

This tool is great, but so far int merely prints errors and warnings, which makes it difficult to evaluate the validation status e.g. by a CI script or pre-commit hook.

A naïve implementation might just evaluate the list of errors and warnings and exit the process with a non-zero status:

    const errorsAndWarnings = validateSchema(targetObj, options)
    if (errorsAndWarnings?.length > 0) {
      process.exit(1)
    }

Wildcard parent

Hi great module. Thanks for this one.

I have a need to check child key/values under parents with wildcards.

Example:

foo:
  bar:
    color: blue
  baz:
    color: red

Where color needs to be checked but it's immediate parent is not always known: foo.*.color. Anyway to accomplish this?

Using with yargs, it overrides options

Compatibility issue with yargs package. When i try to use both while creating my owm cmd tool, options for schema-validator overrides my yargs options

how to validate array of elements?

services:
- name: a name
  ports:
    - port: 81
      service: web
    - port: 10101
      service: api

- name: another name
  ports:
    - port: 2801
      name: api
    - port: 2802
      name: mysql
    - port: 31975
      service: nodePort/public api
    - port: 31976
      service: nodePort/internal api

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.