simonplend / creating-flexible-validation-rules

Creating flexible validation rules in Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Creating flexible validation rules in Node.js

Part of the Node Validation Essentials workshop series by Simon Plenderleith.

How to use this project

Run in your browser with StackBlitz

Note: Currently only works in Chrome, Edge or Brave browsers.

Set it up locally

  • git clone this repository
  • Run npm install
  • Run any of the examples below

Requires Node.js >= v14.13.0 (how to install).

Dependencies this project uses

Defined in package.json.

Examples

Validation with JSON Schema and Ajv

Run the code in example-ajv.js:

node example-ajv.js

JSON schema generation

Run the code in example-fluent-json-schema.js:

node example-fluent-json-schema.js

Ready for a challenge?

The iceCreamData object in example-ajv.js fails validation against the JSON schema we've defined (iceCreamSchema).

Challenge: Change the schema so the stock property allows for a value which is either an integer or null.

A couple of rules:

  • You can only use the Understanding JSON Schema page on the type keyword as a reference — it contains all the information you need for this challenge.
  • You can't change the data in the iceCreamData object.

When you run the script and see the message:

The ice cream data is valid! 🍨

...you've completed the challenge.

Good luck!

License

This project is licensed under the MIT License.

About

Creating flexible validation rules in Node.js

License:MIT License


Languages

Language:JavaScript 100.0%