|
| 1 | +// Support json-schema.org schema conversion to a Parquet file |
| 2 | +import { JSONSchema4 } from 'json-schema'; |
| 3 | +import { FieldDefinition, SchemaDefinition } from './declare'; |
| 4 | +import * as fields from './fields'; |
| 5 | + |
| 6 | +type SupportedJSONSchema4 = Omit<JSONSchema4, '$ref' | 'multipleOf' | 'allOf' | 'anyOf' | 'oneOf' | 'not' | 'additionalItems' | 'enum' | 'extends'> |
| 7 | + |
| 8 | +/** |
| 9 | + * Simple check to make sure that `SupportedJSONSchema4` is correct. |
| 10 | + * There are a lot of JSON schema stuff we just don't support for now. |
| 11 | + */ |
| 12 | +const isJsonSchemaSupported = (js: JSONSchema4): js is SupportedJSONSchema4 => { |
| 13 | + const unsupportedFields = [ |
| 14 | + "$ref", |
| 15 | + "multipleOf", |
| 16 | + "allOf", |
| 17 | + "anyOf", |
| 18 | + "oneOf", |
| 19 | + "not", |
| 20 | + "additionalItems", |
| 21 | + "enum", |
| 22 | + "extends", |
| 23 | + ]; |
| 24 | + for (const field in unsupportedFields) { |
| 25 | + if (!(js[field] === undefined || js[field] === false)) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + } |
| 29 | + return true; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Error to capture all the unsupported edge cases |
| 34 | + */ |
| 35 | +export class UnsupportedJsonSchemaError extends Error { |
| 36 | + constructor(msg: string) { |
| 37 | + const message = `Unsupported JSON schema: ${msg}`; |
| 38 | + super(message); |
| 39 | + this.name = 'UnsupportedJsonSchemaError'; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Json Schema has required at the top level instead of field level |
| 45 | + */ |
| 46 | +const isJsonSchemaRequired = (jsonSchema: SupportedJSONSchema4) => (field: string): boolean => { |
| 47 | + switch (jsonSchema.required) { |
| 48 | + case true: return true; |
| 49 | + case undefined: |
| 50 | + case false: |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + return jsonSchema.required.includes(field); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Converts the Array field type into the correct Field Definition |
| 59 | + */ |
| 60 | +const fromJsonSchemaArray = (fieldValue: SupportedJSONSchema4, optionalFieldList: boolean): FieldDefinition => { |
| 61 | + if (!fieldValue.items || !fieldValue.items.type) { |
| 62 | + throw new UnsupportedJsonSchemaError("Array field with no values found."); |
| 63 | + } |
| 64 | + |
| 65 | + switch (fieldValue.items.type) { |
| 66 | + case 'string': |
| 67 | + return fields.createListField('UTF8', optionalFieldList); |
| 68 | + case 'integer': |
| 69 | + case 'number': |
| 70 | + return fields.createListField('INT64', optionalFieldList); |
| 71 | + case 'boolean': |
| 72 | + return fields.createListField('BOOLEAN', optionalFieldList); |
| 73 | + case 'object': |
| 74 | + return fields.createStructListField(fromJsonSchema(fieldValue.items), optionalFieldList); |
| 75 | + default: |
| 76 | + throw new UnsupportedJsonSchemaError(`Array field type ${JSON.stringify(fieldValue.items)} is unsupported.`); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Converts a field from a JSON Schema into a Parquet Field Definition |
| 82 | + */ |
| 83 | +const fromJsonSchemaField = (jsonSchema: JSONSchema4) => (fieldName: string, fieldValue: JSONSchema4): FieldDefinition => { |
| 84 | + if (!isJsonSchemaSupported(fieldValue)) { |
| 85 | + throw new UnsupportedJsonSchemaError(`Field: ${fieldName} has an unsupported schema`); |
| 86 | + } |
| 87 | + const optional = !isJsonSchemaRequired(jsonSchema)(fieldName); |
| 88 | + |
| 89 | + switch (fieldValue.type) { |
| 90 | + case 'string': |
| 91 | + return fields.createStringField(optional); |
| 92 | + case 'integer': |
| 93 | + case 'number': |
| 94 | + return fields.createIntField(64, optional); |
| 95 | + case 'boolean': |
| 96 | + return fields.createBooleanField(optional); |
| 97 | + case 'array': |
| 98 | + return fromJsonSchemaArray(fieldValue, optional); |
| 99 | + case 'object': |
| 100 | + return fields.createStructField(fromJsonSchema(fieldValue), optional); |
| 101 | + default: |
| 102 | + throw new UnsupportedJsonSchemaError( |
| 103 | + `Unable to convert "${fieldName}" with JSON Schema type "${fieldValue.type}" to a Parquet Schema.`, |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Converts supported Json Schemas into Parquet Schema Definitions |
| 110 | + */ |
| 111 | +export const fromJsonSchema = (jsonSchema: JSONSchema4): SchemaDefinition => { |
| 112 | + if (!isJsonSchemaSupported(jsonSchema)) { |
| 113 | + throw new UnsupportedJsonSchemaError("Unsupported fields found"); |
| 114 | + } |
| 115 | + |
| 116 | + const schema: SchemaDefinition = {}; |
| 117 | + |
| 118 | + const fromField = fromJsonSchemaField(jsonSchema) |
| 119 | + |
| 120 | + for (const [fieldName, fieldValue] of Object.entries( |
| 121 | + jsonSchema.properties || {}, |
| 122 | + )) { |
| 123 | + schema[fieldName] = fromField(fieldName, fieldValue); |
| 124 | + } |
| 125 | + |
| 126 | + return schema; |
| 127 | +} |
0 commit comments