Yet another piece of code that tries to validate json based on types, this is NOT BASED ON THE JSON SCHEMA STANDARD which I find too verbose for my likes but that's just me. YET REJOICE BECAUSE THE OUTPUT IS MONADIC !!!!
- To provide type validation for entities inside a json object
- To provide checkif-empty/undefined validation for entities inside a json object
- The output of this will be the first error encountered (Wrapped in a Left) or the whole object (wrapped in a Right) aka monadic (Either Monad)
npm install yajjsl
- Use Applictive-functors/Monads and it's related operations to make for readable code
- Use Ramda.js for other other operations
- Test library thoroughly
const schema1 = {
"weapon": "String",
"pokemon": Tuple("Array", "String"),
"pokeball": {
"material": "String",
"cost": "Number"
},
"characters": Tuple("Array", {"name": "String"})
};
const incorrect1 = {
"pokemon": [1, 2],
"pokeball": {
"material": ["Egg Nut", "Brown Nut"],
"cost": 100
},
"characters": [{ "name": "Red Foreman"},
{ "name": "I am Fez" }]
};
validate(incorrect1, schema1);
// output -> Left(Tuple("weapon", undefined))
// tuples are based on fantasy-land-tuples
// note I use Tuple and Tuple2 interchangebly
Left(Tuple("key", undefined)) // -> "key" field is undefined
Left(Tuple("key", false)) // -> "key" has failed schema-based-validation
Left(Tuple("key", "empty")) // -> "key" has an empty element ({} or [])
Left(Tuple("Schema","invalid")) // -> schema given is invalid
- An uber fast library
- This also features schema checking for validity based on some preset rules
- The output is based off fantasy-land compliant stuff so you should be able to use the monadic output in any monadic/ applicative-functorish chains as long as the monads/applicative-functors belong to any fantasy-land compliant library
MIT License