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

metaschema

Package Overview
Dependencies
Maintainers
1
Versions
73
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

metaschema - npm Package Compare versions

Comparing version 0.0.10 to 0.0.11

schemas/cartography/Address.schema

29

lib/decorators.js
'use strict';
// Field decorators
class Enum {

@@ -19,4 +21,5 @@ constructor(values) {

class Include extends Decorator {}
class Dictionary extends Decorator {}
// Index decorators
class Index {

@@ -30,7 +33,16 @@ constructor(fields) {

// Validation function decorator
class Validate {}
// Entity decorators
class Registry extends Decorator {}
class Dictionary extends Decorator {}
class System extends Decorator {}
class Log extends Decorator {}
module.exports = {
// Field decorators
Enum: (...values) => new Enum(values),
// field decorators
Many: def => new Many(def),

@@ -40,9 +52,12 @@ Parent: def => new Parent(def),

Include: def => new Include(def),
// category decorators
Dictionary: def => new Dictionary(def),
// index decorators
// Index decorators
Index: (...values) => new Index(values),
Unique: (...fields) => new Unique(fields),
// validation function decorator
Validate: fn => Object.setPrototypeOf(fn, Validate)
// Validation function decorator
Validate: fn => Object.setPrototypeOf(fn, Validate),
// Entity decorators
Registry: def => new Registry(def),
Dictionary: def => new Dictionary(def),
System: def => new System(def),
Log: def => new Log(def)
};

@@ -42,3 +42,2 @@ 'use strict';

} catch (e) {
console.log(e);
callback(e);

@@ -51,15 +50,8 @@ return;

loadSchema('./taxonomy/domains.schema', (error, schema) => {
for (const name in schema) {
const domain = schema[name];
domains.set(name, domain);
}
});
const load = (
// Load schemas
// Load schemas directory
path, // directory
callback // function, (error, data)
) => {
if (!path.includes('/')) path = './taxonomy/' + path;
if (!path.includes('/')) path = './schemas/' + path;
const schemas = {};

@@ -73,13 +65,11 @@ fs.readdir(path, (err, files) => {

loadSchema(path + '/' + file, (err, schema) => {
if (err) {
callback(err);
return;
}
const name = basename(file, '.schema');
schemas[name] = schema;
callback();
callback(null);
});
}, (err) => {
if (err) {
callback(err);
return;
}
callback(null, schemas);
});
}, (err) => callback(err, schemas));
});

@@ -99,7 +89,5 @@ };

const domain = domains.get(name);
const type = typeof(value);
const type = typeof value;
const expected = domain ? domain.type : name;
if (type === expected) return value;
const data = `{ ${name}: ${JSON.stringify(value)} }`;
console.log(new TypeError(`${data} expected to be ${expected}`).stack);
return null;

@@ -145,3 +133,3 @@ };

def = definition[field];
if (typeof(def) === 'function') {
if (typeof def === 'function') {
value = def(value);

@@ -153,2 +141,3 @@ obj[field] = value;

if (value) obj[field] = value;
else return null;
}

@@ -174,5 +163,71 @@ }

}
return (name, ...args) => factories[name](...args);
return (
name, // Category name
...args // fields to create instance
) => factories[name](...args);
};
const validate = (
category, // Category name
instance, // Instance to validate
result = { valid: true, errors: [] }
// Returns: object, result
) => {
const record = categories.get(category);
if (!record) {
result.valid = false;
result.errors.push(`Category ${category} not found`);
return;
}
const { definition } = record;
const defKeys = Object.keys(definition);
const insKeys = Object.keys(instance);
const keys = new Set([...defKeys, ...insKeys]);
let key, val, type, def, domain, defKey, insKey, decorator;
for (key of keys) {
defKey = defKeys.includes(key);
insKey = insKeys.includes(key);
if (!defKey && insKey) {
result.errors.push(`Field ${key} not defined`);
continue;
}
def = definition[key];
val = instance[key];
type = typeof val;
domain = domains.get(def.domain);
if (!insKey && defKey) {
if (typeof def === 'function') {
decorator = def.prototype.constructor.name;
if (decorator === 'Validate' && !def(instance)) {
result.errors.push('Validation failed');
continue;
}
}
if (def.required) {
result.errors.push(`Field ${key} not found`);
}
continue;
}
if (domain && type !== domain.type) {
result.errors.push(`Field ${key}:${val} expected to be ${domain.type}`);
}
}
result.valid = result.errors.length === 0;
return result;
};
const validateFields = (
category, // Category name
instance, // Instance to validate
result = { valid: true, errors: [] }
// Returns: object, result
) => {
let key, val;
for (key in instance) {
val = instance[key];
validate(category, val, result);
}
return result;
};
const guard = (

@@ -193,2 +248,15 @@ // Function arguments guard

loadSchema('./schemas/CategoryField.schema', (err, schema) => {
if (err) throw err;
build({ CategoryField: schema });
});
loadSchema('./schemas/domains.schema', (err, schema) => {
if (err) throw err;
for (const name in schema) {
const domain = schema[name];
domains.set(name, domain);
}
});
module.exports = {

@@ -201,3 +269,5 @@ load,

createInstance,
validate,
validateFields,
guard,
};
{
"name": "metaschema",
"version": "0.0.10",
"version": "0.0.11",
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>",

@@ -34,3 +34,3 @@ "description": "Metadata Schema and Interface Definition Language (IDL)",

"dependencies": {
"metarhia-common": "^0.0.30",
"metarhia-common": "^0.0.31",
"metasync": "^0.3.29"

@@ -37,0 +37,0 @@ },

@@ -6,1 +6,2 @@ 'use strict';

require('./guard');
require('./validate');
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