Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var validateJson = function( working_dir, json ) {
var valide = tv4.validate( json, require('../specs/user_'+config.client_version+'.json') );
if( !valide ) {
console.log('ERROR'.red +': spore.json is not valide:\n' + tv4.error.message );
process.exit();
}
let files = json.files;
// Check if any files are about to be included
if( files.length == 0 )
throw new Error('Include some files first');
// Check if linked files exists
files.forEach( ( file ) => {
let exists = fs.existsSync( working_dir + '/' + file );
if( !exists ) throw new Error(`File ${file} can't be found in ${working_dir}`);
});
}
function validate(json, schema) {
// ignore validation if no schema
if (schema === undefined) return Promise.resolve();
const valid = tv4.validate(json, schema);
if (!valid) {
debug(tv4.error.dataPath);
debug(tv4.error.message);
return Promise.reject(tv4.error);
}
return Promise.resolve();
}
if (typeof request === 'string') {
try {
request = JSON.parse(request);
} catch (e) {
logger.error('unable to parse request', request, e);
return;
}
}
logger.verbose('Request: %j', request);
// Check whether this request is a valid request, return error directly if not
if (
!tv4.validate(request, RequestSchema['general']) ||
!tv4.validate(request, RequestSchema[request.op])
) {
Client.pushError(parseWebsocket, 1, tv4.error.message);
logger.error('Connect message error %s', tv4.error.message);
return;
}
switch (request.op) {
case 'connect':
this._handleConnect(parseWebsocket, request);
break;
case 'subscribe':
this._handleSubscribe(parseWebsocket, request);
break;
case 'update':
this._handleUpdateSubscription(parseWebsocket, request);
break;
case 'unsubscribe':
this._handleUnsubscribe(parseWebsocket, request);
function validate(json, schema) {
// ignore validation if no schema
if (schema === undefined) return Promise.resolve();
const valid = tv4.validate(json, schema);
if (!valid) {
debug(tv4.error.dataPath);
debug(tv4.error.message);
return Promise.reject(tv4.error);
}
return Promise.resolve();
}
try {
request = JSON.parse(request);
} catch (e) {
logger.error('unable to parse request', request, e);
return;
}
}
logger.verbose('Request: %j', request);
// Check whether this request is a valid request, return error directly if not
if (
!tv4.validate(request, RequestSchema['general']) ||
!tv4.validate(request, RequestSchema[request.op])
) {
Client.pushError(parseWebsocket, 1, tv4.error.message);
logger.error('Connect message error %s', tv4.error.message);
return;
}
switch (request.op) {
case 'connect':
this._handleConnect(parseWebsocket, request);
break;
case 'subscribe':
this._handleSubscribe(parseWebsocket, request);
break;
case 'update':
this._handleUpdateSubscription(parseWebsocket, request);
break;
case 'unsubscribe':
this._handleUnsubscribe(parseWebsocket, request);
break;
it('loads configuration files into its data property', function () {
var expected = fs.readYamlSync(fixtureRCExpanded);
var rc = DappleRC.create({paths: [fixtureRC]});
assert.deepEqual(rc.data, expected);
var valid = tv4.validate(rc.data, dapplercSchema);
assert(valid, 'dapplerc is not valid by schema' + tv4.error);
});
function(model, schema, callback) {
const valid = tv4.validate(
this.results.fields[this.component],
JSON.parse(schema)
);
if (!valid) {
return callback(
new Error(
'Expected no validation errors on schema but got:\n' +
JSON.stringify(tv4.error, null, 2)
)
);
} else {
return callback();
}
}
);
function jsonValidate (schema, value, propPath) {
if (tv4.validate(value, schema)) {
return true;
}
else if (propPath !== undefined) {
let dataPath = propPath ? propPath + "." + tv4.error.dataPath : tv4.error.dataPath;
throw ono(tv4.error, { status: 400 }, 'JSON Schema validation error. \nData path: "%s" \nSchema path: "%s"',
dataPath, tv4.error.schemaPath);
}
else {
throw ono(tv4.error, { status: 400 }, "JSON Schema validation error.");
}
}
function jsonValidate(value, schema, detailedErrorMessage) {
if (tv4.validate(value, schema)) {
return true;
}
else if (detailedErrorMessage) {
throw syntaxError('%s \nData path: "%s" \nSchema path: "%s"\n',
tv4.error.message, tv4.error.dataPath, tv4.error.schemaPath);
}
else {
throw syntaxError(tv4.error.message);
}
}
test.run(function () {
var result = tv4.validate(schema, metaSchema);
assert.False(result);
assert.NotNull(tv4.error);
for (var propertyName in expectedErrorValues) {
assert.Equal(expectedErrorValues[propertyName], tv4.error[propertyName]);
}
});
}