Skip to content

Is valid data for custom api

  • To validate the request body data of the custom API we should use this API.

Request Method: POST

URL

/api/system-api/user-path/is-valid-data-for-custom-api

  • Provide custom API name, type and it's data.

Custom API settings

  • Provide 'reqBodySchema' object within the custom API setting as shown below.
let customApi: T.ICustomApiSettingsTypes = {
    reqBodySchema: {
        first_name: {
            __type: T.EType.string,
            validations: {
                required: true
            }
        },
        phone: {
            __type: T.EType.number,
            validations: {
                required: true,
                min: 5,
                max: 999
            }
        }
    }
}

Request body

  • In the 'is-valid-data-for-custom-api' system API give the request body as shown below.
  • Here, 'type' can be 'BODY | QUERY_PARAMS'
1
2
3
4
5
6
7
{
    "name": "custom_api_name",
    "type": "BODY",
    "data": {
        "some": "data"
    }
}

Validate custom API body object

Test custom API data valid or not using global object 'g'.

1
2
3
4
5
6
7
8
await g.sys.system.isValidDataForCustomAPI({
    name: "custom_api_name",
    data: {
        "some": "data",
        "some": "data"
    },
    type: "BODY" // BODY | QUERY_PARAMS
});

API response

  • If we do not provide the 'data' object we will get the below response.
{
    "success": false,
    "statusCode": 400,
    "errors": [
        {
            "type": "required",
            "field": "first_name",
            "message": "Please provide valid 'first_name' field with type 'string'.",
            "code": 400
        },
        {
            "type": "required",
            "field": "phone",
            "message": "Please provide valid 'phone' field with type 'number'.",
            "code": 400
        }
    ]
}