17.12.1 • Published 3 months ago
json-joi-converter v17.12.1
Json Joi
Json-Joi is a powerfull TypeScript module that builds on top of Joi module. Converts Joi objects to json and vice versa, makes Joi objects from json.
From Joi description: "The most powerful schema description language and data validator for JavaScript."
See Joi API for documentation and api.
Json-Joi supports almost entire Joi API!
Installation
npm install json-joi-converter
Usage
import Joi, {fromJson, toJson, Schema} from 'json-joi-converter';
const json: Schema = {
type: "object",
properties: {
a: {
type: "number",
min: 100,
max: 1000,
required: true
}
}
};
// fromJson(json) is equal to following
const joi = Joi.object({
a: Joi.number().min(100).max(1000).required()
});
// VALID
assert.deepEqual(toJson(joi), toJson(fromJson(json)));
Joi Reference & Functions
{
a: {
type: "number"
},
b: {
type: "number",
min: {
$ref: "a",
adjust: "value => value + 1"
}
}
}
// where adjust is a stringed function
// is equal to
Joi.object({
a: Joi.number(),
b: Joi.number.min(Joi.ref("a", {
adjust: value => value + 1
}))
})
Joi RegExp
let json: ObjectSchema = {
type: "object",
properties: {
a: {
type: "string",
pattern: "/a/"
},
b: {
type: "string",
regex: {$regex: "/a/", flags: "i"}
},
c: {
type: "string",
regex: {pattern: {$regex: "/a/"}}
}
}
};
const converted = (toJson(fromJson(json)) as any);
assert.equal(converted.properties?.a?.pattern?.regex?.$regex, '\\/a\\/');
assert.equal(converted.properties?.b?.pattern?.regex?.$regex, '\\/a\\/');
assert.equal(converted.properties?.b?.pattern?.regex?.flags, 'i');
assert.equal(converted.properties?.c?.pattern?.regex?.$regex, '\\/a\\/');
Joi Replace
{
type: "string",
replace: {find: {$regex: "a", flags: "gi"}, replace: "b"}
}
// is equal to
Joi.string().replace(/a/gi, "b")
{
type: "string",
replace: [
{find: {$regex: "a", flags: "gi"}, replace: "b"},
{find: "a", replace: "b"}
]
}
// is equal to
Joi.string().replace(/a/gi, "b").replace("a", "b")
17.12.1
3 months ago
17.12.0
3 months ago
17.11.1
3 months ago
17.11.0
6 months ago
17.2.1-h
1 year ago
17.2.1-g
2 years ago
17.2.1-f
3 years ago
17.2.1-e
3 years ago
17.2.1-d
3 years ago
17.2.1-c
3 years ago
17.2.1-a
3 years ago
17.2.1
4 years ago
17.1.1-g
4 years ago
17.1.1-h
4 years ago
17.1.1-e
4 years ago
17.1.1-f
4 years ago
17.1.1-d
4 years ago
17.1.1-c
4 years ago
17.1.1-beta
4 years ago
17.1.4
4 years ago
17.1.2
4 years ago
17.1.3
4 years ago
17.1.1
4 years ago
17.1.0
4 years ago