paperhive / fefe Goto Github PK
View Code? Open in Web Editor NEWValidate, sanitize and transform values with proper TypeScript types and zero dependencies.
License: MIT License
Validate, sanitize and transform values with proper TypeScript types and zero dependencies.
License: MIT License
Validation functions assume that the input is unknown
. Sometimes you know more about the input value (e.g., in envfefe it's { [k: string]: string}
. Allow to provide the type of input values.
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
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!
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 :)
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.
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.
Throwing errors (like fefe does currently) is not a pure functional approach and lacks error type safety. We could rewrite error handling by employing the Either<Error, Result>
type, e.g. like in https://dev.to/gcanti/getting-started-with-fp-ts-either-vs-validation-5eja.
Make this work with proper types:
const validatePerson = (value: unknown) => validate.object({ friend: validatePerson })(value)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.