rawmodel/framework

JSON Schema support

xpepermint opened this issue · 1 comments

  • Add new @rawmodel/schema package.
  • We support only a subset of JSON Schema objects, arrays, primitive types.
  • We should support polymorphic arrays.
  • We support validators which are or can be added in the @rawmodel/validators.
  • Should throw on invalid or unsupported schema style.
  • Should work in backend or frontend with no additional dependencies

RawModel should provide its own schema structure which follows and can be created from JSON Schema. A schema in RawModel are basically arrays of property definitions.

export interface Schema {
  title: string;
  description?: string;
  config?: SchemaConfig;
  props: SchemaProp[];
}
export interface SchemaConfig {
  failFast?: boolean;
}
export interface SchemaProp {
  title: string;
  cast?: PropCast;
  defaultValue?: any;
  fakeValue?: any;
  emptyValue?: any;
  validate?: ValidatorRecipe[];
  populatable?: string[];
  serializable?: string[];
}
export interface PropCast {
  array: boolean;
  handler: 'String' | 'Boolean' | 'Integer' | 'Float' | 'Number' | 'Date' | Schema;
}

The new interface should look like this:

export function parseJsonSchema(json) {
    ...
}
export function createModelClass(json: Schema) {
    const Model = eval(`class ${json.title} extends Model {}`);
    json.properties.forEach((prop) => {
        Model.addProp(prop);
    });
    return Model;    
}
import { parseJsonSchema, createModelClass } from '@rawmodel/schema';

// Parse JSON schema into RawModel Schema.
const schema = parseJsonSchema({ ...JSON_SCHEMA... });

// Create a new Model class object.
const User = createModelClass(schema);

Refs: #24, #15

Implemented in #62.