https://github.com/moongoal/happy-chappy
JSON object validation module for JavaScript and TypeScript
https://github.com/moongoal/happy-chappy
Last synced: about 2 months ago
JSON representation
JSON object validation module for JavaScript and TypeScript
- Host: GitHub
- URL: https://github.com/moongoal/happy-chappy
- Owner: moongoal
- License: isc
- Created: 2022-01-14T23:06:15.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-10-18T02:06:19.000Z (over 2 years ago)
- Last Synced: 2025-12-27T14:31:44.569Z (6 months ago)
- Language: TypeScript
- Size: 891 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# happy-chappy
Happy-chappy is a compact, dependency-free JSON object validator package for JavaScript and TypeScript.
## Usage
```typescript
import { Schema, createValidator, validate } from "happy-chappy";
const MY_OBJECT_SCHEMA: Schema = {
object: {
members: {
firstField: "number",
secondField: {
number: {},
optional: true
},
thirdField: "string",
fourthField: {
enum: [1, 2, 3, "four", 5]
}
}
}
};
const validateMyObject = createValidator(MY_OBJECT_SCHEMA);
const myObject = { firstField: 5, thirdField: "hey", fourthField: "four" };
validateMyObject(myObject) === true;
// Or alternatively
validate(myObject, MY_OBJECT_SCHEMA) === true;
```
Many more examples available in the test folder.
## Data types
Supported data types are:
* String
* Number
* Boolean
* Array
* Object
* Enumeration
Each of these can be further restricted using matcher functions or specific configuration.
### String options
Text string validation is supported using multiple types of matcher. A string can be matched by:
* An exact value
* A regular expression
* A matcher function
### Number options
Number validation options allow to restrict the available range as follows:
* Exact value matching (checked with the strict equality operator or matcher)
* Minimum threshold
* Maximum threshold
* Integer vs floating point
* Epsilon (custom floating point comparison constant)
### Array options
Arrays can be further scoped by setting the following:
* Exact length
* Minimum length
* Maximum length
* Matcher function
### Object options
Object specification can be customised by allowing extra memebrs not to be taken into account during validation or by specifying a matcher function.
### Enumerations
Enumerated values can be any string or number, matched with the strict equality operator.
## Matchers
Matchers allow more complex logic to be included in the validation model. For example:
```typescript
const isPersonWithAgeTuple = (v: any[]) => (
v.length === 2
&& typeof v[0] === "string"
&& typeof v[1] === "number"
&& Number.isInteger(v[1])
); // Enforces [Name: string, Age: integer]
const schema: Schema = {
array: { matcher: isPersonWithAgeTuple }
};
validate(["Dummy", 5], schema) === true;
validate([5, "Dummy"], schema) === false;
```
## Typing Aids
Version 2 brings in typing aids for objects and enumerations to streamline the schema ceration process. When you define a schema for an object or enumeration you can provide a type that will be used to assist in constraining object members and enumerated values.
```typescript
interface MyRequest {
a: number
b: string
}
const MY_REQUEST_SCHEMA: Schema = {
object: {
members: {
a: { ... },
b: { ... },
c: { ... }, // TypeScript error!
}
}
};
```
```typescript
enum MyEnum {
first = 1,
second = 2
}
const MY_REQUEST_SCHEMA: Schema = {
enum: [
MyEnum.first,
2,
3 // TypeScript error!
]
};
```
```typescript
type MyEnum = "first" | "second";
const MY_REQUEST_SCHEMA: Schema = {
enum: [
"first",
"second",
"third" // TypeScript error!
]
};
```
## Changelog
Read the changelog [here](./CHANGELOG.md).
## License
This package is licensed under the [ISC license](./LICENSE).
## Bugs and feedback
You can find the [repostiory for this package](https://github.com/moongoal/happy-chappy) on GitHub.