Skip to content

Commit ed8be49

Browse files
authored
feat(infra): Allow $data References in Ajv Schemas [CLK-245035] (#7)
* feat(infra): Enable $data references with Ajv schemas [CLK-245035] * chore(infra): Add UTs for $data references with Ajv [CLK-245035]
1 parent 9c75c91 commit ed8be49

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/generator.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { IRole } from 'aws-cdk-lib/aws-iam';
33
import { Bucket } from 'aws-cdk-lib/aws-s3';
44
import { BucketDeployment, Source } from 'aws-cdk-lib/aws-s3-deployment';
55
import { Construct } from 'constructs';
6-
const ajv = new Ajv();
6+
7+
// $data fields in schemas allow us to set conditional permitted values.
8+
// For example, if we have two numbers `num1` and `num2`, where `num2` cannot
9+
// exceed `num1`, we can use a `$data` reference to enforce that.
10+
// See test/resources/test.schema.ts `testLessThanTest2` for an example.
11+
const ajv = new Ajv({ $data: true });
712

813
export enum GeneratorFileType {
914
JSON = 'generator_json',

test/generator.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('Generator', () => {
3131
{ test: false, test1: true, testComplex: { foo: 'buildee', arr: [1] } },
3232
{ test: false, test1: true, testComplex: { arr: [1] } },
3333
{ test: false, test1: true, testComplex: { yield: 'no', arr: [1] } }, // additionalProperties allowed here
34+
{ test: false, test1: true, test2: 2, testLessThanTest2: 2, testComplex: { arr: [1] } }, // testLessThanTest2 is less than test2
3435
];
3536

3637
const testCasesShouldFail = [
@@ -41,6 +42,7 @@ describe('Generator', () => {
4142
{ test: false, test1: true, testComplex: { arr: [1, 2, 3] } },
4243
{ test: false, test1: true, notInSchema: 'correct', testComplex: { arr: [1, 2, 3] } },
4344
{ test: false, test1: true, test2: true, testComplex: { arr: [1] } }, // additionalProperties NOT allowed here
45+
{ test: false, test1: true, test2: 2, testLessThanTest2: 3, testComplex: { arr: [1] } }, // testLessThanTest2 is GREATER than test2
4446
];
4547

4648
test.each(testCasesShouldSucceed)('validateFileContents succeeds properly', (testContents) => {

test/resources/test.schema.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { JSONSchemaType } from 'ajv';
33
interface Schema {
44
test: boolean;
55
test1: boolean;
6+
test2?: number;
7+
testLessThanTest2?: number;
68
testComplex: { foo?: string; arr: number[] };
79
}
810

@@ -32,6 +34,15 @@ export const schema: JSONSchemaType<Schema> = {
3234
test1: {
3335
type: 'boolean',
3436
},
37+
test2: {
38+
type: 'integer',
39+
nullable: true,
40+
},
41+
testLessThanTest2: {
42+
type: 'integer',
43+
nullable: true,
44+
maximum: { $data: '1/test2' } as unknown as number,
45+
},
3546
testComplex: objSchema,
3647
},
3748
required: ['test', 'test1', 'testComplex'],

0 commit comments

Comments
 (0)