valid-sift

Check if something is a valid sift filter

Usage no npm install needed!

<script type="module">
  import validSift from 'https://cdn.skypack.dev/valid-sift';
</script>

README

valid-sift Build Status Coverage Status

Check if something is a valid sift filter.

NPM

Install

$ npm install --save valid-sift

Usage

Importing

import isValid from 'valid-sift';

or

var isValid = require('valid-sift');

Checking a sift filter

If the filter is valid the function returns true.

const siftFilter = {
  name: {
    $or: [{$eq: 'Max'}, {$eq: 'Moritz', age: 13}]
  }
};

const allowedAttributes = ['name', 'age'];

valid(siftFilter, ...allowedAttributes); // => true

If you pass a filter with a not allowed attribute it returns false.

const siftFilter = {
  id: {
    $or: [{$eq: 12}, {$eq: 13}]
  }
};
valid(siftFilter, ...allowedAttributes); // => false

If you pass a filter with an invalid operator it returns false.

const siftFilter = {
  id: {
    $or: [{$eq: 12}, {$nope: 13}]
  }
};
valid(siftFilter); // => false

API

isValid(filter, [attribute, ...]) => boolean

  • filter - the filter to check.
  • attribute - Allowed attributes in the filter.

or pass the attributes as an array.

isValid(filter, attributes) => boolean

  • attributes - Allowed attributes in the filter as an array.

License

MIT © Stoeffel