feathers-plus / generator-feathers-plus

A Yeoman generator to (re)generate a FeathersJS application supporting both REST and GraphQL architectural concepts and their query languages.
https://generator.feathers-plus.com/
Other
44 stars 30 forks source link

Default value not applied when using with $ref #273

Closed josephstgh closed 4 years ago

josephstgh commented 5 years ago

Steps to reproduce

When using $ref, I am unable to define a default value for the field.

Say I have the following configuration.

common.json

{
  "description": "Common JSON-schema definitions.",
  "type": "object",
  "definitions": {
    "customText": {
      "readOnly": false,
      "type": "string"
    }
  }
}

<service>.schema.ts

// Fields in the model.
  properties: {
    // !code: schema_properties
    name: {},
    phone: { type: 'number' },
    createdAt: { $ref: 'common.json#/definitions/created_at' },
    firstName: { $ref: 'common.json#/definitions/customText', default: 'sam' },
    lastName: { default: 'pahc' },
    // !end
  },

After generating the service, here's what generated in <service>.mongoose.ts.

let moduleExports = merge({},
  // !<DEFAULT> code: model
  {
    name: String,
    phone: Number,
    createdAt: Date,
    firstName: String,
    lastName: {
      type: String,
      default: "pahc"
    }
  },
  // !end
  // !code: moduleExports // !end
);

Am I setting it wrongly, or I am not able to create a default value for $ref?

Expected behavior

The generated <service>.mongoose.ts. (or any DB generated file) should be

let moduleExports = merge({},
  // !<DEFAULT> code: model
  {
    name: String,
    phone: Number,
    createdAt: Date,
    firstName: {
      type: String,
      default: "sam" // Will take the default value set in `schema.ts` instead of ignoring it
    }
    lastName: {
      type: String,
      default: "pahc"
    }
  },
  // !end
  // !code: moduleExports // !end
);

This does not work for setting default: Date.now and other types as well.