2.1.1 • Published 9 months ago

joi-country-state v2.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

joi-country-state

A Joi extension that provides easy validation for country and state fields.

Installation

npm install joi-country-state

Usage

import BaseJoi from "joi";
import countryStateValidator from "joi-country-state";

const Joi = BaseJoi.extend(countryStateValidator);

// Country Validation
const countrySchema = Joi.object({
  country: Joi.string().country().required(),
});
countrySchema.validate({ country: "IN" });

// State Validation
const stateSchema = Joi.object({
  state: Joi.string().state("IN").required(),
});
stateSchema.validate({ state: "TN" });

// Country & State Validation
const schema = Joi.object({
  country: Joi.string().country().required(),
  state: Joi.string().state(Joi.ref("country")).required(),
});
schema.validate({ country: "IN", state: "TN" });

// Country & State with Custom Data
const countryStateConfig = {
  IN: ["DL", "MH", "KA", "TN"],
  US: ["CA", "TX", "NY", "CO"],
};
const customSchema = Joi.object({
  country: Joi.string()
    .country()
    .valid(...Object.keys(countryStateConfig))
    .required(),
  state: Joi.string().state(Joi.ref("country"), countryStateConfig).required(),
});
customSchema.validate({ country: "IN", state: "TN" });
2.1.1

9 months ago

2.1.0

9 months ago

1.6.2

9 months ago

1.6.1

9 months ago

1.6.0

9 months ago

1.5.0

10 months ago

1.4.0

10 months ago

1.3.0

10 months ago

1.2.0

10 months ago

1.1.0

10 months ago

1.0.0

10 months ago