I'm just learn about Loopback from StrongLoop to create a simple API.
I got some trouble to make the API works.
Here is my config :
model.js
--------
'use strict';
module.exports = function(Account) {
};
This is my model.json:
{
"name": "Account",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "string",
"required": true,
"defaultFn": "uuidv4",
"postgresql": {
"columnName": "id",
"dataType": "uuid"
}
},
"code": {
"type": "string",
"required": true,
"postgresql": {
"columnName": "code",
"dataType": "character varying",
"dataLength": "50"
}
},
"name": {
"type": "string",
"required": true,
"postgresql": {
"columnName": "name",
"dataType": "text"
}
},
"type": {
"type": "string",
"required": true,
"postgresql": {
"columnName": "type",
"dataType": "character varying",
"dataLength": "50"
}
},
"isActive": {
"type": "boolean",
"required": true,
"default": true
},
"isDelete": {
"type": "boolean",
"required": true,
"default": false
},
"createdBy": {
"type": "string",
"required": true,
"defaultFn": "uuidv4",
"postgresql": {
"columnName": "createdBy",
"dataType": "uuid"
}
},
"updatedBy": {
"type": "string",
"required": true,
"defaultFn": "uuidv4",
"postgresql": {
"columnName": "updatedBy",
"dataType": "uuid"
}
},
"createdAt": {
"type": "date",
"required": true,
"defaultFn": "now"
},
"updatedAt": {
"type": "date",
"required": true,
"defaultFn": "now"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
The following model have been migrated successfull, I'm using postgresql for my db.
The problem is when Loopback create the CRUD standard API, the POST API seem to not working when I try the POST method. below is my json string when i try to put on API explorer :
{
"id": "123e4567-e89b-12d3-a456-426655440000",
"code": "1000",
"name": "Cash and Bank",
"type": "Assets",
"isActive": true,
"isDelete": false,
"createdBy": "123e4567-e89b-12d3-a456-426655440000",
"updatedBy": "123e4567-e89b-12d3-a456-426655440000",
"createdAt": "2017-09-01T17:00:00.000Z",
"updatedAt": "2017-09-01T17:00:00.000Z"
}
But It throw an error, here is the response body :
{
"error": {
"statusCode": 500,
"name": "error",
"message": "syntax error at end of input",
"length": 92,
"severity": "ERROR",
"code": "42601",
"position": "230",
"file": "scan.l",
"line": "1078",
"routine": "scanner_yyerror",
"stack": "error: syntax error at end of input\n at Connection.parseE (/media/andylpjr/EA7C669F7C6665F3/Project/kpost/kpost-api/node_modules/pg/lib/connection.js:567:11)\n at Connection.parseMessage (/media/andylpjr/EA7C669F7C6665F3/Project/kpost/kpost-api/node_modules/pg/lib/connection.js:391:17)\n at Socket.<anonymous> (/media/andylpjr/EA7C669F7C6665F3/Project/kpost/kpost-api/node_modules/pg/lib/connection.js:129:22)\n at emitOne (events.js:96:13)\n at Socket.emit (events.js:191:7)\n at readableAddChunk (_stream_readable.js:178:18)\n at Socket.Readable.push (_stream_readable.js:136:10)\n at TCP.onread (net.js:561:20)"
}
}
For the note when I'm try from the pgadmin insert to table using SQL query it works. Something like "INSERT INTO Account VALUES (bla bla bla)". I know this maybe just a common mistake, please help. Sorry for bad english.
Well case closed, it appears I'm missing my model definition for primary key, what a shame.. Thanks guys.
Related
I am creating an API using Loopback, and I created a model. One of the properties is "depth", and inside it, it should have 4 other number properties. So I made depth an array of numbers, but it appears like this in my fish.json file:
"depth": {
"type": [
"number"
],
"required": true
},
Here is the entire file (fish.json):
{
"name": "Fish",
"plural": "fish",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"scientific": {
"type": "string",
"required": true
},
"environment": {
"type": "string",
"required": true
},
"minClimate": {
"type": "number",
"required": true
},
"maxClimate": {
"type": "number",
"required": true
},
"depth": {
"type": [
"number"
],
"required": true
},
"avLength": {
"type": "number",
"required": true
},
"maxLength": {
"type": "number",
"required": true
},
"avWeight": {
"type": "number",
"required": true
},
"maxWeight": {
"type": "number",
"required": true
},
"maxAge": {
"type": "number",
"required": true
},
"description": {
"type": "string",
"required": true
},
"imageUrl": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
I have looked up in the Loopback docs, and it looks like I can use a nested model here. How would I go about changing this array to a nested model? Do I need to create a new model, and then somehow add that new model into the depth property?
Basically, I want the JSON to look like this:
"depth": {
"min": "0",
"max": "0",
"avMin": "0",
"avMax": "0"
},
You may have try this:-
"depth": {
"min": {"type": "number"},
"max": {"type": "number"},
"avMin": {"type": "number"},
"avMax": {"type": "number"}
}
I'm trying to use an API but the documentation is really bad. I got this JSON schema but I don't understand it. What am I supposed to include in the request?
url: https://mpc.getswish.net/qrg-swish/api/v1/prefilled
I have tried this but it doesn't work:
{
"payee":{
"editable":{
"editable":"false"
},
"swishString":{
"value":"0721876507"
}
},
"size":600,
"border":20,
"transparent":false,
"format":"png"
}
Here's the JSON schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Swish pre-filled qr code generator",
"description": "REST interface to get a QR code that the Swish app will interpret as a pre filled code",
"definitions": {
"editable": {
"description ": "Controls if user can modify this value in Swish app or not",
"type": "object",
"properties": {
"editable": {
"type": "boolean",
"default": false
}
}
},
"swishString": {
"type": "object",
"properties": {
"value": {
"type": "string",
"maxLength": 70
}
},
"required": [
"value"
]
},
"swishNumber": {
"type": "object",
"properties": {
"value": {
"type": "number"
}
},
"required": [
"value"
]
}
},
"type": "object",
"properties": {
"format": {
"enum": [
"jpg",
"png",
"svg"
]
},
"payee": {
"description": "Payment receiver",
"allOf": [
{
"$ref": "#/definitions/editable"
},
{
"$ref": "#/definitions/swishString"
}
]
},
"amount": {
"description": "Payment amount",
"allOf": [
{
"$ref": "#/definitions/editable"
},
{
"$ref": "#/definitions/swishNumber"
}
]
},
"message": {
"description": "Message for payment",
"allOf": [
{
"$ref": "#/definitions/editable"
},
{
"$ref": "#/definitions/swishString"
}
]
},
"size": {
"description": "Size of the QR code. The code is a square, so width and height are the same. Not required is the format is svg",
"value": "number",
"minimum": 300
},
"border": {
"description": "Width of the border.",
"type": "number"
},
"transparent": {
"description": "Select background color to be transparent. Do not work with jpg format.",
"type": "boolean"
}
},
"required": [
"format"
],
"anyOf": [
{
"required": [
"payee"
]
},
{
"required": [
"amount"
]
},
{
"required": [
"message"
]
}
],
"additionalProperties": false,
"maxProperties": 5
}
The API should return a QR code.
To be honest, I have not taken the time to learn JSON schema, but your example should probably look something like this:
{
"payee": {
"value": "0721876507",
"editable": false
},
"size": 600,
"border": 20,
"transparent": false,
"format": "png"
}
There are other parameters you may choose to utilize:
{
"payee": {
"value": "1239006032",
"editable": false
},
"message": {
"value": "LIV",
"editable": true
},
"amount": {
"value": 100,
"editable": true
},
"format": "png",
"size": 300,
"border": 0,
"transparent": true
}
Honestly, I think the developers behind the Swish APIs are trying to look smart by complicating things. They should, of course, have provided example JSON data instead of forcing consumers to understand their JSON schema. Also, I believe their published schema is wrong. The second example I provided works even though it doesn't validate according to the JSON schema ("Object property count 7 exceeds maximum count of 5").
Here is a minimal and pretty useless request that returns a valid QR-code
{
"format": "png",
"size": 300
}
And here is a more usable example that works
{
"format": "png",
"size": 300,
"transparent": false,
"amount": {
"value": 999.99,
"editable": true
},
"payee": {
"value": "0701000000",
"editable": false
},
"message": {
"value": "Hello",
"editable": false
}
}
Using Draft-07
What I got was
valid JSON
What error I expected from audit object was
directory: String length must be greater than or equal to 2
Tried two different validators with same results
https://www.jsonschemavalidator.net/
GoLang https://github.com/xeipuuv/gojsonschema
This is my schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ISAM-Wrapper",
"description": "Validate isam wrapper json",
"type": "object",
"properties": {
"directory": {
"description": "path to location of isam file",
"type": "string",
"minLength": 2
},
"isamFile": {
"description": "isam database file",
"type": "string",
"minLength": 4
},
"isamIndex": {
"description": "isam index file",
"type": "string",
"minLength": 4
},
"port": {
"description": "port number for REST listener",
"type": "integer",
"minimum": 60410,
"maximum": 69999
},
"actions": {
"description": "Which operations are supported",
"type": "object",
"items": {
"properties": {
"create": {
"type": "boolean"
},
"read": {
"type": "boolean"
},
"update": {
"type": "boolean"
},
"delete": {
"type": "boolean"
}
}
},
"required": [
"create",
"read",
"update",
"delete"
]
},
"fields": {
"description": "each object describes one field of the isam file",
"type": "array",
"minItems": 1,
"items": {
"title": "field",
"description": "field schema",
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"ordinal": {
"type": "integer",
"minimum": 0
},
"offset": {
"type": "integer",
"minimum": 0
},
"length": {
"type": "integer",
"minimum": 1
},
"dataType": {
"enum": [
"uchar",
"ulong",
"long",
"uint",
"int",
"ushort",
"short"
]
}
},
"required": [
"name",
"ordinal",
"offset",
"length",
"dataType"
]
}
},
"audit": {
"description": "input needed to enable and configure isam auditing",
"type": "object",
"items": {
"properties": {
"enable": {
"enum": [
true,
false
]
},
"directory": {
"type": "string",
"minLength": 2
},
"fileName": {
"type": "string",
"minLength": 4
},
"workDirectory": {
"type": "string",
"minLength": 2
},
"archiveDirectory": {
"type": "string",
"minLength": 2
},
"interval": {
"type": "integer",
"minimum": 1
},
"byteThreshold": {
"type": "integer",
"minimum": 1048576,
"maximum": 1073741824
}
}
},
"required": [
"enable"
],
"if": {
"not": {
"properties": {
"enable": {
"enum": [
false
]
}
}
}
},
"then": {
"required": [
"directory",
"fileName",
"workDirectory",
"archiveDirectory",
"interval",
"byteThreshold"
]
}
}
},
"required": [
"directory",
"isamFile",
"isamIndex",
"port",
"actions",
"fields",
"audit"
]
}
This is my JSON
{
"directory": "./",
"isamFile": "isam.dat",
"isamIndex": "isam.idx",
"port": 60410,
"actions": {
"create": true,
"read": true,
"update": true,
"delete": true
},
"fields": [
{
"name": "F1",
"ordinal": 0,
"offset": 0,
"length": 4,
"dataType": "ulong"
},
{
"name": "F2",
"ordinal": 1,
"offset": 4,
"length": 4,
"dataType": "ulong"
}
],
"audit": {
"enable": true,
"directory": "",
"fileName": "file",
"workDirectory": "./work",
"archiveDirectory": "./archive",
"interval": 5,
"byteThreshold": 1500000
}
}
This issue you have is that your schema is invalid. For both actions and audit you specify these as objects but you don't provide any properties. What you do do, however, is specify an items key (which does nothing here - that's a key on an array) which contains the properties.
Once you correct this error, the schema behaves as you intend, see https://repl.it/repls/BlankWellmadeFrontpage
I created a task that talks to a REST API to retreive the values for 2 picklists.
Filling the first dropdown box works fine, when using just the jsonpath.
Based on the first picklist I'd like to retreive values of the second list.
I've tried some variations and I'm trying something like this:
The json which I receive in the first rest call is similar to:
{
"id": "45",
"href": "https://selfservice/api/configurations/45/",
"name": "Type",
"description": "",
"version": "0.0.4",
"attributes": [
{
"value": {
"sdk-object": {
"type": "ConfigurationElement",
"id": "56"
}
},
"type": "ConfigurationElement",
"name": "win"
},
{
"value": {
"sdk-object": {
"type": "ConfigurationElement",
"id": "57"
}
},
"type": "ConfigurationElement",
"name": "lin"
}
]
}
I try to show the attributes name in the list and need the id of the attribute in the second picklist.
I created the following datasourcebindings in the task.json. Of course, the targets exist in the task.
task.json:
{
"id": "GUID",
"name": "Spinup",
"friendlyName": "Create environment",
"description": "Starts Workflow to create an environment. ___ The following placeholders are created deplending on the template and can be used in the rest of the release:**XLDEnvironment** and **testAgentHostname**",
"category": "Deploy",
"author": "***",
"version": {
"Major": 0,
"Minor": 0,
"Patch": 33
},
"minimumAgentVersion": "1.95.3",
"inputs": [
{
"label": "Endpoint",
"name": "connectedServiceName",
"required": true,
"type": "connectedService:server",
"helpMarkDown": "endpoint to connect to."
},
{
"name": "stage",
"type": "string",
"label": "Stage",
"defaultValue": "$(Release.EnvironmentName)",
"required": true,
"helpMarkDown": "Stage of the release, default value is based on the pipeline."
},
{
"name": "releaseName",
"type": "string",
"label": "Environment name",
"defaultValue": "$(Release.ReleaseName)",
"required": true,
"helpMarkDown": "Name of the environment that will be created."
},
{
"name": "owner",
"type": "string",
"label": "Owner of the environment",
"defaultValue": "***",
"required": true,
"helpMarkDown": "It is common to use the initials of the product owner in this field. There has to be a valid AD user owner of each environment. This can also be set by using a variable through the pipeline."
},
{
"name": "group",
"type": "string",
"label": "Group of the owner",
"defaultValue": "",
"required": true,
"helpMarkDown": "group that owns the environment. groups can be requested by the infrastructure department. Example: ****"
},
{
"name": "platform",
"type": "pickList",
"label": "Platform (OS)",
"defaultValue": "",
"required": true,
"helpMarkDown": "Choose the type of the target platform."
},
{
"name": "size",
"type": "pickList",
"label": "Environment Template",
"defaultValue": "",
"required": true,
"helpMarkDown": "Size of the environment to create."
}
],
"dataSourceBindings": [
{
"dataSourceName": "GetCEPlatformType",
"endpointId": "$(connectedServiceName)",
"target": "platform",
"selector": "jsonpath:$.attributes[*].name",
"keySelector": "jsonpath:$.attributes[*].value.sdk-object.id"
},
{
"dataSourceName": "GetCEPlatformSize",
"endpointId": "$(connectedServiceName)",
"target": "size",
"parameters": {
"platformTypeId": "$(platform)"
}
}
],
"instanceNameFormat": "Spin up $(size) $(platform) environment",
"execution": {
"PowerShell3": {
"target": "$(currentDirectory)\\task.ps1",
"argumentFormat": "",
"workingDirectory": "$(currentDirectory)"
}
}
}
vss-extension.json:
{
"manifestVersion": 1,
"id": "*****",
"name": "Release Tasks",
"version": "1.0.1",
"publisher": "***",
"targets": [
{
"id": "Microsoft.VisualStudio.Services"
}
],
"description": "Tools to contact ***. Includes a task to spin up a platform and a task to remove the platform.",
"categories": [
"Build and release"
],
"icons": {
"default": "images/extension-icon.png"
},
"files": [
{
"path": "RemoveEnvironment"
},
{
"path": "SpinUpEnvironment"
}
],
"contributions": [
{
"id": "******",
"type": "ms.vss-distributed-task.task",
"targets": [
"ms.vss-distributed-task.tasks"
],
"properties": {
"name": "RemoveEnvironment"
}
},
{
"id": "*********",
"type": "ms.vss-distributed-task.task",
"targets": [
"ms.vss-distributed-task.tasks"
],
"properties": {
"name": "SpinUpEnvironment"
}
},
{
"description": "Service Endpoint type for all connections",
"id": "endpoint-type",
"properties": {
"authenticationSchemes": [
{
"inputDescriptors": [
{
"description": "Username",
"id": "username",
"inputMode": "textbox",
"name": "Username",
"validation": {
"dataType": "string",
"isRequired": true
}
},
{
"description": "Password",
"id": "password",
"inputMode": "passwordbox",
"isConfidential": true,
"name": "Password",
"validation": {
"dataType": "string",
"isRequired": false
}
}
],
"type": "ms.vss-endpoint.endpoint-auth-scheme-basic"
}
],
"dataSources": [
{
"endpointUrl": "api/configurations/*****/",
"name": "GetCEPlatformType",
"resultSelector": "jsonpath:$.attributes[*].name"
},
{
"endpointUrl": "api/configurations/$(platformTypeId)/",
"name": "GetCEPlatformSize",
"resultSelector": "jsonpath:$.attributes[*].name"
}
],
"displayName": "*****",
"helpMarkDown": "Enter the url and credentials to connect to the endpoint.",
"name": "server",
"url": {
"displayName": "Server URL",
"helpText": "Url for the server to connect to."
}
},
"targets": [
"ms.vss-endpoint.endpoint-types"
],
"type": "ms.vss-endpoint.service-endpoint-type"
}
]
}
Sadly I can't get this to work. The datasources exist in the endpoint, but I'm not sure what to do with the resultSelector.
Does anyone have an idea on how to get this to work? The documentation on this isn't too good.
I'm not familiar with mustache, but in the mustache test tool this seems to work.
Thoughts are appreciated!
Try with this in the first data source:
{
"dataSourceName": "dsList1",
"endpointId": "$(connectedServiceName)",
"target": "list1",
"selector": "jsonpath:$.attributes[*].name",
"keySelector": "jsonpath:$.attributes[*].value.sdk-object.id"
}
Update:
You were trying to use "platformTypeId" in vss-extension.json file while it was defined in task.json, this cause the error message you mentioned in the comment.
By the way, I just noticed that you are using "dataSourceBindings", then you cannot use "selector" and "KeySelector" to parse the result since they are used for "sourceDefinitions".
To achieve the feature you want with "dataSourceBindings", you can definition the endpoint url in the task.json directly instead of defining datasource in endpoint contribution.
So you can move the datasources section in the vss-extension.json file first, and then in the task.json file, change to following:
"dataSourceBindings": [
{
"endpointId": "$(connectedServiceName)",
"endpointURL": "api/configurations/*****/",
"target": "platform",
"resultselector": "jsonpath:$.attributes[*]",
"resultTemplate": "{ \"Value\" : \"{{{value.id}}}\", \"DisplayValue\" : \"{{{name}}}\" }"
},
{
"endpointId": "$(connectedServiceName)",
"endpointURL": "api/configurations/$(platformTypeId)/",
"target": "size"
}
],
I have created a JSON schema following the draft v3 specifications. The schema looks like this:
{
"housePolicies": {
"properties": {
"guaranteePolicies": {
"items": {
"$ref": "#/housePolicies/guaranteePolicy"
},
"type": "array"
},
"specialRequirements": {
"items": {
"$ref": "#/housePolicies/specialRequirement"
},
"type": "array"
},
"chainCode": {
"required": true,
"type": "string",
"description": "Unique identifier of the chain"
},
"losRestrictions": {
"items": {
"$ref": "#/housePolicies/losRestriction"
},
"type": "array"
},
"specialEvents": {
"items": {
"$ref": "#/housePolicies/specialEvent"
},
"type": "array"
},
"propertyCode": {
"required": true,
"type": "string",
"description": "Unique identifier of the property in the chain"
}
},
"specialRequirement": {
"id": "specialRequirement",
"properties": {
"minLOS": {
"type": "integer",
"description": "Minimum stay, in days, that applies for a special requirement restriction.\nOptional: If no input provided, there is no minimum LOS required for that period.",
"format": "int64"
},
"startDate": {
"required": true,
"type": "string",
"description": "Date when a special requirement restriction starts",
"format": "date"
},
"endDate": {
"required": true,
"type": "string",
"description": "Date when a special requirement restriction ends",
"format": "date"
}
}
},
"guaranteePolicy": {
"dow": {
"id": "dow",
"properties": {
"monday": {
"required": true,
"type": "boolean"
},
"tuesday": {
"required": true,
"type": "boolean"
},
"friday": {
"required": true,
"type": "boolean"
},
"wednesday": {
"required": true,
"type": "boolean"
},
"thursday": {
"required": true,
"type": "boolean"
},
"sunday": {
"required": true,
"type": "boolean"
},
"saturday": {
"required": true,
"type": "boolean"
}
}
},
"id": "guaranteePolicy",
"properties": {
"startDate": {
"required": true,
"type": "string",
"description": "Date when a guarantee policy starts",
"format": "date"
},
"endDate": {
"required": true,
"type": "string",
"description": "Date when a guarantee policy ends",
"format": "date"
},
"guaranteeRequiredDow": {
"items": {
"$ref": "#/housePolicies/guaranteePolicy/dow"
},
"required": true
}
}
},
"losRestriction": {
"id": "losRestriction",
"properties": {
"startDate": {
"required": true,
"type": "string",
"description": "Date when a length of stay restriction starts",
"format": "date"
},
"max": {
"type": "integer",
"description": "In case max is not provided it measn that there is no maximum length of stay restrictions.\nOptional: If no input provided, there is no maximum length restriction.",
"format": "int64"
},
"endDate": {
"required": true,
"type": "string",
"description": "Date when a length of stay restriction ends",
"format": "date"
},
"min": {
"type": "integer",
"description": "In case min is not provided it means that there is no minimum length of stay restrictions.\nOptional: If no input provided, there is no minimum length restriction.",
"format": "int64"
}
}
},
"specialEvent": {
"id": "specialEvent",
"properties": {
"startDate": {
"required": true,
"type": "string",
"description": "Date when a special event restriction starts",
"format": "date"
},
"endDate": {
"required": true,
"type": "string",
"description": "Date when a special event restriction ends",
"format": "date"
}
}
},
"id": "housePolicies"
},
"$schema": "http://json-schema.org/draft-03/schema#",
"id": "request",
"properties": {
"housePolicies": {
"items": {
"$ref": "#/housePolicies"
},
"required": true
}
}
}
I am now trying to validate some JSON against it, but jsonschemavalidator complains about the schema, giving error when resolving schema reference '#/housePolicies/guaranteePolicy/dow'. I verified in the documentation that the references are put in the proper format. Could anyone point where is the error in this schema?
You have used json-path in your $ref, but still use an id on the objects. You don't need both and they seem to be conflicting. If you remove the id's, your schema works.