Socket
Socket
Sign inDemoInstall

safe-load

Package Overview
Dependencies
1
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    safe-load

Validate your payload, stay safe friends


Version published
Maintainers
1
Created

Readme

Source

safe-load

Validate your payload in js or typescript

Installation

npm i safe-load

Usage

It's very easy to use, you need 2 object:

  • payload: contains your payload
  • payloadSchema: contains your expect payload schema

Safe-Load is very usefull if you're using typescript !

Safe-Load validates your payload and lets typescript know that it is valid

import { validate } from "./safe_load";

type Payload = {
  data: {
    welcomingMessages: string[];
    level: number;
    nickname: string;
  };
  key: string;
  isABoolean: boolean;
};

const payload = {
  data: {
    welcomingMessages: ["hello world", "hello there"],
    level: 12,
    nickname: "ked"
  },
  key: "akey",
  isABoolean: true
};

const payloadSchema = {
  data: {
    welcomingMessages: ["string"],
    level: "number",
    nickname: "string"
  },
  key: "string",
  isABoolean: "boolean"
};

if (validate<Payload>(payload, payloadSchema)) {
  //your payload object is now considered typed as Payload by typescript !
}

Basic js example

const { validate } = require("safe-load");

const payload = {
  data: {
    welcomingMessage: "hello world",
    level: 12,
    nickname: "ked"
  }
};

const payloadSchema = {
  data: {
    welcomingMessage: "string",
    level: "number",
    nickname: "string"
  }
};

if(validate(payload, payloadSchema)){
  console.log("payload is valid")
}else{
  console.log("payload is not valid")
}

The result should be

Payload can be larger than the schema

Do you always use every field of your payload ? I personaly don't, that's why I chose to let you validate payloads that are bigger than your schema.

Example:

const { validate } = require("safe-load");

const payload = {
  data: {
    welcomingMessage: "hello world",
    level: 12,
    nickname: "ked",
    somethingNotUsefull: "this is really not important"
  }
};

const payloadSchema = {
  data: {
    welcomingMessage: "string",
    level: "number",
    nickname: "string"
  }
};

if(validate(payload, payloadSchema)){
  console.log("payload is valid")
}else{
  console.log("payload is not valid")
}

This will work because the object "payload" contains all the fields listed in the schema and they all got the correct type

Contribute !

Contributions are always welcomed !

Keywords

FAQs

Last updated on 05 Feb 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc