edge schema validation

This commit is contained in:
2024-03-30 18:02:17 +02:00
parent 0e19c38f23
commit 49c1d5efd0
7 changed files with 43 additions and 16 deletions
+24 -2
View File
@@ -6,15 +6,21 @@ use Lucent\Graph\Record\Contracts\RecordEdgeData;
use Lucent\Graph\Record\Record; use Lucent\Graph\Record\Record;
use Lucent\LucentException; use Lucent\LucentException;
use Lucent\Query\Query; use Lucent\Query\Query;
use Lucent\Schema\Schema\Schema;
use Lucent\Schema\Validator\DataValidator;
use Lucent\Support\Collection; use Lucent\Support\Collection;
use Lucent\Support\Option\Option;
use Lucent\Support\Result\Error;
use Lucent\Support\Result\Result; use Lucent\Support\Result\Result;
use Lucent\Support\Result\Success; use Lucent\Support\Result\Success;
class EdgeService class EdgeService
{ {
public function __construct( public function __construct(
public EdgeRepo $edgeRepo, public EdgeRepo $edgeRepo,
public Mapper $mapper, public Mapper $mapper,
public DataValidator $recordValidator,
public ChannelService $channelService,
) )
{ {
} }
@@ -35,9 +41,25 @@ class EdgeService
public function update(EdgeData $data) :Result{ public function update(EdgeData $data) :Result{
$edge = $this->mapper->fromArray(toArray($data)); $edge = $this->mapper->fromArray(toArray($data));
$schema = $this->getEdgeDataSchema($edge);
$errors = $this->recordValidator->check($schema->get(), $edge->data);
if ($errors->isNotEmpty()) {
return Error::create($errors);
}
$this->edgeRepo->update($edge); $this->edgeRepo->update($edge);
return Success::create($edge); return Success::create($edge);
} }
/**
* @param Edge $edge
* @return Option<Schema>
*/
private function getEdgeDataSchema(Edge $edge): Option
{
$edgeSchemaName = $this->channelService->getSchema($edge->sourceSchema)->get()->fields->where("name", $edge->field)->first()->data;
return $this->channelService->getSchema($edgeSchemaName);
}
// //
// /** // /**
// * @throws LucentException // * @throws LucentException
-1
View File
@@ -39,7 +39,6 @@ class Mapper
private function getEdgeDataSchema(Edge $edge): string private function getEdgeDataSchema(Edge $edge): string
{ {
return $this->channelService->getSchema($edge->sourceSchema)->get()->fields->where("name", $edge->field)->first()->data; return $this->channelService->getSchema($edge->sourceSchema)->get()->fields->where("name", $edge->field)->first()->data;
} }
} }
+5 -4
View File
@@ -19,7 +19,7 @@ use Lucent\Query\Query;
use Lucent\Revision\RevisionService; use Lucent\Revision\RevisionService;
use Lucent\Schema\Field\FieldDataInterface; use Lucent\Schema\Field\FieldDataInterface;
use Lucent\Schema\Schema\Schema; use Lucent\Schema\Schema\Schema;
use Lucent\Schema\Validator\Validator; use Lucent\Schema\Validator\DataValidator;
use Lucent\Schema\Validator\ValidatorError; use Lucent\Schema\Validator\ValidatorError;
use Lucent\Schema\Validator\ValidatorException; use Lucent\Schema\Validator\ValidatorException;
use Lucent\Support\Collection; use Lucent\Support\Collection;
@@ -34,7 +34,7 @@ readonly class RecordService
private AuthService $authService, private AuthService $authService,
private RevisionService $revisionService, private RevisionService $revisionService,
private ChannelService $channelService, private ChannelService $channelService,
private Validator $recordValidator, private DataValidator $recordValidator,
private Query $query, private Query $query,
private InputFormatter $inputFormatter, private InputFormatter $inputFormatter,
private RecordRepo $recordRepo, private RecordRepo $recordRepo,
@@ -62,7 +62,7 @@ readonly class RecordService
); );
if ($data->status === Status::PUBLISHED) { if ($data->status === Status::PUBLISHED) {
$errors = $this->recordValidator->check($data->schemaName, $record->data); $errors = $this->recordValidator->check($schema, $record->data);
if ($errors->isNotEmpty()) { if ($errors->isNotEmpty()) {
return Error::create($errors); return Error::create($errors);
} }
@@ -88,7 +88,8 @@ readonly class RecordService
$formattedData = $this->inputFormatter->fill($record->schema, new FieldData($data->data)); $formattedData = $this->inputFormatter->fill($record->schema, new FieldData($data->data));
if ($data->status === Status::PUBLISHED) { if ($data->status === Status::PUBLISHED) {
$errors = $this->recordValidator->check($record->schema, $record->data); $schema = $this->channelService->getSchema($record->schema)->get();
$errors = $this->recordValidator->check($schema, $record->data);
if ($errors->isNotEmpty()) { if ($errors->isNotEmpty()) {
return Error::create($errors); return Error::create($errors);
} }
+5
View File
@@ -28,6 +28,11 @@ class EdgeController extends Controller
data: $request->input("data"), data: $request->input("data"),
rank: $request->input("rank"), rank: $request->input("rank"),
)); ));
if ($res->error()->isDefined()) {
return result($res);
}
return result($res); return result($res);
} }
-1
View File
@@ -233,7 +233,6 @@ class RecordController extends Controller
} }
$schema = $this->channelService->getSchema($record->schema)->get(); $schema = $this->channelService->getSchema($record->schema)->get();
$recordHistory = $this->recordManager->fromSession($request->session())->push($rid)->getRecords($rid);
return $this->svelte->render( return $this->svelte->render(
layout: "channel", layout: "channel",
view: "recordEdit", view: "recordEdit",
+5 -5
View File
@@ -70,11 +70,11 @@ class LucentServiceProvider extends ServiceProvider
if ($this->app->runningInConsole()) { if ($this->app->runningInConsole()) {
$this->commands([ $this->commands([
// CompileSchemas::class, CompileSchemas::class,
// RebuildThumbnails::class, RebuildThumbnails::class,
// LiveLink::class, LiveLink::class,
// RemoveOrphanEdges::class, RemoveOrphanEdges::class,
// GenerateJsonSchema::class, GenerateJsonSchema::class,
]); ]);
} }
@@ -5,11 +5,12 @@ namespace Lucent\Schema\Validator;
use Lucent\Channel\ChannelService; use Lucent\Channel\ChannelService;
use Lucent\Graph\Data\FieldData; use Lucent\Graph\Data\FieldData;
use Lucent\Schema\Field\FieldDataInterface; use Lucent\Schema\Field\FieldDataInterface;
use Lucent\Schema\Schema\Schema;
use Lucent\Support\Collection; use Lucent\Support\Collection;
use Lucent\Support\Option\Option; use Lucent\Support\Option\Option;
class Validator class DataValidator
{ {
@@ -23,12 +24,12 @@ class Validator
* @return Collection<ValidatorError> * @return Collection<ValidatorError>
*/ */
public function check( public function check(
string $schemaName, Schema $schema,
FieldData $data, FieldData $data,
): Collection ): Collection
{ {
$schema = $this->channelService->getSchema($schemaName)->get();
return $schema->getDataFields() return $schema->getDataFields()
->map(fn(FieldDataInterface $f) => $this->validate($f, $data)) ->map(fn(FieldDataInterface $f) => $this->validate($f, $data))
->filter(fn(Option $error) => $error->isDefined()) ->filter(fn(Option $error) => $error->isDefined())