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

140
Vistas
How to a specify a property that is a function in Json Schema

I want to be able to define functions with parameters and return type for my JavaScript object specification. I am hoping to be able to do this using JSONSchema. But, I can't seem to figure out how to do it.

I am trying to do something like the following:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/product.schema.json",
  "title": "Product",
  "description": "A JavaScript object that gets a product from Acme's catalog",
  "type": "object",
  "properties": {
    "getProduct": {
      "description": "returns a product object when a product id is provided as a parameter",
      "type": "function",
      "parameters": ["id": uuid],
      "returnType": object
    }
  },
}

Does anybody know of a project or tool that will allow me to do this? Triple thanks in advance.

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

0

JSON Schema does not have any capacity to describe functions, due to the fact that JSON cannot express functions. JSON Schema is defined over the data model expressible in JSON.

current versions of the JSON Schema specification are quite extensible with the addition of vocabularies. although it would not be consistently interoperable, a vocabulary to describe functions might be a possibility.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can always define functions as properties of JSON objects by key value syntax. like this:

    const Product = {
      id: 50,
      getProduct: function(id){
        return 'product '+id;
      }
    }

But if you want to serialize this JSON to exchange it in some way. you have to override JSON.stringfy replacer function like this

    JSON.stringify(Product, function(key, value){
      if(typeof value === 'function')
        return value.toString();
      else return value;
    });

And if you like to add comments to your JSON, you can add some key/value like _comment:"Type your comment here" to your object.

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