npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

roud

v0.1.2

Published

Roud is a Laravel-inspired validation library built for Remix powered by Zod

Downloads

6

Readme

Table of contents

Installation

Requirements

Roud inherits requirements from Zod itself:

  • Typescript 4.1+

  • Strict mode activated

    // tsconfig.json
    {
    	// ...
    	"compilerOptions": {
    		// ...
    		"strict": true
    	}
    }

Node

Zod is a peer dependency and must be installed alongside Roud.

npm install roud zod

Basic usage

Roud fully extends Zod and for the most parts can be used outside of Remix. For simplicity reasons the following example will show how it should be used within Remix action.

import { ActionArgs } from "@remix-run/server-runtime";
import { r } from "roud";

const action = async ({ request }: ActionArgs) => {
	const Schema = r.schema({
		title: r.string(),
		quantity: r.number().min(0).max(10),
		termsOfService: r.boolean().accepted(),
	});

	const formData = await Schema.parseRequestFormData(request);
	// Type ^
	// {
	//     title: string;
	//     quantity: number;
	//     termsOfService: boolean;
	// }
};

If you used Zod before, parseRequestFormData() acts the same as parse() and throws RoudError if validation fails. You can also use safeParseRequestFormData which instead of throwing will return either the data or RoudError.

Booleans

Accepts true, "yes", "on", "true" "1", 1, false, "no", "off", "false" "0", 0 and normalizes the value to either true or false.

Available validators

| Validator | Description | | ------------------------------------- | ----------------------------------------------------------- | | accepted() | Boolean must be true | | acceptedIf(callback: () => boolean) | Boolean must be true when callback evaluates to true | | declined() | Boolean must be false | | declinedIf(callback: () => boolean) | Boolean must be false when callback evaluates to true |

Example

const Schema = r.schema({
	termsOfService: r.boolean().accepted(),
});

Numbers

Accepts any value parsable by Javascript's Number() constructor and normalizes it to real number.

Available validators

| Validator | Description | | ----------------------------------------- | ------------------------------------------------------------------------- | | between(min: number, max: number) | Number must have a size between the given minimum and maximum (inclusive) | | digits(digits: number) | Number must have an exact length of value | | digitsBetween(min: number, max: number) | Number must have a length between the given mininum and maximum | | integer() | Number must be an integer | | max(number: number) | Number must be less than or equal to a maximum value | | maxDigits(digits: number) | Number must have a maximum length of value | | min(number: number) | Number must have a minimum value | | minDigits(digits: number) | Number must have a minimum length of value | | multipleOf(number: number) | Number must must be a multiple of value |

Example

const Schema = r.schema({
	quantity: r.number().integer().between(0, 10),
	price: r.number().min(0),
});

Strings

Accepts any string value.

Available validators

| Validator | Description | | ------------------------------------------ | ----------------------------------------------------------------- | | doesNotEndWith(string: string) | String must not end with a provided value | | doesNotStartWith(string: string) | String must not start with a provided value | | email() | String must be a valid email address | | endsWith(string: string) | String must end with a provided value | | ipAddress() | String must be a valid IPv4 address | | json() | String must be a valid JSON | | macAddress() | String must be a valid MAC address | | notRegex(regExp: RegExp) | String must not match the given regular expression | | onlyLetters() | String must only contain letters | | onlyLettersAndNumbers() | String must only contain letters and numbers | | onlyLettersNumbersAndDashes() | String must only contain letters, numbers and dashes | | onlyLettersNumbersDashesAndUnderscores() | String must only contain letters, numbers, dashes and underscores | | regex(regExp: RegExp) | String must match the given regular expression | | startsWith(string: string) | String must start with a provided value | | timeZone() | String must be a valid time zone | | url() | String must be a valid URL | | uuid() | String must be a valid UUID |

Example

const Schema = r.schema({
	title: r.string().onlyLetters(),
	username: r.string().onlyLettersNumbersDashesAndUnderscores(),
});

Errors

Roud extends the default ZodError which simplifies passing errors back to the form. Here is an basic example:

import { ActionArgs } from "@remix-run/server-runtime";
import { r } from "roud";

const action = async ({ request }: ActionArgs) => {
	const Schema = r.schema({
		title: r.string(),
		quantity: r.number().min(0).max(10),
		termsOfService: r.boolean().accepted(),
	});

	const formDataResult = await Schema.safeParseRequestFormData(request);

	if (formDataResult.success) {
		return json({ formData: formDataResult.data });
	}

	return json({ formDataErrors: formDataResult.error.getValidationErrors() });
	// Type ^
	// {
	//     title: ["The title must be a string."],
	//     quantity: [
	//         "The title must be a number.",
	//         "The quantity must be at least 0.",
	//         "The quantity must not be greater than 10.",
	//     ],
	//     termsOfService: [
	//         "The termsOfService must be a boolean.",
	//         "The termsOfService must be accepted.",
	//     ],
	// }
};