Use Statamic Blueprint to validate Collection Entry data

When programmatically creating or updating a Collection Entry, you can lean on the Collection’s Blueprint to help you validate the fields values:

1<?php
2 
3use Statamic\Facades\Blueprint;
4use Statamic\Facades\Entry;
5 
6$data = [
7 'slug' => 'some-title',
8 'title' => 'Some title',
9 // more data
10];
11 
12// validate the data
13$validated_data = Blueprint::setDirectory('resources/blueprints/collections/my_collection')
14 ->find('my_collection')
15 ->fields()
16 ->addValues($data)
17 ->validator()->validate();
18 
19// create & save the entry with the validated data
20$entry = Entry::make()
21 ->collection('my_collection')
22 ->slug(data_get($validated_data, 'slug'))
23 ->published()
24 ->data($validated_data);
25 
26$entry->save();