1.3.11 • Published 3 years ago

basic-json-validator v1.3.11

Weekly downloads
520
License
ISC
Repository
github
Last release
3 years ago

codecov

Basic JSON validator

Description: basic-json-validator is a light weight JavaScript library for simple JSON validation.

Installation

npm i basic-json-validator

Usage

Possible schema validation configurations: 1. Simple Type validation 2. Custom Regex validation 3. Custom Callback validation

The same schema could have multiple validation types for the different keys.

This package offers few default test types to help with simple key validations. Those are: string, number, boolean, url, email

Simple type validation
const schema: ISchema = {
  name: {
    type: 'string',
    errorMessage: 'Name is required',
    isRequired: true
  }
  email: {
    type: 'email',
    errorMessage: 'Email is not valid',
    isRequired: true
  }
}

const JSONObj = {
  "name": "John Doe",
  "email": "mail@email.com"
}

validateSchema({
  jsonValue: JSON.stringify(JSONObj),
  schema: schema,
})
Custom regex validation
const schema: ISchema = {
  email: {
    errorMessage: 'Email does not match',
    regex: emailRegex,
  },
  url: {
    errorMessage: 'Url does not match',
    regex: urlRegex,
  }
}

validateSchema({
  jsonValue: JSON.stringify(JSONObj),
  schema: schema,
})
Custom callback validation
const schema: ISchema = {
  email: {
    customCallback: (email: string) => {
      return isEmail(email)
    },
    errorMessage: 'Email does not match',
  },
  url: {
    customCallback: (url: string) => {
      return isUrl(url) 
    },
    errorMessage: 'Url does not match',
  }
}

validateSchema({
  jsonValue: JSON.stringify(JSONObj),
  schema: schema,
})

Getting involved

Please, create issues if you have any problems using this library and feel free to open a pull request with suggestions.

Any contributions should be 100% covered by automated tests.

1.3.10

3 years ago

1.3.9

3 years ago

1.3.11

3 years ago

1.3.12

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.8

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.1.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.0

3 years ago