Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bs-ajv

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bs-ajv

BucklesScript bindings to Ajv (Another JSON Validator)

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

Build Status Coverage Status

bs-ajv

BuckleScript bindings to Ajv (Another JSON Validator)

Why?

To be able to use standardized validation schemas across our JavaScript and BuckleScript based code.

How do I install?

  1. Add the bs-ajv package to your project via yarn/npm
yarn add bs-ajv
  1. Add bs-ajv to your bsconfig.json
{
  "dependencies": [ "bs-ajv" ]
}
  1. Enjoy!

Usage

The interface here is a bit rough, but it is usable and is used within production. Here are a few examples pulled from the Ajv docs.

Basic first example

let schema = Json.Encode.(object_([
  ("properties", object_([
    ("smaller", object_([
      ("type", string("number")),
      ("maximum", object_([
        ("$data", string("1/larger")),
      ])),
    ])),
    ("larger", object_([
      ("type", string("number")),
    ])),
  ])),
]));

let validData = Json.Encode.(object_([
  ("smaller", int(5)),
  ("larger", int(7)),
]));

AjvOptions.make()
|> AjvOptions.setData(_, Js.true_)
|> Ajv.ajv
|> Ajv.validate(`Schema(schema), validData)
/* This will return `Js.true_` */

Example with a self reference

let schema = Json.Encode.(object_([
  ("additionalProperties", object_([
    ("type", string("string")),
    ("format", object_([ ("$data", string("0#")) ]))
  ])),
]));

let validData = Json.Encode.(object_([
  ("date-time", string("1963-06-19T08:30:06.283185Z")),
  ("email", string("joe.bloggs@example.com")),
]));

AjvOptions.make()
|> AjvOptions.setData(_, Js.true_)
|> Ajv.ajv
|> Ajv.validate(`Schema(schema), validData)
/* This will return `Js.true_` */

Example using schema compilation and additional property filtering

let schema = Json.Encode.(object_([
  ("additionalProperties", bool(false)),
  ("properties", object_([
    ("foo", object_([
      ("type", string("number")),
    ])),
    ("bar", object_([
      ("baz", object_([
        ("type", string("string")),
      ])),
    ])),
  ])),
]));

let validData = Json.Encode.(object_([
  ("foo", int(0)),
  ("additional1", int(1)),
  ("bar", object_([
    ("baz", string("abc")),
    ("additional2", int(2)),
  ])),
]));

AjvOptions.make()
|> AjvOptions.removeAdditional(_, Js.true_)
|> Ajv.ajv
|> Ajv.compile(schema)
|> (fun
  | `Sync(fn) => fn
  | `Async(_) => failwith("unexpected_async_mode")
  )
|> (v) => validate(validData)
|> (fun
  | `Valid(_) => Js.true_
  | `Invalid(_) => Js.false_
  )
/* This will return `Js.true_`

Keywords

FAQs

Package last updated on 02 Apr 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc