Giter VIP home page Giter VIP logo

js-oop-project-lvl1's Introduction

Hexlet tests and linter status:

Actions Status .github/workflows/check.yml

Validator

The library provide set of methods to validate data with ability to write your own validators. It can be used anywhere: backend, frontend - no matter.

The purpose of this project is to understand how to work with OOP in JavaScript

You can find more information here

Usage

To start work with library you need to create a validator:

const validator = new Validator();

const schema = schema.string();

schema.isValid('string'); // true

Every validator has isValid method

String schema

Provide 3 methods to validate strings:

  • string - creates a schema
  • required
  • contains
const schema = validator.string();

schema.isValid(''); // true

schema.required();

schema.isValid('what does the fox say'); // true
schema.isValid('hexlet'); // true
schema.isValid(null); // false
schema.isValid(''); // false

schema.contains('what').isValid('what does the fox say'); // true
schema.contains('whatthe').isValid('what does the fox say'); // false

Number schema

Provide 4 methods to validate strings:

  • number - creates a schema
  • required
  • positive
  • range
const schema = validator.number();

schema.isValid(null); // true

schema.required();

schema.isValid(null); // false
schema.isValid(7); // true

schema.positive().isValid(10); // true

schema.range(-5, 5);

schema.isValid(-3); // false
schema.isValid(5); // true

Array schema

Provide 3 methods to validate strings:

  • array - creates a schema
  • required
  • sizeof
const schema = validator.array();

schema.isValid(null); // false

const schema = schema.required();

schema.isValid([]); // true
schema.isValid(['hexlet']); // true

schema.sizeof(2);

schema.isValid(['hexlet']); // false
schema.isValid(['hexlet', 'code-basics']); // true

Object schema

Provide 2 methods to validate strings:

  • object - creates a schema
  • shape - allows to set nested schemas for every object property
const schema = validator.object();

schema.shape({
  name: validator.string().required(),
  age: validator.number().positive(),
});

schema.isValid({ name: 'kolya', age: 100 }); // true
schema.isValid({ name: 'maya', age: null }); // true
schema.isValid({ name: '', age: null }); // false
schema.isValid({ name: 'ada', age: -5 }); // false

Custom validators

You can write your own validators for any schema using addValidator method

const validator = new Validator();

const validateStartsWith = (value, start) => value.startsWith(start);
validator.addValidator('string', 'startWith', validateStartsWith);

const schema = validator.string().test('startWith', 'H');
schema.isValid('exlet'); // false
schema.isValid('Hexlet'); // true

const validateMin = (value, min) => value >= min;
validator.addValidator('number', 'min', validateMin);

const schema = validator.number().test('min', 5);
schema.isValid(4); // false
schema.isValid(6); // true

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.