apiaryio / api-blueprint

API Blueprint
https://apiblueprint.org
MIT License
8.64k stars 2.14k forks source link

String variable? #380

Closed basickarl closed 7 years ago

basickarl commented 7 years ago

I'm embarrassed to ask this, but how does one create a simple string variable?

I'd like to fill the array below with a string:

+ Response 401 (application/vnd.api+json)

    Something back.

    + Attributes (object)
        + errors (array[My Var Here, My Second Var Here])

I'd like the response to look like the following in JSON:

{
  "errors": [
    "My var value here!",
    "My second var value here!"
  ]
}
YamiOdymel commented 7 years ago

Maybe you are searching for array[string]?

+ Response 401 (application/vnd.api+json)

    Something back.

    + Attributes (object)
        + errors (array[string])
kylef commented 7 years ago

You can also add the two sample values, for example:

+ Response 401 (application/vnd.api+json)
    + Attributes (object)
        + errors (array)
            + `My var value here!`
            + `My second var value here!`

screen shot 2017-02-13 at 08 26 27

screen shot 2017-02-13 at 08 26 33

DScheglov commented 11 months ago

I'd like to reopen the issue.

yes, it is possible to specify the "example values" in this way, but the schema is generated without item type:

  + Response 422 (application/json)

    + Attributes
        + `errors` (array, required) - List of Validation Errors
            + `password couldn't be empty` (string)
            + `email must be a valid email` (string)

The Body Example:

{
  "errors": [
    "password couldn't be empty",
    "email must be a valid email"
  ]
}

The json-schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "errors": {
      "type": "array",
      "description": "List of Validation Errors"
    }
  },
  "required": [
    "errors"
  ]
}

So, the schema must be fixed.

kylef commented 11 months ago

@DScheglov What type of schema are you looking for, could you provide an example please?

DScheglov commented 11 months ago

Hey, @kylef

The full sample is bellow:

FORMAT: 1A

# User Management

## Sign Up [POST /sign-up]

Creates a new user

+ Request (application/json)

    + Attributes (object)
        + `email`: `example@not-email.server.com` (string, required) - User E-Mail
        + `password`: `SomeRandomPa55word!` (string, required) - User Password

+ Response 200 (application/json)

    + Attributes
        + One Of
            + data (object, fixed-type)
                + `id`: `79d3d4a4-491f-4f07-b971-a4891d6e0208` (string, required) - Id of User Account
                + `email`: `example@not-email.server.com` (string, required) - User E-Mail
                + `token`: `<some-jwt>` (string, required) - Access Token
                + `refreshToken`: `<some-jwt>` (string, required) - Refresh Token
            + error (object, fixed-type)
                + code: `EPASSWORD_TOO_SHORT` (enum, required) - The Error Code
                    + `EPASSWORD_TOO_SHORT`
                    + `EPASSWORD_TOO_SIMPLE`
                    + `EPASSWORD_TOO_WIDELY_USED`
                    + `EUSER_IS_ALREADY_REGISTERED`
                + message: `Password is too short` (string, required) - The Error Message

+ Response 400

        Bad Request

+ Response 404

        Not Found

+ Response 422 (application/json)

    + Attributes
        + `errors` (array[string], required) - List of Validation Errors
            + `password couldn't be empty` (string)
            + `email must be a valid email` (string)

+ Response 500

        Internal Error

The screenshot from the Apiary Editor Screenshot from 2023-08-14 16-16-42

kylef commented 11 months ago

and what JSON Schema are you looking for? I'm asking for the schema as its not clear what you are trying to achieve.

DScheglov commented 11 months ago

@kylef oh, my bad ...

The expected schema is:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "errors": {
      "type": "array",
      "items": {
         "type": "string"
      },
      "description": "List of Validation Errors"
    }
  },
  "required": [
    "errors"
  ]
}

The items is described here: https://json-schema.org/understanding-json-schema/reference/array.html#items

DScheglov commented 11 months ago

Oh, actually it seems it is another bug, doesn't related the sampling for string arrays, because the items is not generated even if samples are not specified.

Sorry for that.

kylef commented 11 months ago

If you use the fixed-type keyword on the array definition an items fixing the type to string should end up in the schema:

## Errors (object)
+ `errors` (array[string], fixed-type, required) - List of Validation Errors
    + `password couldn't be empty` (string)
    + `email must be a valid email` (string)

The documentation for this part can be found in: https://apiblueprint.org/documentation/mson/cheatsheet.html#arrays

I will note that there will be two differences between the generated schema and your example:

DScheglov commented 11 months ago

@kylef ,

Thank you for fast answer.

But it seems excessive to specify fixed-type after array[Type], or not? If someone doesn't want to specify the items type they can just omit it and specify just array.

So, actually it is just an opinion.

Thank you for your answser and for the Project.