npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

validasi

v1.0.8

Published

Library validasi objek javascript bahasa Indonesia

Downloads

20

Readme

validasi

Library/package untuk memvalidasi objek javascript, mudah, ringan dan sederhana.

Installation

  npm i validasi

Penggunaan umum

Contoh lolos validasi

import CheckValue from "validasi";

const schema = {
  nama: CheckValue.string().alpha().max(25),
  //Jika nomor ingin disimpan dengan tipe "string" (Gunakan method string())
  noKTP: CheckValue.string().number(),
  email: CheckValue.string().email(),
  password: CheckValue.string().min(8),
};

const obj = {
  nama: "Budi Susanto",
  noKTP: "332966660046464997",
  email: "[email protected]",
  password: "ET125tenggorokan",
};

const error = new CheckValue().object(schema).validate(obj);
console.log(error);

// hasil
// { nama: '', noKTP: '', email: '', password: '' }

Contoh gagal validasi (result objek)

import CheckValue from "validasi";

const schema = {
  id: CheckValue.string().uuid(),
  lulus: CheckValue.boolean().truthy(),
};

const obj = {
  id: "WizPCaln6Q",
  lulus: false,
};

const error = new CheckValue().object(schema).validate(obj);
console.log(error);

// hasil
// { id: 'id must be a valid UUID', lulus: 'lulus must be true' }

Contoh gagal validasi (result single string)

import CheckValue from "validasi";

const schema = {
  id: CheckValue.string().uuid(),
  lulus: CheckValue.boolean().truthy(),
};

const obj = {
  id: "WizPCaln6Q",
  lulus: true,
};

// Tambahkan {true} pada parameter kedua didalam method {validate(object)}
const error = new CheckValue().object(schema).validate(obj, true);
console.log(error);

// hasil
//  'id: must be a valid UUID'

Contoh kustom pesan error

import CheckValue from "validasi";

const schema = {
  startup: CheckValue.string().alpha(
    "Kolom startup hanya mendukung karakter alfabet."
  ),
  website: CheckValue.string().url("Harap masukan nama website dengan benar"),
};

const obj = {
  startup: "Admin Bengk3l",
  website: "adminbengkel.my.id", // harus diawali dengan http/https
};

const error = new CheckValue().object(schema).validate(obj);
console.log(error);

// {
//   startup: 'Kolom startup hanya mendukung karakter alfabet.',
//   website: 'Harap masukan nama website dengan benar'
// }

Contoh checkEnum method

enum enumExample {
  NUMBER = 0,
  STRING = "string",
}
const customMessage = "invalid type!";
const enumSchema = {
  number: CheckValue.enumCheck(enumExample, { message: customMessage }),
  string: CheckValue.enumCheck(enumExample, { message: customMessage }),
};

const _check = new CheckValue()
  .object(enumSchema)
  .validate({ number: 0, string: "string" }, true);

console.log(_check);

// 'invalid type!'

Contoh kustom method

interface IResult {
  message?: string;
  result: boolean;
}
type IMethod = (value: any, value2?: number) => IResult;

// Method custom tersedia untuk schema string, boolean, dan number
custom(method: IMethod, option: {value2?: number; message?: string;}): this;

Contoh dibawah menggunakan typescript, untuk native kamu bisa menyesuaikannya sendiri

import CheckValue, { IMethod } from "validasi";

const bilanganGanjil: IMethod = (val: any) => {
  if (val % 2 !== 0) {
    return { result: false, message: "" };
  }
  return { result: true, message: "harus bilangan ganjil" };
};

const schema = {
  angka: CheckValue.custom(bilanganGanjil, {}),
};

const obj = {
  angka: 4,
};

// gunakan typeof schema agar key dapat diekstrak langsung ex: "error.angka"
const error = new CheckValue<typeof schema>().object(schema).validate(obj);
console.log(error);

// hasil
// { angka: 'angka harus bilangan ganjil' }

TERIMAKASIH SUDAH MAU MENCOBA validasi

Mohon saran dan masukannya