Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

schema-magic

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

schema-magic - npm Package Compare versions

Comparing version 0.0.8 to 0.0.9

110

lib/index.js
const $json=require('json-magic');
const $deepcopy=require('deepcopy');
const $copy=require('clone-deep');
const $ajv=require('ajv');

@@ -22,3 +22,6 @@ const $check=require('check-types');

_ajv.addMetaSchema(_jsonSchema);
require('ajv-keywords')(_ajv, ['switch']);
const _ajvCoerce = $ajv({

@@ -30,7 +33,21 @@ format:"full",

_ajvCoerce.addMetaSchema(_jsonSchema);
require('ajv-keywords')(_ajv, ['switch']);
require('ajv-keywords')(_ajvCoerce, ['switch']);
const _ajvAllErrors = $ajv({
format:"full",
allErrors:true
});
_ajvAllErrors.addMetaSchema(_jsonSchema);
require('ajv-keywords')(_ajvAllErrors, ['switch']);
const _ajvCoerceAllErrors = $ajv({
format:"full",
coerceTypes:true,
allErrors:true
});
_ajvCoerceAllErrors.addMetaSchema(_jsonSchema);
require('ajv-keywords')(_ajvCoerceAllErrors, ['switch']);
for (let k in _formatters){

@@ -42,2 +59,4 @@ if(!_formatters.hasOwnProperty(k)){

_ajvCoerce.addFormat(k,_formatters[k]);
_ajvAllErrors.addFormat(k,_formatters[k]);
_ajvCoerceAllErrors.addFormat(k,_formatters[k]);
}

@@ -52,2 +71,4 @@

_ajvCoerce.addKeyword(k,_keywords[k]);
_ajvAllErrors.addKeyword(k,_keywords[k]);
_ajvCoerceAllErrors.addKeyword(k,_keywords[k]);
}

@@ -57,3 +78,12 @@

class SchemaMagic{
static validate(document,schema,coerce){
/** Validate a schema to a document
*
* @param {*} document - The document/value to validate
* @param {object} schema - the schema to validate against
* @param {boolean} [coerce=false] - coerce values in the document to the types specified in the schema
* @param {boolean} [showAllErrors=false] - show all errors instead of only the first one
* @returns {{schemaPath: string, message: string, keyword: string, dataPath: string}[]|null|{Error: *, message: string}}
*/
static validate(document,schema,coerce,showAllErrors){
//argument checks

@@ -64,3 +94,9 @@ if (!document){throw new Error("No Document specified");}

let localAjv=_ajv;
if (coerce)localAjv=_ajvCoerce;
if (coerce&&showAllErrors){
localAjv=_ajvCoerceAllErrors;
}else if (coerce){
localAjv=_ajvCoerce;
}else if(showAllErrors){
localAjv=_ajvAllErrors;
}

@@ -75,3 +111,3 @@ let valid=true;

if (!valid){
let retErr=localAjv.errors.map(function(doc){
let retErr=localAjv.errors.map((doc)=>{
return {

@@ -92,2 +128,12 @@ message:doc.message,

/**Validate a schema ignoring parameters (values starting with@
*
* @param {*} document - The document/value to validate
* @param {object} schema - the schema to validate against
* @param {object} [options] - The options
* @param {boolean} [options.coerce] - coerce values in the document to the types specified in the schema
* @param {boolean} [options.showAllErrors] - show all errors instead of only the first one
* @param {string} [options.paramIdentifier=@] - The identifier that specifies at the start of a string that the value is a parameter
* @returns {{schemaPath: string, message: string, keyword: string, dataPath: string}[]|{Error: *, message: string}}
*/
static validateWithParameters(document,schema,options){

@@ -107,3 +153,3 @@ let paramPaths=[];

if (paramPaths.length>0){
let workingSchema=$deepcopy(schema);
let workingSchema=$copy(schema);

@@ -137,5 +183,5 @@ for (let paramPath of paramPaths){

}
return SchemaMagic.validate(document,workingSchema,options.coerce);
return SchemaMagic.validate(document,workingSchema,options.coerce,options.showAllErrors);
}else{
return SchemaMagic.validate(document,schema,options.coerce);
return SchemaMagic.validate(document,schema,options.coerce,options.showAllErrors);
}

@@ -146,7 +192,17 @@

/**Copy a schema
*
* @param {object} schema - The schema to copy
*/
static copy(schema){
if (!schema){throw new Error("No Schema specified")}
return $deepcopy(schema);
return $copy(schema);
}
/**Coerces a value to the provided schema
*
* @param {*} data - the value to coerce
* @param {object} schema - the schema to coerce against
* @returns {null|*}
*/
static coerceData(data,schema){

@@ -165,3 +221,3 @@ if (!data)return null;

var coerceFields=null;
let coerceFields=null;
if(schema.coerceFields){

@@ -173,8 +229,8 @@ coerceFields=schema.coerceFields;

for (var i=0;i<coerceFields.length;i++){
for (let i=0;i<coerceFields.length;i++){
var values=jsonPath.eval(data,coerceFields[i].path,{resultType:"PATH"}) ;
for (var j=0;j<values.length;j++){
var pointer=pathToPointer(values[j]);
var value=$json.get(data,pointer);
let values=jsonPath.eval(data,coerceFields[i].path,{resultType:"PATH"}) ;
for (let j=0;j<values.length;j++){
let pointer=pathToPointer(values[j]);
let value=$json.get(data,pointer);
$json.set(data,pointer,new Date(value));

@@ -189,3 +245,10 @@

//flattens a schema returning an array of paths to access elements of an object
/**Flattens a schema returning an array of paths to access elements of an object
*
* @param {object} schema - the schema to validate against
* @param {object} [options] - The options
* @param {string} [options.format] - path means that a / path is returned, dot means the path is specified in dot format
* @returns {{path: string, type: (string|*)}[]|[]|*}
*/
static flattenSchema(schema,options){

@@ -354,2 +417,7 @@ if (!schema)return schema;

/**Merges an array of schemas into a single schema
*
* @param {object[]} schemas - an array of schemas to merge
* @returns {null|*}
*/
static mergeSchemas(schemas){

@@ -416,3 +484,3 @@ if (!schemas||schemas.length===0)return null;

if (options.copy)
workingSchema=$deepcopy(schema);
workingSchema=$copy(schema);

@@ -493,8 +561,8 @@

}
static get Validator(){
return Validator;
return Validator;
}
}
module.exports=SchemaMagic;
module.exports=SchemaMagic;

21

package.json
{
"name": "schema-magic",
"version": "0.0.8",
"version": "0.0.9",
"description": "JSON Schema Utilities",

@@ -10,14 +10,13 @@ "author": {

"dependencies": {
"ajv": "6.10.0",
"ajv-keywords": "3.4.0",
"bson-objectid": "1.2.5",
"check-types": "8.0.2",
"cron-parser": "2.11.0",
"deepcopy": "2.0.0",
"json-magic": "0.0.8",
"ajv": "6.12.0",
"ajv-keywords": "3.4.1",
"bson-objectid": "1.3.0",
"check-types": "11.1.2",
"cron-parser": "2.13.0",
"clone-deep": "4.0.1",
"json-magic": "0.0.10",
"moment": "2.24.0",
"moment-timezone": "0.5.25",
"moment-timezone": "0.5.28",
"type-magic": "0.0.2",
"underscore": "1.9.1",
"underscore": "1.9.2",
"underscore.string": "3.3.5"

@@ -24,0 +23,0 @@ },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc