Repository: jupyter/nbformat

Issue:
When using fastjsonschema, nbformat.v4.new_code_cell throws error
Reproducer - 
 `env NBFORMAT_VALIDATOR=fastjsonschema python3 -c 'import nbformat; nbformat.v4.new_code_cell()'`

Output- 
```Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/nbformat/v4/nbbase.py", line 124, in new_code_cell
    validate(cell, 'code_cell')
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/nbformat/v4/nbbase.py", line 38, in validate
    return validate(node, ref=ref, version=nbformat)
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/nbformat/validator.py", line 262, in validate
    relax_add_props=relax_add_props):
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/nbformat/validator.py", line 292, in iter_validate
    errors = validator.iter_errors(nbdict, {'$ref' : '#/definitions/%s' % ref})
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/nbformat/json_compat.py", line 51, in iter_errors
    validate_func = self._validator if schema is None else fastjsonschema.compile(schema)
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/fastjsonschema/__init__.py", line 167, in compile
    global_state = code_generator.global_state
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/fastjsonschema/draft04.py", line 70, in global_state
    res = super().global_state
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/fastjsonschema/generator.py", line 80, in global_state
    self._generate_func_code()
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/fastjsonschema/generator.py", line 118, in _generate_func_code
    self.generate_func_code()
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/fastjsonschema/generator.py", line 132, in generate_func_code
    self.generate_validation_function(uri, name)
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/fastjsonschema/generator.py", line 140, in generate_validation_function
    with self._resolver.resolving(uri) as definition:
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/fastjsonschema/ref_resolver.py", line 135, in resolving
    yield resolve_path(schema, fragment)
  File "/Users/maharshi/Library/Python/3.7/lib/python/site-packages/fastjsonschema/ref_resolver.py", line 41, in resolve_path
    raise JsonSchemaDefinitionException('Unresolvable ref: {}'.format(part))
fastjsonschema.exceptions.JsonSchemaDefinitionException: Unresolvable ref: definitions```

Discussion:

Patch:
diff --git a/nbformat/json_compat.py b/nbformat/json_compat.py
index 936d3c8..2c4f81e 100644
--- a/nbformat/json_compat.py
+++ b/nbformat/json_compat.py
@@ -38,6 +38,7 @@ class FastJsonSchemaValidator(JsonSchemaValidator):
     name = "fastjsonschema"
 
     def __init__(self, schema):
+        super().__init__(schema)
         self._validator = fastjsonschema.compile(schema)
 
     def validate(self, data):
@@ -47,8 +48,11 @@ class FastJsonSchemaValidator(JsonSchemaValidator):
             raise ValidationError(error.message, schema_path=error.path)
 
     def iter_errors(self, data, schema=None):
+        if schema is not None:
+            return self._default_validator.iter_errors(data, schema)
+
         errors = []
-        validate_func = self._validator if schema is None else fastjsonschema.compile(schema)
+        validate_func = self._validator
         try:
             validate_func(data)
         except _JsonSchemaException as error:


Test patch:
diff --git a/nbformat/tests/test_validator.py b/nbformat/tests/test_validator.py
index 7723dc9..1c7a1ee 100644
--- a/nbformat/tests/test_validator.py
+++ b/nbformat/tests/test_validator.py
@@ -197,3 +197,15 @@ def test_invalid_validator_raises_value_error_after_read():
     set_validator("foobar")
     with pytest.raises(ValueError):
         validate(nb)
+
+
+def test_fallback_validator_with_iter_errors_using_ref():
+    """
+    Test that when creating a standalone object (code_cell etc)
+    the default validator is used as fallback.
+    """
+    import nbformat
+    set_validator("fastjsonschema")
+    nbformat.v4.new_code_cell()
+    nbformat.v4.new_markdown_cell()
+    nbformat.v4.new_raw_cell()