truefieng / restless Goto Github PK
View Code? Open in Web Editor NEWExpress.js api, type safe validations and more
License: The Unlicense
Express.js api, type safe validations and more
License: The Unlicense
I just ran into this library, while looking for a library to do data sanitization for one of my apps. It looks very promising. I just have one question though. I noticed that each "as" function has a second "path" argument. What exactly is this argument? Or, what do I pass in to it? The docs in the README do not really go over the arguments.
Thanks.
asAnyOf
takes 2 or more sanitizers and returns a sanitizer that goes through them and returns the first that succeeds.
const asLiteral = <T extends string> (literal: T) =>
asChecked(asString, value => value === literal) as Sanitizer<T>
In my personal project I have this usage of restless solutions:
my point of Route, method post:
route.post('/login', asyncHandler( sanitize({
body: asObject({
username: asString,
password: asString,
})
}),
(reqData) => {
const {username, password} = reqData.body;
findUser('user', username)
.then((user: User) => {
if(user.password === password) {
responseOf(user, 200)
}
else {
responseOf('user not found', 400)
}
})
.catch(err => {
responseOf('internal server error', 500)
})
}
));
TypeScript compiler error:
Type void is not assignable to type ResponseFunction | Promise<ResponseFunction>
When I change code to marked returns type like belowe
... function(reqData): ResponseFunction | Promise<ResponseFunction> { ...
I have another ts compiler warrning
Does anyone have any idea how to solve this issue?
It would be nice to have short circuiting for responses. Current implementation idea is to throw a custom class.
It's very common to expect integers in URL path, e.g. /users/123
to access user by Id.
With using asNumber
sanitizer, request /users/3.14
would be still valid while not being a correct Id.
It seems it is not yet possible to use an async sanitizer
function inside cast
, do i mistake ?
If not, would it be possible to add support for this ?
import { Result } from '@restless/sanitizers';
export const asCertificate = async (value, path: string) => {
const errors = (await validate(value)) as any[];
if (Object.keys(errors).length) {
const resultError = flatten(
Object.values(errors).map((error) => {
return error.map((error) => ({
path: `${path}${error.path}`,
expected: error.expected || '',
}));
})
);
console.log(resultError);
return Result.error(resultError);
}
return Result.ok(value as Certificate);
};
import {cast} from '@restless/sanitizers';
import {asCertificate} from './somewhere';
const certificate = {};
const value = cast(certificate, asCertificate);
This is a list of all "nice-to-have" features for Restless:
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.