You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

validata-mongo

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validata-mongo

MongoDB update `$set` operations

1.1.1
latest
Source
npmnpm
Version published
Weekly downloads
217
-2.69%
Maintainers
1
Weekly downloads
 
Created
Source

Validata Mongo

Type safe data validation and sanitization for MongoDB update $set operations using validata.

See validata for more details on validation functionality.

Getting started

npm i validata validata-mongo

Basic usage

Given interfaces

interface MyObject {
  required: string;
  optional?: string;
  child: Child;
}

interface Child {
  foo: number;
  bar: number;
}

And validata

const myCheck = isObjectSet<MyObject>({
  required: isString(),
  optional: maybeString(),
  child: isObjectSet({
    foo: isNumber({ min: 1 }),
    bar: asNumber({ coerceMax: 10 }),
  }),
});

Then

const result = check(myCheck, () => ({
  required: 'blue',
  child: {
    foo: 1,
    bar: 2,
  },
}));
console.log(JSON.stringify(result));
// -> SUCCESS -> {"required":"blue","child":{"foo":1,"bar":2}}

const result = check(myCheck, () => ({
  required: 'blue',
  optional: 'red',
  child: {
    foo: 20,
    bar: '20',
  },
}));
console.log(JSON.stringify(result));
// -> SUCCESS -> {"required":"blue","optional":"red","child":{"foo":20,"bar":10}}
// NOTE child.bar has been converted to a number and coerced to a max of 10

const result = check(myCheck, () => ({
  required: 'blue',
  'child.foo': 3,
}));
console.log(JSON.stringify(result));
// -> SUCCESS -> {"required":"blue","child.foo":3}

const result = check(myCheck, () => ({
  required: 'blue',
  'child.bar': '73',
}));
console.log(JSON.stringify(result));
// -> SUCCESS -> {"required":"blue","child.bar":10}
// NOTE child.bar has been converted to a number and coerced to a max of 10

check(myCheck, () => ({
  wow: 'blue',
}));
// -> ERROR -> {"issues":[{"path":["wow"],"value":"blue","reason":"unexpected-property"}],"name":"ValidationError"}

check(myCheck, () => ({
  required: 'blue',
  'child.unexpected': 17,
}));
// -> ERROR -> {"issues":[{"path":["child.unexpected"],"value":17,"reason":"unexpected-property"}],"name":"ValidationError"}

Keywords

typescript

FAQs

Package last updated on 04 Sep 2022

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