Skip to content

Quicksilver0218/Object-Validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

79f4ec0 · Apr 1, 2025

History

11 Commits
Mar 10, 2025
Apr 1, 2025
Apr 1, 2025
Mar 10, 2025
Nov 12, 2024
Jan 25, 2025
Nov 12, 2024
Jan 25, 2025
Nov 14, 2024
Feb 19, 2025

Repository files navigation

Object-Validator NuGet Maven npm

A library providing functions that let users validate the values in objects with YAML-formatted rules.

Installation

NuGet Gallery

Maven Central Repository

npm Public Registry

Usage

Document

JSON / YAML Rule List

First of all, a list of rules in JSON / YAML format should be provided. Here is the structure of the list:

type Root = {
    condition: Condition;
    id?: number;
    errorMessage?: string;
}[]

type Condition = {
    type: string;
    field?: string;
    arg?: string | null;
    args?: (string | null)[];
    conditions?: Condition[];
}

JSON Schema

For example:

[
    {
        "condition": {
            "type": "and",
            "field": "name",
            "conditions": [
                {
                    "type": "!null"
                },
                {
                    "type": "!blank"
                }
            ]
        },
        "id": 1,
        "errorMessage": "\"name\" is required."
    },
    {
        "condition": {
            "type": "range",
            "field": "age",
            "arg": "[18"
        },
        "id": 2,
        "errorMessage": "\"age\" should be >= 18."
    }
]

Program Code

C♯

using Quicksilver.ObjectValidator;

Validator validator = new(File.OpenText("path/validation-rules.json"));
ValidationResult validationResult = validator.Validate(myObject);

Java

import com.quicksilver.objectvalidator.Validator;
// ...
Validator validator = new Validator(getClass().getResource("/path/validation-rules.json"));
ValidationResult validationResult = validator.Validate(myObject);

JavaScript / TypeScript

import rules from "path/validation-rules.json";
import { Validator } from "@quicksilver0218/object-validator";

const validator = new Validator(rules);
const validationResult = validator.validate(myObject);

About

A library providing functions that let users validate the values in objects with YAML-formatted rules.

Resources

License

Stars

Watchers

Forks