Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function validateAgainstExample(t, validator, protoType, protoEnumTypes, examplePath, jsonExample) {
const originalJsonExample = clone(jsonExample);
protoEnumsToInts(protoType, jsonExample, protoEnumTypes);
// Sanity check out input data
const schemaName = protoType.options[EXTENSION_PROPERTY];
validateXVIZJSON(t, validator, schemaName, originalJsonExample, 'Example JSON');
// Verify content "works" as protobuf
verifyProto(t, protoType, jsonExample, `Protobuf verified`);
// Populate proto object with content, we do "fromObject" first
// so that the well known types will be handled
const protoObject = protoType.fromObject(jsonExample);
const serializedProtoData = protoType.encode(protoObject).finish();
const protoData = protoType.decode(serializedProtoData);
// Dump to Object
const options = {
test('protoEnumsToInts#Simple', t => {
const goodObj = {
foo: 'zip',
other: 42
};
const expObj = {
foo: 0,
other: 42
};
protoEnumsToInts(TEST_PROTO_TYPE, goodObj, ENUM_TYPES);
t.ok(JSON.stringify(goodObj) === JSON.stringify(expObj), 'Converted enum');
t.end();
});
test('protoEnumsToInts#PackageScopeEnum', t => {
const goodObj = {
foo: 'cpp',
other: 42
};
const expObj = {
foo: 1,
other: 42
};
const testPackageType = TEST_PROTO_ROOT.lookupType('TestPackage');
protoEnumsToInts(testPackageType, goodObj, ENUM_TYPES);
t.ok(JSON.stringify(goodObj) === JSON.stringify(expObj), 'Converted enum');
t.end();
});
mapped: {
foo: {
foo: 1,
other: 42
}
},
list: [
{
foo: 0
}
],
primMap: {a: 4, b: 2},
primList: ['a', 'b']
};
protoEnumsToInts(nestedType, nestedObj, ENUM_TYPES);
t.deepEqual(nestedObj, expectedObj, 'Converted nested and mapped enums');
t.end();
});
() => protoEnumsToInts(TEST_PROTO_TYPE, badObj, ENUM_TYPES),
/Error/,