1.2.0 • Published 4 years ago

@dotvirus/fjord v1.2.0

Weekly downloads
13
License
ISC
Repository
github
Last release
4 years ago

npm version

fjord

Install

npm i @dotvirus/fjord
```

```javascript
// Require
const fjord = require("@dotvirus/fjord");

// Import
import Fjord from "@dotvirus/fjord";

Basic usage

const fjord = new Fjord();

fjord
  .validate({ a: 2, b: 3 }, [
    // a is mandatory and needs to be an integer
    {
      key: "a",
      handler: fjord.integer()
    },
    // b is mandatory and needs to be an integer
    {
      key: "b",
      handler: fjord.integer()
    }
  ])
  .then(val => {
    console.log(val); // -> true
  });

fjord
  .validate({ a: 2, b: 3 }, [
    // a is mandatory and needs to be an integer
    {
      key: "a",
      handler: fjord.integer()
    },
    // b is mandatory and needs to be an integer
    {
      key: "b",
      handler: fjord.integer()
    },
    // c is optional, but if it exists it needs to be an integer
    {
      key: "c",
      handler: fjord.integer().optional()
    },
    // d too is optional, but if it exists it needs to be an integer
    // if it does not exist, it will be set to null (but will pass, because defaults skip any rules)
    {
      key: "d",
      handler: fjord
        .integer()
        .optional()
        .default(null)
    }
  ])
  .then(val => {
    console.log(val); // -> true
  });

Custom error messages

const fjord = new Fjord();

fjord
  .validate({ a: "str" }, [
    {
      key: "a",
      handler: fjord.integer("a must be an integer")
    }
  ])
  .then(val => {
    console.log(val); // -> "a must be an integer"
  });

Connect-style middleware

const fjord = new Fjord();

app.get(
  "/",
  fjord.connect([
    // Root will be request object
    {
      key: "query.name",
      handler: fjord
        .string()
        .min(2)
        .max(50)
    }
  ]),
  (req, res) => {
    res.send("OK");
  }
);

Koa-style middleware

const fjord = new Fjord();

app.get(
  "/",
  fjord.koa([
    // Root will be context object
    {
      key: "req.query.name",
      handler: fjord
        .string()
        .min(2)
        .max(50)
    }
  ]),
  ctx => {
    res.send("OK");
  }
);

Transform input

const fjord = new Fjord({
  transformBefore: async v => {
    if (typeof v == "string") return v.trim();
    return v;
  }
});

const obj = { name: " not trimmed string    " };

fjord
  .validate(obj, [
    {
      key: "name",
      handler: fjord.string()
    }
  ])
  .then(val => {
    console.log(val); // -> true
    console.log(obj.name); // -> "not trimmed string"
  });

Custom functions

const fjord = new Fjord();

// Example: Require all numbers to be even
fjord
  .validate({ a: 2, b: 3 }, [
    {
      key: "a",
      handler: fjord.integer().custom(async i => i % 2 == 0)
    },
    {
      key: "b",
      handler: fjord.integer().custom(async i => i % 2 == 0)
    }
  ])
  .then(val => {
    console.log(val); // -> false
  });
1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.4.0

4 years ago

0.2.5

4 years ago

0.2.3

4 years ago

0.2.4

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago