freeman-check
Advanced tools
Comparing version 2.1.0 to 3.0.1
137
index.js
@@ -1,122 +0,17 @@ | ||
//freeman-check | ||
//usage: | ||
/* | ||
var Check = require('freeman-check') | ||
var object = { | ||
name: "Nabil Freeman", | ||
favourite_films: [ | ||
"Face/Off", | ||
"Bad Lieutenant", | ||
"The Wicker Man" | ||
] | ||
} | ||
var schema = { | ||
type: "object", | ||
properties: { | ||
name: { | ||
type: "string" | ||
} | ||
email: { | ||
type: "string", | ||
format: "email" | ||
}, | ||
favourite_films: { | ||
type: "array", | ||
items: { | ||
type: "string" | ||
} | ||
} | ||
}, | ||
required: ["name", "email", "favourite_films"] | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Check.test(some_object, schema).then(function(object){ | ||
console.log(object) //object === some_object | ||
}).catch(function(error){ | ||
console.log(error.message) // "`email` is missing." // "`email` is malformatted." // etc... | ||
console.log(error.schema) //error.schema === schema | ||
}) | ||
*/ | ||
var Promise = require('bluebird'); | ||
var Validate = require('jsonschema').validate; | ||
var anora = require('anora'); | ||
//override error object so we can have whatever we want in the message field. | ||
var CheckError = function(message){ | ||
this.message = message | ||
} | ||
CheckError.prototype = new Error(); | ||
var main = { | ||
test: function(object, schema){ | ||
return new Promise(function(resolve, reject){ | ||
if(object === null || object === undefined){ | ||
reject(new CheckError('The first argument is null or undefined.')) | ||
} | ||
if(schema === null || schema === undefined){ | ||
reject(new CheckError('The second argument is null or undefined.')) | ||
} | ||
//validate. | ||
var result = Validate(object, schema); | ||
//if there are no errors, the object is valid. | ||
if(result.errors.length === 0){ | ||
resolve(object); | ||
return; | ||
} | ||
//init error string | ||
var error_message = ""; | ||
result.errors.forEach(function(error){ | ||
var subject = error.argument; //name, email, etc. | ||
var problem = "is incorrect"; //default | ||
//assign plain english to error messages. | ||
switch(error.name){ | ||
case "additionalProperties": | ||
problem = "is not allowed"; | ||
break; | ||
case "required": | ||
problem = "is missing"; | ||
break; | ||
case "format": | ||
problem = "is malformatted"; | ||
break; | ||
case "type": | ||
subject = error.property.replace("instance.", ""); | ||
var type = error.schema.type; | ||
if(typeof type !== "string"){ | ||
type = JSON.stringify(type); | ||
} | ||
problem = 'needs to be ' + anora(type) + ' `' + type + '`' | ||
break; | ||
} | ||
//concatenate all validation errors. | ||
error_message += ('`' + subject + '` ' + problem + '. '); | ||
}); | ||
//remove last character from concatenated error message. | ||
error_message = error_message.slice(0, -1); | ||
var error = new CheckError(error_message); | ||
error.schema = schema; | ||
reject(error); | ||
}) | ||
}, | ||
Error: CheckError | ||
}; | ||
module.exports = main; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./src"), exports); |
{ | ||
"name": "freeman-check", | ||
"version": "2.1.0", | ||
"version": "3.0.1", | ||
"description": "Easy type and format checking on JavaScript objects", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"build": "bash ./build.sh", | ||
"lint": "npx tsc --noEmit", | ||
"test": "mocha -r ts-node/register 'src/**/*.test.ts'" | ||
}, | ||
@@ -21,5 +22,21 @@ "repository": { | ||
"anora": "^1.1.0", | ||
"bluebird": "^3.4.6", | ||
"jsonschema": "^1.1.0" | ||
"jsonschema": "^1.4.1" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.3.5", | ||
"@types/json-schema": "^7.0.12", | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^16.18.0", | ||
"@typescript-eslint/eslint-plugin": "^6.2.1", | ||
"@typescript-eslint/parser": "^6.2.1", | ||
"chai": "^4.3.7", | ||
"chai-exclude": "^2.1.0", | ||
"eslint": "^8.46.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"json-schema-to-ts": "^2.9.1", | ||
"mocha": "^10.2.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.4" | ||
} | ||
} |
107
README.md
@@ -10,7 +10,15 @@ # freeman-check | ||
# Usage | ||
``` | ||
var Check = require('freeman-check') | ||
var object = { | ||
You can use `freeman-check` to validate JavaScript objects against a specified JSON schema. | ||
In this example, the Check class is used to validate an object against a defined schema. If the object doesn't conform to the schema, a CheckError is thrown with details about the validation failure. | ||
### TypeScript example | ||
```typescript | ||
import { Check, Schema, CheckError } from 'freeman-check'; | ||
const object = { | ||
name: "Nabil Freeman", | ||
email: "nabil@freeman.sh", | ||
favourite_films: [ | ||
@@ -21,5 +29,55 @@ "Face/Off", | ||
] | ||
}; | ||
const schema: Schema = { | ||
type: "object", | ||
properties: { | ||
name: { | ||
type: "string" | ||
}, | ||
email: { | ||
type: "string", | ||
format: "email" | ||
}, | ||
favourite_films: { | ||
type: "array", | ||
items: { | ||
type: "string" | ||
} | ||
} | ||
}, | ||
required: ["name", "email", "favourite_films"] | ||
}; | ||
const check = new Check(schema); | ||
try { | ||
check.test(object); | ||
console.log("Object is valid!"); | ||
} catch (error: unknown) { | ||
if (error instanceof CheckError) { | ||
console.log(error.message); | ||
console.log(error.schema); | ||
} | ||
throw error; | ||
} | ||
``` | ||
var schema = { | ||
### JavaScript example | ||
```javascript | ||
const { Check, CheckError } = require('freeman-check'); | ||
const object = { | ||
name: "Nabil Freeman", | ||
email: "nabil@freeman.sh", | ||
favourite_films: [ | ||
"Face/Off", | ||
"Bad Lieutenant", | ||
"The Wicker Man" | ||
] | ||
}; | ||
const schema = { | ||
type: "object", | ||
@@ -29,3 +87,3 @@ properties: { | ||
type: "string" | ||
} | ||
}, | ||
email: { | ||
@@ -45,8 +103,33 @@ type: "string", | ||
Check.test(some_object, schema).then(function(object){ | ||
console.log(object) //object === some_object | ||
}).catch(function(error){ | ||
console.log(error.message) // "`email` is missing." // "`email` is malformatted." // etc... | ||
console.log(error.schema) //error.schema === schema | ||
}) | ||
``` | ||
const check = new Check(schema); | ||
try { | ||
check.test(object); | ||
console.log("Object is valid!"); | ||
} catch (error) { | ||
if (error instanceof CheckError) { | ||
console.log(error.message); | ||
console.log(error.schema); | ||
} | ||
throw error; | ||
} | ||
``` | ||
# The `Schema` type | ||
This type is directly exported from the `jsonschema` package for your convenience. | ||
This is the library that the `Check` class uses to validate objects. | ||
You can find more information about this library here: | ||
https://www.npmjs.com/package/jsonschema | ||
### Compatibility with Schema types from other libraries | ||
There are small inconsistencies between JSON Schema types from different libraries, because the standard is so flexible. | ||
Depending on reported use cases it might be necessary to add support for a custom schema type. | ||
If you think this applies to you, feel free to open an issue. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
15534
2
14
279
0
132
15
1
- Removedbluebird@^3.4.6
- Removedbluebird@3.7.2(transitive)
Updatedjsonschema@^1.4.1