*/ public function compile( FormModel $form ) { $properties = array(); $required = array(); foreach ( $form->fields() as $field ) { $json_type = $this->json_type( $field->type() ); $property = array( 'type' => $json_type, 'description' => $field->label(), ); if ( 'date' === $field->type() ) { $property['format'] = 'date'; } if ( 'email' === $field->type() ) { $property['format'] = 'email'; } if ( 'url' === $field->type() ) { $property['format'] = 'uri'; } if ( 'number' === $field->type() ) { $property['description'] .= ' Enter a number.'; } if ( in_array( $field->type(), array( 'select', 'radio' ), true ) ) { $property['enum'] = $field->options(); } if ( $field->required() && 'string' === $json_type && ! isset( $property['enum'] ) ) { $property['minLength'] = 1; } $properties[ $field->name() ] = $property; if ( $field->required() ) { $required[] = $field->name(); } } ksort( $properties, SORT_STRING ); sort( $required, SORT_STRING ); $schema = array( '$schema' => 'https://json-schema.org/draft/2020-12/schema', 'type' => 'object', 'properties' => $properties, 'additionalProperties' => false, ); if ( ! empty( $required ) ) { $schema['required'] = $required; } return $schema; } /** * Perform the documented operation. * * @param string $type Canonical type. * @return string */ private function json_type( $type ) { return 'number' === $type ? 'number' : 'string'; } }