HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

how to write the validation using API format

give me some tips creating validation using controller and model where i write the code of that

Comments

  • R_JR_J Ex-Fanboy Munich Admin

    Please don't get me wrong: I really do not want to be impolite, but what is the question?

    "Validation API format" doesn't exist.

    "Where" to write code depends on what piece of software you are creating. Tell us what you have and what you want to achieve and we might be able to support

    "Example for validating, controller, model" - Vanilla has a great inline documentation, so if you look at any of the included models, you should be able to find examples. If you cannot understand them, show the code and ask a specific question


    In order to help you, you need to help us to better understand what exactly you are asking for. I'm always happy to provide code snippets.

  • riyanriyan New
    edited March 2019

    Thank you for replying..

    Sorry it's my mistake how to write the error validation on API using validation PHP

  • charrondevcharrondev Developer Lead (PHP, JS) Montreal Vanilla Staff

    If you want assistance here, you’ll need to provide more information. I. An only guess the kind of vague area you’re asking about but maybe this will help you?


    Looking at existing classes is quite useful as well. You can try and look at any ApiController in the app to see what’s going on.

  • R_JR_J Ex-Fanboy Munich Admin

    Please take a look at e.g. the DiscussionsApiController and /vendor/vanilla/garden-schema/src/Schema.php

    Simply by looking at that I could see that validation uses the Schema class and the DiscussionsApiController has quite a lot of examples. This is what I came up with:

    <?php
    
    use Garden\Schema\Schema;
    use Garden\Web\Data;
    
    class AahApiController extends AbstractApiController {
       public function post(array $data) {
           $in = $this->schema(
               [
                   'id:i' => 'Some numerical ID. Required.',
                   'name:s?' => 'Some name. Optional because of the "?".',
               ],
               'in'
           );
    
           $result = $in->validate($data);
    
           return new Data($result);
       }
    
  • charrondevcharrondev Developer Lead (PHP, JS) Montreal Vanilla Staff

    @R_J I'll note best practice for these controllers (as documented) is to always have a `$this->permission()` call even if it's an empty string. This allows "default" permissions to be enforced (if the site is configured to have escalated default permissions).

  • R_JR_J Ex-Fanboy Munich Admin
    edited March 2019

    I do not really have read the documentation, I just wanted to quickly assemble some working code. Thanks for giving that extra information. But now that you have mentioned it, I have taken a look at the Schema docs and I have found that:

    $schema = new Garden\Schema();
    $schema([
        's:name' => 'The title of the discussion.',
        's:body' => 'The body of the discussion.',
        's:format?' => 'The discussion format.',
        'i:categoryID' => 'The category to place the discussion.',
    ])
    ->setDescription('Add a new discussion.')
    ->validate($body)
    

    In this example the type shortcut and the name are the other way around. That looks like an error...


    I also cannot see what 'score:i|n' => 'Total points associated with this post.', would mean (taken from the DiscussionsApiController). What does this "|n" stands for?

  • charrondevcharrondev Developer Lead (PHP, JS) Montreal Vanilla Staff

    Hmmm. That documentation is definitely incorrect. The scheme is its own package though and has better documentation it’s github repo.


Sign In or Register to comment.