oxidecomputer/typify

Failing schema: unhandled array validation

clbarnes opened this issue · 1 comments

The schema is here: https://github.com/jgm/djot.js/blob/baa4d3e62bccaebe0680adea1b185adbd211fd8a/djot-schema.json , and is AFAIK automatically generated using typescript-json-schema.

The output of cargo typify is

cargo typify --output src/types.rs djot.js/djot-schema.json
Error: 
   0: Failed to convert JSON Schema to Rust code
   1: Could not add ref types from the 'definitions' field in the JSON Schema
   2: schema invalid: unhandled array validation ArrayValidation {
          items: Some(
              Vec(
                  [
                      Object(
                          SchemaObject {
                              metadata: None,
                              instance_type: None,
                              format: None,
                              enum_values: None,
                              const_value: None,
                              subschemas: None,
                              number: None,
                              string: None,
                              array: None,
                              object: None,
                              reference: Some(
                                  "#/definitions/Caption",
                              ),
                              extensions: {},
                          },
                      ),
                  ],
              ),
          ),
          additional_items: Some(
              Object(
                  SchemaObject {
                      metadata: None,
                      instance_type: None,
                      format: None,
                      enum_values: None,
                      const_value: None,
                      subschemas: None,
                      number: None,
                      string: None,
                      array: None,
                      object: None,
                      reference: Some(
                          "#/definitions/Row",
                      ),
                      extensions: {},
                  },
              ),
          ),
          max_items: None,
          min_items: Some(
              1,
          ),
          unique_items: None,
          contains: None,
      }
ahl commented

Here's the schema:

                  {
                    "additionalItems": {
                        "$ref": "#/definitions/Row"
                    },
                    "items": [
                        {
                            "$ref": "#/definitions/Caption"
                        }
                    ],
                    "minItems": 1,
                    "type": "array"
                }

I think we'd want to generate a type more or less like this?

(Caption, Vec<Row>)

This is a bit like #203 and a subset of #254. In particular the constraints relevant for this proposed type are

  • maxItems = None
  • items is an array
  • items.len() == minItems

If that type seems reasonable I think it wouldn't be that hard to implement; the only trick is to "flatten" or "inline" the second item in tuple for Serialize and Deserialize (which @dtolnay describes here serde-rs/serde#1337 (comment)).