package
Small Validator for Fluent JSON Schema
npm install fluent-schema-validatorGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
40.7 kB
Includes dependencies.
Small validator for Fluent JSON Schema.
Don't let yourself handle validation, let the automation handle it.
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
pattern for RegEx, string format, required field or an advances anyOf, allOf, not is supported.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-schemaThen 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) // trueField 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) // falseField 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) // trueIt would work the way you expected, see more in test cases or _specs_ folder.
