Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

$ref doesn't work in document root schema #185

Open
dashameter opened this issue Jun 3, 2020 · 5 comments
Open

$ref doesn't work in document root schema #185

dashameter opened this issue Jun 3, 2020 · 5 comments
Labels
bug Something isn't working

Comments

@dashameter
Copy link

Creating a document using JSON:

{
    "note": {
        "$ref": "#/definitions/msg"
    },
    "definitions": {
        "msg": {
            "required": [
                "timestamp"
            ],
            "properties": {
                "timestamp": {
                    "type": "number"
                },
                "description": {
                    "type": "string"
                }
            },
            "additionalProperties": false
        }
    }
}

and setting definitions via

        const definitions = json.definitions;
        delete json.definitions;
        const contract = await platform.contracts.create(json, identity);
        contract.setDefinitions(definitions);
        await platform.dpp.dataContract.validate(contract);

dpp.dataContract.validate(contract) throws


TypeError: Cannot set property '$id' of undefined
    at enrichDataContractWithBaseSchema.js:43
    at Array.forEach (<anonymous>)
    at enrichDataContractWithBaseSchema.js:42
    at Array.forEach (<anonymous>)
    at i (enrichDataContractWithBaseSchema.js:28)
    at c.g [as validateDataContract] (validateDataContractFactory.js:59)

contract.JSON() as output by browser debug console (since node version is broken):

contract: DataContract
definitions:
msg:
additionalProperties: false
properties:
description: {type: "string"}
timestamp: {type: "number"}
required: Array(1)
0: "timestamp"
length: 1
documents:
note:
$ref: "#/definitions/msg"
entropy: "yd8ob63kZQ2c2aKsBgAcsik9SvC6xpLzoZ"
id: "2DEBGqaZDhxA8JSmdvsGUzucW7GLUzDtJKRzkN3teJZy"
ownerId: "32EfBxtPmTDSmdohuRFJ6EUTm92WPgyVojFrxyW9rKbi"
schema: "https://schema.dash.org/dpp-0-4-0/meta/data-contract"
@dashameter dashameter added the bug Something isn't working label Jun 3, 2020
@thephez
Copy link
Contributor

thephez commented Jun 3, 2020

@dashameter The issue with definitions only occurs when they are included in a document and then deleted out? Or definitions fail for you regardless of how you try to use them?

@shumkov
Copy link
Member

shumkov commented Jul 10, 2020

@dashameter You define "definitions" as a document. You need "definitions" sections, not "documents".

@dashameter
Copy link
Author

Using $ref directly at the toplevel:

        note: {
              $ref: '#/definitions/msg'
        }

example script:


const Dash = require('dash')

const clientOpts = {
  network: 'testnet',
    wallet: {
        mnemonic: ''
    }
}
const client = new Dash.Client(clientOpts)

const registerContract = async function() {
  try {
    const platform = client.platform
    const identity = await platform.identities.get('')

    const contractDocumentWithDefinitions = {
      documents: {
        note: {
              $ref: '#/definitions/msg'
        }
      },
      definitions: {
        msg: {
          required: ['timestamp'],
          properties: {
            timestamp: {
              type: 'number'
            },
            description: {
              type: 'string'
            }
          },
          additionalProperties: false
        }
      }
    }
    const { documents, definitions } = contractDocumentWithDefinitions

    const contract = await platform.contracts.create(documents, identity)
    console.dir({ contract })

    // Set contract definitions
    contract.setDefinitions(definitions)
    console.dir({ contract })

    // Make sure contract passes validation checks
    await platform.dpp.dataContract.validate(contract)

    // Sign and submit the data contract
    await platform.contracts.broadcast(contract, identity)
  } catch (e) {
    console.error('Something went wrong:', e)
  } finally {
    client.disconnect()
  }
}

registerContract()

throws the error:

{ contract:
   DataContract {
     id: '13ygEjg1q2iQHuZtyBgtAW9Wmyiufy9j9XVyPWkqKiJF',
     ownerId: '5aAinbP9UKXdWTyywW9C7Qk6u8WbkKDg4gNzYZbhTqfr',
     schema: 'https://schema.dash.org/dpp-0-4-0/meta/data-contract',
     documents: { note: { '$ref': '#/definitions/msg' } },
     definitions:
      { msg:
         { required: [ 'timestamp' ],
           properties:
            { timestamp: { type: 'number' },
              description: { type: 'string' } },
           additionalProperties: false } },
     entropy: 'yaWRRS8j6pQSqaTFSdDcM5i5q6XXJjYRFQ' } }

     
Something went wrong: TypeError: Cannot set property '$id' of undefined
    at Object.keys.forEach (node_modules/@dashevo/dpp/lib/dataContract/enrichDataContractWithBaseSchema.js:42:41)
    at Array.forEach (<anonymous>)
    at Object.keys.forEach (node_modules/@dashevo/dpp/lib/dataContract/enrichDataContractWithBaseSchema.js:41:8)
    at Array.forEach (<anonymous>)
    at enrichDataContractWithBaseSchema (node_modules/@dashevo/dpp/lib/dataContract/enrichDataContractWithBaseSchema.js:28:32)
    at DataContractFacade.validateDataContract (node_modules/@dashevo/dpp/lib/dataContract/validateDataContractFactory.js:59:34)

@thephez
Copy link
Contributor

thephez commented Jul 13, 2020

Try this:

      documents: {
        note: {
          properties: {
            $ref: '#/definitions/msg'
          }
        }
      },

@shumkov
Copy link
Member

shumkov commented Jul 13, 2020

It supposed to work. We will fix it, thank you. Atm, you can try the @thephez’s workaround.

@shumkov shumkov changed the title using definitions as document toplevel properties throws error $ref doesn't work in document root schema Sep 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Development

No branches or pull requests

3 participants