Skip to content

API self-documentation #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tobyzerner opened this issue Jan 21, 2021 · 3 comments
Closed

API self-documentation #17

tobyzerner opened this issue Jan 21, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@tobyzerner
Copy link
Owner

tobyzerner commented Jan 21, 2021

$type->attribute('firstName')
    ->type('string')
    ->description("The user's first name.");

$type->attribute('createdAt')
    ->type(DateTime::class)
    ->description('The time at which the user was created.');
@tobyzerner tobyzerner added the enhancement New feature or request label Jan 21, 2021
@tobyzerner
Copy link
Owner Author

Part of this is attribute types. Perhaps this could be done with alias methods that define a type description, validator, ± a transformer:

$type->attribute('firstName')
    ->required()
    ->string();

// behind the scenes, equivalent to:
$type->attribute('firstName')
    ->validate(function ($fail, $value) {
        if (empty($value)) {
            $fail('This field is required');
        }
    })
    ->type('string')
    ->validate(function ($fail, $value) {
        if (! is_string($value)) {
            $fail('This field must be a string');
        }
    });
$type->attribute('updatedAt')
    ->datetime();

// behind the scenes, equivalent to:
$type->attribute('updatedAt')
    ->type('datetime')
    ->validate(function ($fail, $value) {
        if (! DateTime::createFromFormat(DateTime::ISO8601, $value)) {
            $fail('This field must be a date in ISO8601 format');
        }
    })
    ->transform(function ($value) {
        return DateTime::createFromFormat(DateTime::ISO8601, $value);
    });
$type->attribute('status')
    ->enum(['draft', 'active', 'archived']);

// behind the scenes, equivalent to:
$type->attribute('updatedAt')
    ->type("'draft' | 'active' | 'archived'")
    ->validate(function ($fail, $value) {
        if (! in_array($value, ['draft', 'active', 'archived'])) {
            $fail('Must be one of: draft, active, archived');
        }
    });

@tobyzerner
Copy link
Owner Author

Some changes will also be necessary to allow endpoint documentation:

$type->listable()
    ->description('blah')
    ->meta('foo', 'bar');

@tobyzerner
Copy link
Owner Author

Closing in favour of #62

@tobyzerner tobyzerner closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant