From 727ab1853b3a231b1659ca8fa497e4e0e48698f6 Mon Sep 17 00:00:00 2001
From: Ian Savchenko <4881952+IanSavchenko@users.noreply.github.com>
Date: Wed, 5 Jun 2019 17:57:05 +0200
Subject: [PATCH] Many small fixes (#512)

* Treat dist dir as binary, so the line endings are not normalized by git

This allows to avoid annoying behaviour when you checkout repo and already have a diff which can't be reset

* add necessary dev dependencies

* clone should handle circular refs

* fix handling nested allOf and oneOf with partial schema inside

* improve noEmpty test helper

* handle alwaysFakeOptionals + additionalProperties: false

* Fix previously hidden failing test: can't alwaysFakeOptionals recursively

* make npm test do what is supposed to do
---
 .gitattributes                                |  2 +-
 package.json                                  |  2 +-
 .../core/issues/all-of-one-of-nested.json     | 41 +++++++++++++++++++
 tests/schema/core/issues/issue-345.json       |  2 +-
 .../alwaysFakeOptionalsNoAdditional.json      | 33 +++++++++++++++
 tests/schema/helpers.js                       |  2 +-
 tests/unit/core/utils.spec.js                 | 22 ++++++++++
 7 files changed, 100 insertions(+), 4 deletions(-)
 create mode 100644 tests/schema/core/issues/all-of-one-of-nested.json
 create mode 100644 tests/schema/core/option/alwaysFakeOptionalsNoAdditional.json

diff --git a/.gitattributes b/.gitattributes
index f15f4c37..0b3baca9 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1,4 +1,4 @@
 lib/** -diff
-dist/** -diff
+dist/** -diff binary
 locale/* -diff
 package-lock.json -diff
diff --git a/package.json b/package.json
index 1ca1d336..bc955470 100644
--- a/package.json
+++ b/package.json
@@ -13,7 +13,7 @@
     "build:node": "bili src/index.js --minimal --format es --format cjs",
     "build": "npm run build:browser && npm run build:node",
     "dev": "npm test -- -w",
-    "test": "npm run test:unit --",
+    "test": "npm run lint && npm run test:unit -- && npm run test:schema",
     "test:ci": "npm run coverage:all && npm run report -- -r lcov",
     "test:all": "npm run test:run tests && npm run report -- -r html",
     "test:run": "NODE_ENV=test _mocha --exit --recursive --watch-extensions js,json -r esm -bR spec",
diff --git a/tests/schema/core/issues/all-of-one-of-nested.json b/tests/schema/core/issues/all-of-one-of-nested.json
new file mode 100644
index 00000000..674e2118
--- /dev/null
+++ b/tests/schema/core/issues/all-of-one-of-nested.json
@@ -0,0 +1,41 @@
+[
+  {
+    "description": "allOf and oneOff nested",
+    "schemas": [{
+      "$schema": "http://json-schema.org/draft-04/schema#",
+      "type": "object",
+      "additionalProperties": false,
+      "properties": {
+        "date_of_birth": {
+          "type": "string",
+          "minimum": 10,
+          "maximum": 10
+        },
+        "age_group": {
+          "type": "string",
+          "minLength": 5,
+          "maxLength": 5
+        }
+      },
+
+      "allOf": [{
+        "oneOf": [{
+          "required": [
+            "age_group"
+          ]
+        }, {
+          "required": [
+            "date_of_birth"
+          ]
+        }]
+      }]
+    }],
+    "tests": [
+      {
+        "description": "should combine recursively",
+        "schema": "schemas.0",
+        "valid": true
+      }
+    ]
+  }
+]
diff --git a/tests/schema/core/issues/issue-345.json b/tests/schema/core/issues/issue-345.json
index e66eee57..cd13636b 100644
--- a/tests/schema/core/issues/issue-345.json
+++ b/tests/schema/core/issues/issue-345.json
@@ -38,7 +38,7 @@
         "additionalProperties": false
       },
       "set": {
-        "alwaysFakeOptionals": true
+        "optionalsProbability": 0.75
       },
       "skip": true,
       "valid": true
diff --git a/tests/schema/core/option/alwaysFakeOptionalsNoAdditional.json b/tests/schema/core/option/alwaysFakeOptionalsNoAdditional.json
new file mode 100644
index 00000000..cac7b168
--- /dev/null
+++ b/tests/schema/core/option/alwaysFakeOptionalsNoAdditional.json
@@ -0,0 +1,33 @@
+[
+  {
+    "description": "alwaysFakeOptionals option with additionalProperties: false",
+    "tests": [
+      {
+        "description": "should handle alwaysFakeOptionals option (= true) for objects",
+        "schema": {
+          "type": "object",
+          "properties": {
+            "optionalProperty1": { "type": "number", "default": 1 },
+            "optionalProperty2": { "type": "number", "default": 1 },
+            "optionalProperty3": { "type": "number", "default": 1 },
+            "optionalProperty4": { "type": "number", "default": 1 },
+            "optionalProperty5": { "type": "number", "default": 1 }
+          },
+          "additionalProperties": false
+        },
+        "valid": true,
+        "equal": {
+          "optionalProperty1": 1,
+          "optionalProperty2": 1,
+          "optionalProperty3": 1,
+          "optionalProperty4": 1,
+          "optionalProperty5": 1
+        },
+        "set": {
+          "alwaysFakeOptionals": true,
+          "useDefaultValue": true
+        }
+      }
+    ]
+  }
+]
diff --git a/tests/schema/helpers.js b/tests/schema/helpers.js
index 8573eb6d..02546f83 100644
--- a/tests/schema/helpers.js
+++ b/tests/schema/helpers.js
@@ -82,7 +82,7 @@ export function tryTest(test, refs, schema) {
       test.notEmpty.forEach(x => {
         const value = pick(sample, x);
 
-        if (value.length === 0) {
+        if (value === undefined || (Array.isArray(value) && !value.length)) {
           throw new Error(`${x} should not be empty`);
         }
       });
diff --git a/tests/unit/core/utils.spec.js b/tests/unit/core/utils.spec.js
index ac59813e..dcef4859 100644
--- a/tests/unit/core/utils.spec.js
+++ b/tests/unit/core/utils.spec.js
@@ -101,4 +101,26 @@ describe('Utils', () => {
       });
     });
   });
+
+  describe('clone function', () => {
+    it('should handle circular refs in objects', () => {
+      const a = {};
+      const b = {
+        a,
+      };
+      a.b = b;
+
+      const clone = utils.clone(a);
+      expect(clone.b.a).to.eql(clone);
+    });
+
+    it('should handle circular refs in arrays', () => {
+      const a = [];
+      const b = [a];
+      a.push(b);
+
+      const clone = utils.clone(a);
+      expect(clone[0][0]).to.eql(clone);
+    });
+  });
 });
-- 
GitLab