Home > database >  Json schema of interface - serialization missing some fields
Json schema of interface - serialization missing some fields

Time:05-18

For this code, where I have an user defined interface and schema definition is guided.

type SchemaDefinition<T> = {
  [K in keyof T]: {
    type: { new(): unknown } //
    required?: boolean
  }
}

class Schema<T> {
  constructor(public schema: SchemaDefinition<T>) {}

  validate(obj: T): boolean {
    for (const prop of Object.keys(this.schema) as (keyof T)[]) {
      if (!(obj[prop] instanceof this.schema[prop].type)) return false
      if (this.schema[prop].required && obj[prop] == null) return false
    }

    return true
  }
}

interface IUser {
  name: string;
  email: string;
}

There are two schemas here. 1. for programming language specific container that is IUser interface 2. the one that I want to send it to backend that is composed by Schema object something like

new Schema<IUser>('users',
            {
                name: {type: Number, required: true},
                email: {type: String, required: true},
            });

now I am trying to serialize this Schema object to string using JSON.stringify() but type is skipped, how can I have it serialized or how can I translate this IUser into JSON schema in the best way in TS.

Edit:

I was able to retrieve the type name by doing something like this

const schemaRepresentation = {};
schemaRepresentation['title'] = this._name;
schemaRepresentation['additionalProperties'] = false;
schemaRepresentation['additionalProperties'] = false;

const properties = {};

for (const schemaKey in this.schema) {
  properties[schemaKey.toString()] = this.schema[schemaKey].datatype.name;
}

schemaRepresentation['properties'] = properties

if there is an array field in the interface - how do I get the type of the array?

CodePudding user response:

You just need to use values that are serializable to JSON because String and Number are functions, and therefore are not serializable.

For example, maybe you want to test the typeof obj[prop] for particular string.

type AllowedTypeNames = 'string' | 'number' | 'boolean'

type SchemaDefinition<T> = {
  [K in keyof T]: {
    type: AllowedTypeNames
    required?: boolean
  }
}

And validate would now look like:

  validate(obj: T): boolean {
    for (const prop of Object.keys(this.schema) as (keyof T)[]) {
      if (typeof obj[prop] !== this.schema[prop].type) return false
      // ^ check if the typeof the obj[prop] matches the schema.

      if (this.schema[prop].required && obj[prop] == null) return false
      // ^ check if the typeof the obj[prop] is required and present.
    }

    return true
  }

Which serializes fine:

const userSchema = new Schema<IUser>({
  name: { type: 'string', required: true },
  email: { type: 'string', required: true },
});

console.log(JSON.stringify(userSchema.schema))
// {"name":{"type":"string","required":true},"email":{"type":"string","required":true}}

See playground

  • Related