Skip to content

Commit

Permalink
Merge branch 'main' into taefi/rename-ts-core-to-frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
taefi authored Feb 12, 2024
2 parents 86f0679 + 380a6e9 commit 8407c2f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
isNonComposedRegularSchema,
type NonComposedRegularSchema,
type Schema,
} from '@vaadin/hilla-generator-core/Schema.js';
import type { NonComposedRegularSchema, Schema } from '@vaadin/hilla-generator-core/Schema.js';
import { template, transform } from '@vaadin/hilla-generator-utils/ast.js';
import ts, {
type Expression,
Expand All @@ -17,7 +13,6 @@ export type ValidationConstrainedSchema = NonComposedRegularSchema &

export function hasValidationConstraints(schema: Schema): schema is ValidationConstrainedSchema {
return (
isNonComposedRegularSchema(schema) &&
'x-validation-constraints' in schema &&
(schema as ValidationConstrainedSchema)['x-validation-constraints'].length > 0
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { Schema } from '@vaadin/hilla-generator-core/Schema.js';
import { expect } from 'chai';
import sinon from 'sinon';
import ts, { type NewExpression } from 'typescript';
import { ValidationConstraintProcessor, type ValidationConstraint } from '../../src/ValidationConstraintProcessor.js';
import {
ValidationConstraintProcessor,
type ValidationConstraint,
hasValidationConstraints,
} from '../../src/ValidationConstraintProcessor.js';

type AnnotationPack = Readonly<{
expected?: string;
Expand All @@ -28,6 +33,11 @@ describe('ValidationConstraintProcessor', () => {
let importer: sinon.SinonSpy;
let processor: ValidationConstraintProcessor;

type SchemaWithValidators = Schema & {
'x-validation-constraints': ValidationConstraint[];
'x-annotations': Array<{ name: string }>;
};

beforeEach(() => {
importer = sinon.fake((name: string) => ts.factory.createIdentifier(name));
processor = new ValidationConstraintProcessor(importer);
Expand Down Expand Up @@ -143,4 +153,15 @@ describe('ValidationConstraintProcessor', () => {
assertValidationConstraint(processor.process(email.obj), email.expected ?? email.str);
assertValidationConstraint(processor.process(pattern.obj), pattern.expected ?? pattern.str);
});

it('should detect validations in composed schemas', () => {
const cityComposedSchema: SchemaWithValidators = {
nullable: true,
anyOf: [{ $ref: '#/components/schemas/com.github.example.domain.City' }],
'x-validation-constraints': [{ simpleName: 'NotNull' }],
'x-annotations': [{ name: 'jakarta.persistence.ManyToOne' }],
};

expect(hasValidationConstraints(cityComposedSchema)).to.be.true;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FormValidationConstraintsModel<T extends FormValidationConstraints_1 = For
return this[_getPropertyModel_1]("notEmpty", (parent, key) => new StringModel_1(parent, key, false, { validators: [new NotEmpty_1(), new NotNull_1()] }));
}
get notNullEntity(): FormEntityModel_1 {
return this[_getPropertyModel_1]("notNullEntity", (parent, key) => new FormEntityModel_1(parent, key, false));
return this[_getPropertyModel_1]("notNullEntity", (parent, key) => new FormEntityModel_1(parent, key, false, { validators: [new NotNull_1()] }));
}
get notBlank(): StringModel_1 {
return this[_getPropertyModel_1]("notBlank", (parent, key) => new StringModel_1(parent, key, false, { validators: [new NotBlank_1()] }));
Expand Down

0 comments on commit 8407c2f

Please sign in to comment.