Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fefe

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fefe

Validate, sanitize and transform values with proper types.

  • 1.0.0-beta.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
49
decreased by-47.87%
Maintainers
1
Weekly downloads
 
Created
Source

fefe

npm version Build Status codecov

Validate, sanitize and transform values with proper types.

🔎 Validation: checks a value (example: check if value is string)
⚙ Sanitization: if a value is not valid, try to transform it (example: transform value to Date)
🛠️ Transformation: transforms a value (example: parse JSON)
🔌 Schemas are functions: easily extendable

🔎 Validation example

Validation only checks the provided value and returns it with proper types.

import { validate } from 'fefe'

const validatePerson = validate.object({ name: validate.string() })

// result is of type { name: string }
const person = validatePerson({ name: 'Leia' })

// throws FefeError because 'foo' is not a valid property
validatePerson({ foo: 'bar' })

You can also use fefe to define your types easily:

type Person = ReturnType<typeof validatePerson> // { name: string }

⚙️ Sanitization example

import { sanitize, validate } from 'fefe'

const sanitizeMovie = validate.object({
  title: validate.string(),
  releasedAt: sanitize.date()
})

// { title: string, releasedAt: Date }
type Movie = ReturnType<typeof sanitizeMovie>

const book: Book = sanitizeMovie({
  title: 'Star Wars',
  releasedAt: '1977-05-25T12:00:00.000Z'
})

Then book equals { title: 'Star Wars', releasedAt: Date(1977-05-25T12:00:00.000Z) } (releasedAt now is a date).

🛠️ Transformation example

This is an example that can be applied to parsing environment variables or query string parameters. Note how easy it is to apply a chain of functions to transform and validate a value (here we use ramda).

import { transform, validate } from 'fefe'
import { pipe } from 'ramda'

const parseConfig = validate.object({
  gcloudCredentials: pipe(
    validate.string(),
    transform.parseJson(),
    validate.object({ key: validate.string() })
  ),
  whitelist: pipe(validate.string(), str => str.split(','))
})

// { gcloudCredentials: { key: string }, whitelist: string[] }
type Config = ReturnType<typeof parseConfig>

const config: Config = parseConfig({
  gcloudCredentials: '{"key":"secret"}',
  whitelist: 'alice,bob'
})

Then config will equal { gcloudCredentials: { key: 'secret'}, whitelist: ['alice', 'bob'] }.

Note: you can use validations in transformations.

Keywords

FAQs

Package last updated on 22 Oct 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc