Schema validation utilities for h3, using typebox & ajv

Overview

h3-typebox

npm version npm downloads Github Actions Codecov

JSON schema validation for h3, using typebox & ajv.

Install

# Using npm
npm install h3-typebox

# Using yarn
yarn install h3-typebox

# Using pnpm
pnpm install h3-typebox

Usage

import { createServer } from 'http'
import { createApp } from 'h3'
import { useValidatedBody, useValidatedQuery, Type } from 'h3-typebox'

const app = createApp()
app.use('/', async (req) => {
  // Validate body
  const body = await useValidatedBody(req, Type.Object({
    optional: Type.Optional(Type.String()),
    required: Type.Boolean(),
  }))

  // Validate query
  const query = useValidatedQuery(req, Type.Object({
    required: Type.String(),
  }))
})

createServer(app).listen(process.env.PORT || 3000)

See how to define your schema with Type on TypeBox documentation.

Development 💻

  • Clone this repository
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Made with 💙

Published under MIT License.

You might also like...

String validation

validator.js A library of string validators and sanitizers. Strings only This library validates and sanitizes strings only. If you're not sure if your

Jan 5, 2023

Lightweight JavaScript form validation library inspired by CodeIgniter.

validate.js validate.js is a lightweight JavaScript form validation library inspired by CodeIgniter. Features Validate form fields from over a dozen r

Dec 15, 2022

Cross Browser HTML5 Form Validation.

Validatr Cross Browser HTML5 Form Validation. Getting Started View the documentation to learn how to use Validatr. Changelog Version 0.5.1 - 2013-03-1

Nov 1, 2022

jQuery Validation Plugin library sources

jQuery Validation Plugin - Form validation made easy The jQuery Validation Plugin provides drop-in validation for your existing forms, while making al

Jan 3, 2023

jQuery form validation plugin

jQuery form validation plugin

jQuery.validationEngine v3.1.0 Looking for official contributors This project has now been going on for more than 7 years, right now I only maintain t

Dec 23, 2022

The most powerful data validation library for JS

joi The most powerful schema description language and data validator for JavaScript. Installation npm install joi Visit the joi.dev Developer Portal f

Jan 4, 2023

:white_check_mark: Easy property validation for JavaScript, Node and Express.

property-validator ✅ Easy property validation for JavaScript, Node and Express Built on top of validator.js, property-validator makes validating reque

Dec 14, 2022

Lightweight and powerfull library for declarative form validation

Formurai is a lightweight and powerfull library for declarative form validation Features Setup Usage Options Methods Rules Examples Roadmap Features ?

May 13, 2022

A simple credit cards validation library in JavaScript

creditcard.js A simple credit cards validation library in JavaScript. Project website: https://contaazul.github.io/creditcard.js Install creditcard.js

Jan 7, 2023
Comments
  • feat: extended ajv formats

    feat: extended ajv formats

    • Added useValidatorOptions type for the user to define extra options
    • Added the option includeAjvFormats to include extended ajv formats, also implemented by typebox 0.25+
    • Added tests for body and query validator to test extended formats

    The current behavior of throwing a 500 when extended schema option was just kept and added to the tests. We should evaluate error handling for this in the future. strictNullChecks was added to tsconfig to properly generate dynamic type checks in VSCode, as warned. As a .eslintrc was already existend, I also added the vscode configuration that implements auto format on save for this eslint config and the minimal possible options for the ts formatter to avoid conflicts.

    opened by itpropro 4
  • added better error output via @apideck/better-ajv-errors

    added better error output via @apideck/better-ajv-errors

    Currently the error message is not helpful in certain cases (e.g. minLength validation).

    Might also be worthwhile to change response to include not only the first error message.

    opened by keslol 4
  • Verify for only allowed fields

    Verify for only allowed fields

    Hey @kevinmarrec, great h3 addition, I had no problems with using it so far. As I am pretty new to TypeBox, I wanted to ask, what the best way would be to make sure that only the provided fields are actually on the request? From a security perspective, it is discussable, but sometimes you want to have paramA, paramB and paramC as query parameter and used validateQuery to define them as string (what query strings always are). If a user now queries the url with paramX, it will not throw an error, as the field will not be verified by typebox. Is there any way to handle a catch all for all parameters/body entries that are not part of the schema or is this something you would implement in nitro natively?

    opened by itpropro 2
  • Fix failing builds with current nitro version

    Fix failing builds with current nitro version

    A current nitro project cannot be compiled because of the dependency to h3 0.7.21. h3 had breaking chages with 0.8.0, which leads to compilation errors:

    import { eventHandler, setHeaders, sendRedirect, defineEventHandler, handleCacheHeaders, createEvent, getRequestHeader, createError, createApp, createRouter as createRouter$1, lazyEventHandler, toNodeListener } from 'h3';
                                                                                                                                                                                                       ^^^^^^^^^^^^^^
    SyntaxError: The requested module 'h3' does not provide an export named 'toNodeListener'
    

    We should at least update the dependency to 0.8.0 and continuously update the h3 dependency until it reaches 1.0.0 in CI/CD. Maybe add renovate to stay up to date?

    PS: @kevinmarrec can you add the hacktoberfest label to the repo? Contributions can then be added to the contributors hacktoberfest score :)

    opened by itpropro 0
Releases(v0.6.0)
Owner
Kevin Marrec
Full-stack Node.js & Vue.js Developer Works at @nuxtlabs on @nuxt things
Kevin Marrec
Dead simple Object schema validation

Yup Yup is a JavaScript schema builder for value parsing and validation. Define a schema, transform a value to match, validate the shape of an existin

Jason Quense 19.2k Jan 2, 2023
Schema-Inspector is an JSON API sanitisation and validation module.

Schema-Inspector is a powerful tool to sanitize and validate JS objects. It's designed to work both client-side and server-side and to be scalable wit

null 494 Oct 3, 2022
TypeScript-first schema validation for h3 and Nuxt applications

h3-zod Validate h3 and Nuxt 3 requests using zod schema's. Install npm install h3-zod Usage import { createServer } from 'http' import { createApp } f

Robert Soriano 48 Dec 28, 2022
FieldVal - multipurpose validation library. Supports both sync and async validation.

FieldVal-JS The FieldVal-JS library allows you to easily validate data and provide readable and structured error reports. Documentation and Examples D

null 137 Sep 24, 2022
Tiny Validator for JSON Schema v4

Tiny Validator (for v4 JSON Schema) Use json-schema draft v4 to validate simple values and complex objects using a rich validation vocabulary (example

Geraint 1.2k Dec 21, 2022
Validate for XML schema and returns all the possible failures

detailed-xml-validator Validate for XML schema and returns all the possible failures Sample Rules file <?xml version = "1.0"?> <students nillable="fa

Natural Intelligence 11 Dec 20, 2022
Validate graphql operations against a schema

@graphql-validate With the power of GraphQL-Tools and GraphQL-JS, we are able to provide a smooth experience for validation your GraphQL operations du

Saihajpreet Singh 13 Dec 23, 2022
Convert JSON examples into JSON schema (supports Swagger 2, OpenAPI 3 and 3.1)

json-to-json-schema Convert JSON examples into JSON schema. Supports JSON Schema draft-05 used in Swagger 2.0 and OpenAPI 3.0 and new draft draft-2020

Redocly 9 Sep 15, 2022
jQuery Validation Plugin library sources

jQuery Validation Plugin - Form validation made easy The jQuery Validation Plugin provides drop-in validation for your existing forms, while making al

null 10.3k Jan 3, 2023