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

fluent-schema-validator

v0.1.3

Published

Small Validator for Fluent JSON Schema

Downloads

19

Readme

forthebadge forthebadge forthebadge forthebadge

Fluent Schema Validator

Small validator for Fluent JSON Schema.

Don't let yourself handle validation, let the automation handle it.

Why

Fastify use Fluent JSON Schema for verifying schema of incoming request. Although, Fastify support basic validation by default, it's not enough for real world application.

For instance, having unintentionally field in the value but the schema bypass it, or having string format of email but also bypass by fastify default validator.

In fact, Fastify docs recommended that you use custom validator instead. This library help just that

Feature

  • Type strict to the schema
    • No unintentional field is bypassed
  • Support most of the API
    • Whether it's pattern for RegEx, string format, required field or an advances anyOf, allOf, not is supported.
  • Handle all type
    • Even if it's a simple string, complex array or full-fledge recursive object, it's all support.
  • It just work.
    • Edgecase like unicode email format is handled, recursive object is handled, etc.
    • No headache of setting up compiler, Fluent Schema Validator provide just a simple function which works out of the box
    • Full compatability with Fluent Schema, Fast JSON Stringify, Fastify Schema or plain object
    • As long as your schema is formatted to plain Fast JSON Stringify schema, it will work
  • Small and fast
    • No dependencies required, small bundle-size considering full-fledge feature.
  • CJS, ESM, MJS, TypeScript
    • Yes, it's all support, TypeScript first bundled by SWC.

Getting Start

Install the package.

npm install fluent-schema-validator fluent-json-schema

# or yarn
yarn add fluent-schema-validator fluent-json-schema

# or pnpm
pnpm add fluent-schema-validator fluent-json-schema

Then write a sample validation.

// Assumming you're using TypeScript or mjs
import S from 'fluent-json-schema'
import validate from 'fluent-schema-validator'

const schema = S.object()
    .prop('username', S.string().required())
    .prop('password', S.string().required())

const value = {
    username: "saltyaom",
    password: "12345678"
}

validate(value, schema) // true

Unintentional field

Field which is not define in schema will not be tolerate

import S from 'fluent-json-schema'
import validate from 'fluent-schema-validator'

const schema = S.object()
    .prop('username', S.string().required())
    .prop('password', S.string().required())

const value = {
    username: "saltyaom",
    password: "12345678",
    "not-in-schema": true
}

validate(value, schema) // false

Optional field

Field which is not mark required, will be fine if not presented

import S from 'fluent-json-schema'
import validate from 'fluent-schema-validator'

const schema = S.object()
    .prop('username', S.string().required())
    .prop('password', S.string())

const value = {
    username: "saltyaom",
}

validate(value, schema) // true

It would work the way you expected, see more in test cases or _specs_ folder.