Nesting model validation

Let’s imagine we develop an API for an online video editor. Client sends JSON representing a video clip:




{

    "title": "Tycho live @ KEXP",

    "description": "Tycho performing live in the KEXP studio.",

    "parts": [

        {

            "type": "image",

            "resource": "logos/kexp.png,

            "effect": {

                "type": "fade_in",

                "easing": "linear"

            },

            "duration": 5000

        },

        {

            "type": "video",

            "resource": "cuts/tycho_kexp_camera_02.mpeg",

            "startFrom": 6052,

            "duration": 14000

        },

        {

            "type": "video",

            "resource": "cuts/tycho_kexp_camera_04.mpeg",

            "startFrom": 30232,

            "duration": 32000,

            "effect": {

                "type": "overlay",

                "effect": {

                    "type": "fade_in",

                    "easing": "swing"

                },

                "duration": 10000,

                "resource": {

                    "type": "video",

                    "resource": "cuts/tycho_kexp_camera_01.mpeg",

                    "startFrom": 8323,

                    "duration": 3000

                }

            }

        }

    ]

}



Of course, we have to deal with it like with any other form: filter, validate and save. Let’s have a MongoDB storage for example. Frontend support is not required, but error output must be proper.

What’s your ideas on how to implement it?

You should create a custom validator (via method or via separated class).

You may as well try to use following extension: yii2tech/embedded. In particular: Embedded Validator

Thanks for reply!

I have a simular validator now. Maybe it would make sense if I open source it.