cheatcode / joystick

A full-stack JavaScript framework for building stable, easy-to-maintain apps and websites.
https://cheatcode.co/joystick
Other
209 stars 11 forks source link

Consider a typecheck library for managing input types and error handling #290

Closed rglover closed 2 months ago

rglover commented 1 year ago

Queue the pitchforks and "why not TypeScript?!"

The idea is to get a dirt simple function that piggybacks on Joystick's existing input validation (same syntax, etc) but gives a standalone function for doing the check on an argument. Credit where it's due: this is a straight rip off of Meteor's check() function, just adapted to Joystick (and accessible to any JavaScript project).

Basic idea:

typecheck(varOne, { type: 'string', required: true });
typecheck(varTwo, { type: 'object', required: true });

As for why, I want to keep the "JavaScript everywhere" philosophy of Joystick intact while also avoiding messy configs and confusing code that other devs may or may not be able to understand. TypeScript introduces a lot of extra weight for something that can be replaced with the above while also keeping code pedestrian.

Goal would be for the function to take a variable and a validation object that describes the requirements and if that variables fails to pass validation, throws an error. It may be worth even considering an options object for the type check itself which lets you decide if the function throws or just returns true/false.