Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/generators/kotlin/presets/ConstraintsPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
ConstrainedFloatModel,
ConstrainedIntegerModel,
ConstrainedMetaModel,
ConstrainedObjectModel,
ConstrainedReferenceModel,
ConstrainedStringModel
} from '../../../models';
import { KotlinPreset } from '../KotlinPreset';
Expand All @@ -20,6 +22,9 @@ export const KOTLIN_CONSTRAINTS_PRESET: KotlinPreset = {
renderer.dependencyManager.addDependency(
`${importFrom}.validation.constraints.*`
);
renderer.dependencyManager.addDependency(
`${importFrom}.validation.Valid`
);
return content;
},
property({ renderer, property, content }) {
Expand All @@ -29,6 +34,15 @@ export const KOTLIN_CONSTRAINTS_PRESET: KotlinPreset = {
annotations.push(renderer.renderAnnotation('NotNull', null, 'get:'));
}

// Add @Valid for cascade validation on array/object/reference fields
if (
property.property instanceof ConstrainedReferenceModel ||
property.property instanceof ConstrainedObjectModel ||
property.property instanceof ConstrainedArrayModel
) {
annotations.push(renderer.renderAnnotation('Valid', null, 'get:'));
}

annotations.push(
...getTypeSpecificAnnotations(property.property, renderer)
);
Expand Down
35 changes: 32 additions & 3 deletions test/generators/kotlin/presets/ConstraintsPreset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ describe('KOTLIN_CONSTRAINTS_PRESET', () => {
required: ['min_number_prop', 'max_number_prop']
};

const docWithNestedObject = {
$id: 'NestedClazz',
type: 'object',
properties: {
array_prop: { type: 'array', items: { type: 'string' } },
obj_prop: { type: 'object', properties: { inner: { type: 'string' } } }
}
};

test('should render javax constraints annotations by default', async () => {
const generator = new KotlinGenerator({
presets: [KOTLIN_CONSTRAINTS_PRESET]
});
const expectedDependencies = ['import javax.validation.constraints.*'];
const expectedDependencies = [
'import javax.validation.constraints.*',
'import javax.validation.Valid'
];

const models = await generator.generate(doc);
expect(models).toHaveLength(1);
Expand All @@ -36,7 +48,10 @@ describe('KOTLIN_CONSTRAINTS_PRESET', () => {
]
});

const expectedDependencies = ['import javax.validation.constraints.*'];
const expectedDependencies = [
'import javax.validation.constraints.*',
'import javax.validation.Valid'
];

const models = await generator.generate(doc);
expect(models).toHaveLength(1);
Expand All @@ -56,11 +71,25 @@ describe('KOTLIN_CONSTRAINTS_PRESET', () => {
]
});

const expectedDependencies = ['import jakarta.validation.constraints.*'];
const expectedDependencies = [
'import jakarta.validation.constraints.*',
'import jakarta.validation.Valid'
];

const models = await generator.generate(doc);
expect(models).toHaveLength(1);
expect(models[0].result).toMatchSnapshot();
expect(models[0].dependencies).toEqual(expectedDependencies);
});

test('should render @Valid annotation for array and object properties', async () => {
const generator = new KotlinGenerator({
presets: [KOTLIN_CONSTRAINTS_PRESET]
});

const models = await generator.generate(docWithNestedObject);
expect(models.length).toBeGreaterThanOrEqual(1);
expect(models[0].result).toContain('@get:Valid');
expect(models[0].result).toMatchSnapshot();
});
});
Loading