Skip to content

Commit d6a6fb3

Browse files
author
Victor Moreno
committed
human readable schema parsing example
1 parent 286c398 commit d6a6fb3

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

cedar-wasm-example/__tests__/main.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,4 +667,47 @@ describe('get valid request envs', () => {
667667
expect(requestEnvs.actions).toStrictEqual(['NS::Action::"a"', 'NS::Action::"a2"']);
668668
expect(requestEnvs.resources).toStrictEqual(['NS::R2']);
669669
});
670+
test('human readable schema parsing', () => {
671+
const schemaStr: cedar.Schema = `
672+
namespace NSxx {
673+
entity E;
674+
entity R1 in [R] = {"p1": String};
675+
entity R;
676+
entity R2 in [R] = {"p1": Long};
677+
entity E1 in [E] = {"p1": String};
678+
entity E2 in [E] = {"p1": Long};
679+
action "as";
680+
action "a" in [Action::"as"] appliesTo {
681+
principal: [E1, E2],
682+
resource: [R1, R2],
683+
context: {"c1": Long}
684+
};
685+
action "a1" in [Action::"as"] appliesTo {
686+
principal: [E1],
687+
resource: [R1],
688+
context: {"c1": Long}
689+
};
690+
action "a2" in [Action::"as"] appliesTo {
691+
principal: [E2],
692+
resource: [R2],
693+
context: {"c1": Long}
694+
};
695+
}
696+
`;
697+
const checkParseResult = cedar.checkParseSchema(schemaStr);
698+
expect(checkParseResult.type).toEqual('success');
699+
const parseResult = cedar.schemaToJson(schemaStr);
700+
if (parseResult.type !== 'success') {
701+
throw new Error(`Expected success in parsing schema, got ${JSON.stringify(parseResult, null, 4)}`);
702+
}
703+
expect(Object.keys(parseResult.json).length).toBe(1);
704+
expect(Object.keys(parseResult.json)[0]).toEqual('NSxx');
705+
if (!('NSxx' in parseResult.json)) {
706+
throw new Error(`Expected NSxx in ${JSON.stringify(parseResult.json, null, 4)}`);
707+
}
708+
const schemaFrag = parseResult.json['NSxx'] as cedar.NamespaceDefinition<string>;
709+
expect(Object.keys(schemaFrag.entityTypes).length).toEqual(6);
710+
expect(Object.keys(schemaFrag.actions).length).toEqual(4);
711+
712+
});
670713
});

0 commit comments

Comments
 (0)