0.7.0 • Published 3 years ago

one-schema v0.7.0

Weekly downloads
25
License
MIT
Repository
github
Last release
3 years ago

one-schema

Install

yarn add one-schema@latest

How to use

const {ons} = require('one-schema');

const userSchema = ons().object({
  _id: ons().int32().min(0),
  name: ons().string().max(48).required(),
  email: ons().string().validator("email").required(),
  bio: ons().string().min(0).max(128).defaultValue("This is my bio"),
  image_url: ons().string().min(10).max(1024).allowNull(),
  location: ons().object({
    city: ons().string(),
    country: ons().string(),
  }),
  pets: ons().arrayOf(
    ons.object({
      animal: ons().string().oneOf(["cat","dog","fish","frog"]).required(),
      color: ons().string().max(12),
      age: ons().int32().min(0).max(500),
    })
  ),
});

const sampleUser1 = {
  _id: 1,
  name: "Sam",
  email: "sam@email.com",
  location: {
    country: "Germany",
  },
  pets: [
    {
      animal: "cat",
      color: "black",
      age: 8
    },
    {
      animal: "fish",
      color: "orange",
      age: 2
    },
  ]
};

const sampleUser2 = {
  _id: 2,
  email: "BADemail.com",
  location: {
    inmyhouse: "yup",
  },
  pets: [
    {
      animal: "cheetah",
      color: "black",
      age: 1337
    },
  ]
};

function printResult(name, result) {
  if(result.success) {
    console.log(`Validation for ${name} passed successfully, object is valid.`);
  }else{
    console.error(`Validation for ${name} FAILED: ${result.error}!`);
  }
}
                                                             

//line below prints: 'Validation for User 1 Passed successfully, object is valid.'
printResult("User 1", ons.validate(sampleUser1, userSchema)); 

//line below prints: 'Validation for User 1 (JSON) Passed successfully, object is valid.'
printResult("User 1 (JSON)", ons.validate(sampleUser1, JSON.parse(JSON.stringify(userSchema)))); 


//line below prints: 'Validation for User 2 FAILED: name -> is undefined, but required!!'
printResult("User 2", ons.validate(sampleUser2, userSchema)); 

//line below prints: 'Validation for User 2 (JSON) FAILED: name -> is undefined, but required!!'
printResult("User 2 (JSON)", ons.validate(sampleUser2, JSON.parse(JSON.stringify(userSchema)))); 

API

Table of Contents

License

MIT © Tiancai

0.7.0

3 years ago

0.6.0

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.3.9

4 years ago

0.4.0

4 years ago

0.3.8

4 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.0.4

5 years ago