Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
Json schema of interface - serialization missing some fields

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?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda