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