Giter Site home page Giter Site logo

paperhive / fefe Goto Github PK

View Code? Open in Web Editor NEW
38.0 4.0 1.0 570 KB

Validate, sanitize and transform values with proper TypeScript types and zero dependencies.

License: MIT License

TypeScript 99.40% JavaScript 0.60%
validate sanitize transform typescript schema functional parse sanitization validation parsing

fefe's People

Contributors

alanpearce avatar andrenarchy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

cuulee

fefe's Issues

Add default() helper

Similar to the optional() helper (see #6), it'd be great if we could also use a default() helper for generating object validation options like this one

  id: {
    validator: string(),
    default: () => generateUuid()
  }

via something like

id: default(string(), () => generateUuid())

However, default is a keyword in JS so we need to find another name for exporting such a helper. Any ideas? @andywer

Add optional() helper

Currently, our object description looks something like this:

  dateOfBirth: parseDate(),
  addressLine1: string(),
  addressLine2: {
    validator: string(),
    optional: true
  }

I think it would be really nice to have fefe come with a convenience function, so that we write it like this instead:

  dateOfBirth: parseDate(),
  addressLine1: string(),
  addressLine2: optional(string())

Of course it would be trivial for the user to write such a function themself, but it feels so basic, that it would probably make more sense to ship it as part of fefe.

Cheers!

Support metadata for schema documentation

A fefe function could return metadata about what it is doing, for example for

import { date } from 'fefe'
const validateReleaseDate = date({ min: new Date() })

the resulting function could offer a function validateReleaseDate.describe() that returns something like

{
  params: { min: Date(2019-02-26) },
  description: 'A date after 2019-02-26.'
}

The user could also provide a semantic description via

date({ min: new Date(), description: 'The release date.' })

or a function that can use the standard description

date({ min: new Date(), description: msg => `The release date: ${msg}` })

CC @andywer :)

Generate validator for type/interface

Given a type/interface, it would be great if fefe could construct a runtime validator for that type.

Example:

import { fromType } from 'fefe'

interface Person {
  name: string
  birthday?: Date
}

const validator = fromType(Person)

validator then should be of type (value: unknown) => Person and behave like

import { object, string, date } from 'fefe'
const explicitValidator = object({ 
  name: string(),
  birthdate: { validator: date(), optional: true }
})

which is tedious to type and error-prone.

This feature would make validating user input significantly easier, e.g. in REST APIs.

Rename 'enum' function

It would be great to rename enum function, to something else (e.g. oneOf, enumerate...), because 'enum' is typescript keyword, so it needs to be aliased everytime, when importing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.