gremlin-helper
Version:
A wrapper around the gremlin client to introduce model validation and other useful functionality to use within a web api.
76 lines • 3.28 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const TypeOps_1 = require("./TypeOps");
class Vertex {
constructor(schema, types = TypeOps_1.defaultTypeOps) {
this.schema = schema;
this.types = types;
}
processAsync(obj) {
return __awaiter(this, void 0, void 0, function* () {
const result = {
hasErrors: false,
errors: {},
model: {}
};
if (!obj) {
result.hasErrors = true;
result.model = null;
return result;
}
if (this.schema.props) {
for (const key in this.schema.props) {
const prop = typeof this.schema.props[key] === 'object'
? this.schema.props[key]
: { type: this.schema.props[key] };
let value = key in obj ? obj[key] : undefined;
if (value !== undefined) {
if (this.types && prop.type in this.types) {
const opResult = yield this.types[prop.type](prop, value);
if (opResult.error) {
result.hasErrors = true;
result.errors[key] = opResult.error;
continue;
}
value = opResult.value;
}
if (this.ops && key in this.ops && typeof this.ops[key] === 'function') {
const opResult = yield this.ops[key](prop, value);
if (opResult.error) {
result.hasErrors = true;
result.errors[key] = opResult.error;
continue;
}
value = opResult.value;
}
if (prop.required && !value) {
result.hasErrors = true;
result.errors[key] = 'Required';
continue;
}
if (value !== undefined) {
result.model[key] = value;
}
}
}
}
if (result.hasErrors) {
result.model = null;
}
else {
result.errors = null;
}
return result;
});
}
}
exports.Vertex = Vertex;
//# sourceMappingURL=Vertex.js.map