react-jsonschema-form icon indicating copy to clipboard operation
react-jsonschema-form copied to clipboard

Specifying a meta schema in $schema forces validation error: no schema with key or ref "http://json-schema.org/draft-06/schema#"

Open sbusch opened this issue 5 years ago • 6 comments

Prerequisites

Description

If the schema specifies a meta schema in $schema:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  // ...
}

RJSF then complains with error

no schema with key or ref "http://json-schema.org/draft-06/schema#"

The error is issued by underlying ajv library.

Steps to Reproduce

Example on RJSF playground

Validation error is immediately visible.

Expected behavior

Schema can be used without errors.

Actual behavior

See description.

Version

This schema could be used on 1.2.1.

This behaviour (regression?) has been introduced by 1.3.0 and is the same at 1.5.0.

sbusch avatar May 03 '19 07:05 sbusch

Interestingly, a workaround is to add an $id referencing the same meta schema:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "$id": "http://json-schema.org/draft-06/schema#",
  // ...
}

Example on RJSF playground

So I suppose (without checking the code) that $id overrides $schema, and seems to be handled correctly (= not triggering the wrong validation error) in contrast to $schema.

In case of an invalid meta schema in $id, the $idkey is seemingly ignored and the error resurfaces again:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "$id": "UNKNOWN META SCHEMA",
  // ...
}

Example on RJSF playground

sbusch avatar May 03 '19 07:05 sbusch

Ok, got it, I had to define a Custom schema validation:

To have your schemas validated against any other meta schema than
draft-07 (the current version of [JSON Schema](http://json-schema.org/)),
make sure your schema has a `$schema` attribute that enables the validator
to use the correct meta schema. For example: ...

Introduced at #1130

@epicfaace some remarks:

  • 1.3.0 was a potentially breaking release. Does the project follow semantic versioning?
  • if the default "draft-07" comes implicitly from ajv, it's better to import and assign it explicitly -> more control over such breaking changes
  • http://react-jsonschema-form.readthedocs.io/ is quite outdated, is that known?
  • the way the example for defining a custom schema validation is implemented forces a re-rendering of the form each time the parent re-renders (because a new array is constructed every time). The example could stay simple, but this should be at least noted for the copy pasta crowd :-)

sbusch avatar May 03 '19 08:05 sbusch

@sbusch I didn't realize #1130 was a breaking change -- what part of it would you say makes it a breaking change?

epicfaace avatar May 10 '19 23:05 epicfaace

Not sure if this is related, but when trying to make this library work with AJV 6.7.0 (React 16.8.4) importing "Form" from "react-jsonschema-form"; causes Webpack to fail compilation with the following error:

Module build failed (from ./node_modules/babel-loader/lib/index.js): /SyntaxError: /Users/jstrong/webapps/rails/eebos_rails/node_modules/ajv/lib/refs/json-schema-draft-07.json: Unexpected token, expected ";" (2:13) ... @ ./node_modules/ajv/lib/ajv.js 488:19-62 @ ./node_modules/react-jsonschema-form/lib/validate.js

laspluviosillas avatar May 27 '19 21:05 laspluviosillas

@laspluviosillas sounds like a problem with your webpack config? if you can send me a link to your github repo I can help look into it

epicfaace avatar Jun 03 '19 00:06 epicfaace

AJV 6.7.0 how to solve this issue?

khateeb82 avatar Nov 28 '19 19:11 khateeb82