@@ -63,4 +63,168 @@ describe("$onValidate", () => {
6363 expectDiagnosticEmpty ( diagnostics ) ;
6464 } ) ;
6565 } ) ;
66+
67+ describe ( "empty-enum" , ( ) => {
68+ it ( "reports error for enum with no values" , async ( ) => {
69+ const [ _ , diagnostics ] = await Tester . compileAndDiagnose ( t . code `
70+ @schema
71+ namespace TestNamespace {
72+ enum Status {}
73+ model Book { status: Status; }
74+ @query op getBooks(): Book[];
75+ }
76+ ` ) ;
77+
78+ expectDiagnostics ( diagnostics , {
79+ code : "@typespec/graphql/empty-enum" ,
80+ severity : "error" ,
81+ message : 'Enum "Status" must define at least one value. GraphQL enums cannot be empty.' ,
82+ } ) ;
83+ } ) ;
84+
85+ it ( "does not report error for enum with values" , async ( ) => {
86+ const [ _ , diagnostics ] = await Tester . compileAndDiagnose ( t . code `
87+ @schema
88+ namespace TestNamespace {
89+ enum Status { Active, Inactive }
90+ model Book { status: Status; }
91+ @query op getBooks(): Book[];
92+ }
93+ ` ) ;
94+
95+ expectDiagnosticEmpty ( diagnostics ) ;
96+ } ) ;
97+ } ) ;
98+
99+ describe ( "reserved-name" , ( ) => {
100+ it ( "reports error for model name starting with __" , async ( ) => {
101+ const [ _ , diagnostics ] = await Tester . compileAndDiagnose ( t . code `
102+ @schema
103+ namespace TestNamespace {
104+ model __Reserved { title: string; }
105+ @query op get(): __Reserved;
106+ }
107+ ` ) ;
108+
109+ expectDiagnostics ( diagnostics , {
110+ code : "@typespec/graphql/reserved-name" ,
111+ severity : "error" ,
112+ message :
113+ 'Name "__Reserved" must not begin with "__" (two underscores), which is reserved by GraphQL for introspection.' ,
114+ } ) ;
115+ } ) ;
116+
117+ it ( "reports error for property name starting with __" , async ( ) => {
118+ const [ _ , diagnostics ] = await Tester . compileAndDiagnose ( t . code `
119+ @schema
120+ namespace TestNamespace {
121+ model Book { __internal: string; }
122+ @query op getBooks(): Book[];
123+ }
124+ ` ) ;
125+
126+ expectDiagnostics ( diagnostics , {
127+ code : "@typespec/graphql/reserved-name" ,
128+ severity : "error" ,
129+ message :
130+ 'Name "__internal" must not begin with "__" (two underscores), which is reserved by GraphQL for introspection.' ,
131+ } ) ;
132+ } ) ;
133+
134+ it ( "reports error for operation parameter starting with __" , async ( ) => {
135+ const [ _ , diagnostics ] = await Tester . compileAndDiagnose ( t . code `
136+ @schema
137+ namespace TestNamespace {
138+ model Book { title: string; }
139+ @query op getBook(__id: string): Book;
140+ }
141+ ` ) ;
142+
143+ expectDiagnostics ( diagnostics , {
144+ code : "@typespec/graphql/reserved-name" ,
145+ severity : "error" ,
146+ message :
147+ 'Name "__id" must not begin with "__" (two underscores), which is reserved by GraphQL for introspection.' ,
148+ } ) ;
149+ } ) ;
150+
151+ it ( "reports error for enum name starting with __" , async ( ) => {
152+ const [ _ , diagnostics ] = await Tester . compileAndDiagnose ( t . code `
153+ @schema
154+ namespace TestNamespace {
155+ enum __Status { Active }
156+ model Book { status: __Status; }
157+ @query op getBooks(): Book[];
158+ }
159+ ` ) ;
160+
161+ expectDiagnostics ( diagnostics , {
162+ code : "@typespec/graphql/reserved-name" ,
163+ severity : "error" ,
164+ message :
165+ 'Name "__Status" must not begin with "__" (two underscores), which is reserved by GraphQL for introspection.' ,
166+ } ) ;
167+ } ) ;
168+
169+ it ( "reports error for enum member starting with __" , async ( ) => {
170+ const [ _ , diagnostics ] = await Tester . compileAndDiagnose ( t . code `
171+ @schema
172+ namespace TestNamespace {
173+ enum Status { __Internal, Active }
174+ model Book { status: Status; }
175+ @query op getBooks(): Book[];
176+ }
177+ ` ) ;
178+
179+ expectDiagnostics ( diagnostics , {
180+ code : "@typespec/graphql/reserved-name" ,
181+ severity : "error" ,
182+ message :
183+ 'Name "__Internal" must not begin with "__" (two underscores), which is reserved by GraphQL for introspection.' ,
184+ } ) ;
185+ } ) ;
186+
187+ it ( "reports error for union name starting with __" , async ( ) => {
188+ const [ _ , diagnostics ] = await Tester . compileAndDiagnose ( t . code `
189+ @schema
190+ namespace TestNamespace {
191+ model Cat { meow: string; }
192+ model Dog { bark: string; }
193+ union __Pet { cat: Cat, dog: Dog }
194+ @query op getPet(): __Pet;
195+ }
196+ ` ) ;
197+
198+ expectDiagnostics ( diagnostics , {
199+ code : "@typespec/graphql/reserved-name" ,
200+ severity : "error" ,
201+ message :
202+ 'Name "__Pet" must not begin with "__" (two underscores), which is reserved by GraphQL for introspection.' ,
203+ } ) ;
204+ } ) ;
205+
206+ it ( "does not report error for names with single underscore prefix" , async ( ) => {
207+ const [ _ , diagnostics ] = await Tester . compileAndDiagnose ( t . code `
208+ @schema
209+ namespace TestNamespace {
210+ model _Book { _title: string; }
211+ @query op _getBooks(_filter: string): _Book[];
212+ }
213+ ` ) ;
214+
215+ expectDiagnosticEmpty ( diagnostics ) ;
216+ } ) ;
217+
218+ it ( "does not report error for names with underscore in middle" , async ( ) => {
219+ const [ _ , diagnostics ] = await Tester . compileAndDiagnose ( t . code `
220+ @schema
221+ namespace TestNamespace {
222+ model My__Book { my__title: string; }
223+ @query op get__Books(): My__Book[];
224+ }
225+ ` ) ;
226+
227+ expectDiagnosticEmpty ( diagnostics ) ;
228+ } ) ;
229+ } ) ;
66230} ) ;
0 commit comments