Repository: stoplightio/spectral (ts)

Issue:
allow empty config
If I create an empty .spectral.yaml file, I get this weird error
`Unexpected token u in JSON at position 0`


Pull request description:
fix: user-friendly error for empty rulesets
Fixes #1407 

**Checklist**

- [x] Tests added / updated
- [x] Docs added / updated

**Does this PR introduce a breaking change?**

- [ ] Yes
- [x] No



Patch:
diff --git a/src/rulesets/reader.ts b/src/rulesets/reader.ts
index 945d487a..8b1923c3 100644
--- a/src/rulesets/reader.ts
+++ b/src/rulesets/reader.ts
@@ -65,15 +65,18 @@ const createRulesetProcessor = (
     }
 
     processedRulesets.add(rulesetUri);
+    const content = await readParsable(rulesetUri, {
+      timeout: readOpts?.timeout,
+      encoding: 'utf8',
+      agent: readOpts?.agent,
+    });
+
+    if (content.trim().length === 0) {
+      throw new Error('Ruleset must not empty');
+    }
+
     const { result } = await createHttpAndFileResolver({ agent: readOpts?.agent }).resolve(
-      parseContent(
-        await readParsable(rulesetUri, {
-          timeout: readOpts?.timeout,
-          encoding: 'utf8',
-          agent: readOpts?.agent,
-        }),
-        rulesetUri,
-      ),
+      parseContent(content, rulesetUri),
       {
         baseUri: rulesetUri,
         dereferenceInline: false,


Test patch:
diff --git a/src/rulesets/__tests__/reader.jest.test.ts b/src/rulesets/__tests__/reader.jest.test.ts
index ddc97f99..0e6a46d6 100644
--- a/src/rulesets/__tests__/reader.jest.test.ts
+++ b/src/rulesets/__tests__/reader.jest.test.ts
@@ -48,6 +48,12 @@ describe('Rulesets reader', () => {
     nock.enableNetConnect();
   });
 
+  it('given empty ruleset, should throw a user friendly error', async () => {
+    await expect(readRuleset(path.join(__dirname, './__fixtures__/empty.json'))).rejects.toThrow(
+      'Ruleset must not empty',
+    );
+  });
+
   it('given flat, valid ruleset file should return rules', async () => {
     expect(await readRuleset(validFlatRuleset)).toEqual(
       expect.objectContaining({