Git Product home page Git Product logo

joi's Introduction

joi

Object schema validation

Build Status Release

See: joi: Object schema validation in JavaScript

Dependencies

None!

Usage

Download

See JitPack

Simple example

// integer
Joi.integer().min(5).less(10).validate(7).assertValid();
// long integer
Joi.longInteger().min(3_000_000_000L).less(5_000_000_000L)
        .validate(4_000_000_000L)
        .assertValid();
// string
Joi.string().regex("hello.*").message("Not hello!").validate("hello!")
        .assertValid(IllegalArgumentException::new);
Joi.string().empty().validate("\n").assertValid();
Joi.string().email().validate("[email protected]").assertValid();
// object
Human alice = new Teacher("Alice", null, new String[]{"reading", "film"});
Schema<Human> humanSchema = Joi.<Human>object().type(Teacher.class)
        .field(Human::getName, Joi.string().regex("Alice").required().message("Wrong name!"))
        .field(Human::getAge, Joi.integer().min(8).max(18))
        .field(Human::getHobbies, hobbies -> hobbies.length >= 3
                ? Result.valid()
                : Result.of("Hobbies number to small!"));

Result result = humanSchema.validate(alice);
Assertions.assertTrue(result.isInvalid());
Assertions.assertEquals("[Hobbies number to small!]", String.valueOf(result.getMessages()));
// array
Joi.array().notEmpty().validate(new Object[]{0}).assertValid();
// list
Joi.<String>list().every(Joi.string().alphanum())
        .validate(Arrays.asList("1a", "2b", "3c"))
        .assertValid();
// map
Joi.<String, Integer>map().entry("key", Joi.integer().greater(7))
        .validate(Collections.singletonMap("key", 8))
        .assertValid();

Api

See code

Licence

WTFPL

joi's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

hiekay

joi's Issues

Update api

  • Update api, use org.jianzhao.joi.Result
  • Add AnySchema::assert api
  • Update document

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.