@@ -100,21 +100,23 @@ public void reconcile(YamlFileAST ast) {
100100 slowDelayedConstraints .clear ();
101101 try {
102102 List <Node > nodes = ast .getNodes ();
103+ boolean emptyDoc = isEmptyDoc (ast );
103104 IntegerRange expectedDocs = schema .expectedNumberOfDocuments ();
104- if (!expectedDocs .isInRange (nodes .size ())) {
105+ int numberOfDocs = emptyDoc ? 0 : nodes .size ();
106+ if (!expectedDocs .isInRange (numberOfDocs )) {
105107 //wrong number of documents in the file. Figure out a good error message.
106- if (nodes . isEmpty () ) {
108+ if (emptyDoc ) {
107109 problem (allOf (ast .getDocument ()), "'" +schema .getName ()+"' must have at least some Yaml content" );
108- } else if (expectedDocs .isTooLarge (nodes . size () )) {
110+ } else if (expectedDocs .isTooLarge (numberOfDocs )) {
109111 int upperBound = expectedDocs .getUpperBound ();
110112 Node extraNode = nodes .get (upperBound );
111113 problem (dashesAtStartOf (ast , extraNode ), "'" +schema .getName ()+"' should not have more than " +upperBound +" Yaml Documents" );
112- } else if (expectedDocs .isTooSmall (nodes . size () )) {
114+ } else if (expectedDocs .isTooSmall (numberOfDocs )) {
113115 int lowerBound = expectedDocs .getLowerBound ();
114116 problem (endOf (ast .getDocument ()), "'" +schema .getName ()+"' should have at least " +lowerBound +" Yaml Documents" );
115117 }
116118 }
117- if (nodes !=null && !nodes .isEmpty ()) {
119+ if (! emptyDoc && nodes !=null && !nodes .isEmpty ()) {
118120 for (int i = 0 ; i < nodes .size (); i ++) {
119121 Node node = nodes .get (i );
120122 reconcile (ast , new YamlPath (YamlPathSegment .valueAt (i )), /*parent*/ null , node , schema .getTopLevelType ());
@@ -127,6 +129,21 @@ public void reconcile(YamlFileAST ast) {
127129 verifyDelayedConstraints ();
128130 }
129131 }
132+
133+ private static boolean isEmptyDoc (YamlFileAST ast ) {
134+ List <Node > nodes = ast .getNodes ();
135+ if (nodes != null && !nodes .isEmpty ()) {
136+ if (nodes .size () == 1 ) {
137+ Node n = nodes .get (0 );
138+ if (n instanceof MappingNode ) {
139+ MappingNode mn = (MappingNode ) n ;
140+ return mn .getValue ().isEmpty ();
141+ }
142+ }
143+ return false ;
144+ }
145+ return true ;
146+ }
130147
131148 private DocumentRegion dashesAtStartOf (YamlFileAST ast , Node node ) {
132149 try {
@@ -241,7 +258,7 @@ private void parse(YamlFileAST ast, Node node, YType type, ValueParser parser) {
241258 }
242259 } catch (Exception e ) {
243260 // check for YTT
244- if (isYttUsed (node )) {
261+ if (! isYttUsed (node )) {
245262 ProblemType problemType = getProblemType (e );
246263 DocumentRegion region = getRegion (e , ast .getDocument (), node );
247264 String msg = getMessage (e );
@@ -250,9 +267,9 @@ private void parse(YamlFileAST ast, Node node, YType type, ValueParser parser) {
250267 }
251268 }
252269
253- private boolean isYttUsed (Node node ) {
270+ private static boolean isYttUsed (Node node ) {
254271 if (node .getBlockComments () != null ) {
255- return node .getBlockComments ().stream ().anyMatch (cl -> cl .getValue ().startsWith ("@" ));
272+ return node .getBlockComments ().stream ().anyMatch (cl -> cl .getValue ().trim (). startsWith ("@" ));
256273 }
257274 return false ;
258275 }
0 commit comments