-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.sql
More file actions
2685 lines (2148 loc) · 71.7 KB
/
Copy pathcommon.sql
File metadata and controls
2685 lines (2148 loc) · 71.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Disable check on function's body
SET check_function_bodies = off;
--
-- PostgreSQL database dump
--
-- Dumped from database version 15.7 (Ubuntu 15.7-0ubuntu0.23.10.1)
-- Dumped by pg_dump version 15.7 (Ubuntu 15.7-0ubuntu0.23.10.1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: common; Type: SCHEMA; Schema: -; Owner: -
--
CREATE SCHEMA common;
--
-- Name: key_value_list; Type: TYPE; Schema: common; Owner: -
--
CREATE TYPE common.key_value_list AS (
attribute_name text,
idx text,
attribute_value text,
attribute_type text
);
--
-- Name: TYPE key_value_list; Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON TYPE common.key_value_list IS 'Standard representation of a key-value table, with idx for array, and type to better conversion';
--
-- Name: __jsonb_schema__compile_conditional(jsonb, jsonb); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__compile_conditional(p_data jsonb, p_schema jsonb) RETURNS jsonb
LANGUAGE plpgsql
AS $$
declare
error_list text[] := array[]::text[];
temp_errors text[];
if_schema jsonb;
then_schema jsonb;
else_schema jsonb;
condition_valid boolean;
begin
-- se non c'è if, non c'è niente da validare
if not (p_schema ? 'if') then
return p_schema;
end if;
if_schema := p_schema->'if';
then_schema := p_schema->'then';
else_schema := p_schema->'else';
-- rimuovo le parti condizionali
p_schema = p_schema - array['if','then','else'];
-- valuta la condizione if
temp_errors := common.__jsonb_schema__validate(p_data, if_schema);
condition_valid := array_length(temp_errors, 1) is null;
--raise notice '%', condition_valid;
-- applica then o else in base alla condizione
if condition_valid then
-- condizione vera: applica then se presente
if then_schema is not null then
p_schema = common.jsonb_recursive_merge(p_schema, then_schema);
end if;
else
-- condizione falsa: applica else se presente
if else_schema is not null then
p_schema = common.jsonb_recursive_merge(p_schema, else_schema);
end if;
end if;
return p_schema;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__compile_conditional(p_data jsonb, p_schema jsonb); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__compile_conditional(p_data jsonb, p_schema jsonb) IS 'Compila lo schema condizionale: if/then/else.';
--
-- Name: __jsonb_schema__validate(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate(p_data jsonb, p_schema jsonb, p_path text DEFAULT '$'::text) RETURNS text[]
LANGUAGE plpgsql
AS $$
declare
error_list text[] := array[]::text[];
temp_errors text[];
begin
--raise notice '% - %', p_path, p_schema->>'required';
-- valida conditional p_schema (if/then/else)
-- temp_errors := common.__jsonb_schema__validate_conditional(p_data, p_schema, p_path);
-- error_list := error_list || temp_errors;
-- compila conditional p_schema (if/then/else)
if p_schema ? 'if' then
p_schema= common.__jsonb_schema__compile_conditional(p_data, p_schema);
-- raise notice '%', jsonb_pretty(p_schema);
end if;
-- valida il tipo principale
temp_errors := common.__jsonb_schema__validate_type(p_data, p_schema, p_path);
error_list := error_list || temp_errors;
-- valida le proprietà se è un oggetto
if jsonb_typeof(p_data) = 'object' then
if p_schema ? 'properties' then
temp_errors := common.__jsonb_schema__validate_properties(p_data, p_schema, p_path);
error_list := error_list || temp_errors;
end if;
end if;
-- valida i required fields
if p_schema ? 'required' then
temp_errors := common.__jsonb_schema__validate_required(p_data, p_schema, p_path);
error_list := error_list || temp_errors;
end if;
-- valida enum se presente
if p_schema ? 'enum' or p_schema ? 'const' then
temp_errors := common.__jsonb_schema__validate_enum(p_data, p_schema, p_path);
error_list := error_list || temp_errors;
end if;
-- valida format se presente
if p_schema ? 'format' then
temp_errors := common.__jsonb_schema__validate_format(p_data, p_schema, p_path);
error_list := error_list || temp_errors;
end if;
-- valida string constraints
temp_errors := common.__jsonb_schema__validate_string_constraints(p_data, p_schema, p_path);
error_list := error_list || temp_errors;
-- valida number constraints
temp_errors := common.__jsonb_schema__validate_number_constraints(p_data, p_schema, p_path);
error_list := error_list || temp_errors;
-- valida array constraints
temp_errors := common.__jsonb_schema__validate_array_constraints(p_data, p_schema, p_path);
error_list := error_list || temp_errors;
-- valida object constraints
temp_errors := common.__jsonb_schema__validate_object_constraints(p_data, p_schema, p_path);
error_list := error_list || temp_errors;
-- valida logical operators (anyOf, allOf, oneOf, not)
temp_errors := common.__jsonb_schema__validate_logical(p_data, p_schema, p_path);
error_list := error_list || temp_errors;
return error_list;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__validate(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate(p_data jsonb, p_schema jsonb, p_path text) IS 'Valida un documento JSONB contro un JSON schema completo. Include tutte le validazioni: tipo, proprietà, required, enum, format, string/number/array/object constraints e operatori logici.';
--
-- Name: __jsonb_schema__validate_array_constraints(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_array_constraints(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $$
declare
array_length integer;
min_items integer;
max_items integer;
items_schema jsonb;
item_data jsonb;
item_index integer;
current_path text;
temp_errors text[];
error_list text[] := array[]::text[];
unique_check jsonb[];
item jsonb;
begin
-- solo array possono avere array constraints
if jsonb_typeof(p_data) != 'array' then
return error_list;
end if;
array_length := jsonb_array_length(p_data);
-- minItems validation
if p_schema ? 'minItems' then
min_items := (p_schema->>'minItems')::integer;
if array_length < min_items then
error_list := error_list || format('path %s: array length %s is less than minItems %s',
p_path, array_length, min_items);
end if;
end if;
-- maxItems validation
if p_schema ? 'maxItems' then
max_items := (p_schema->>'maxItems')::integer;
if array_length > max_items then
error_list := error_list || format('path %s: array length %s exceeds maxItems %s',
p_path, array_length, max_items);
end if;
end if;
-- uniqueItems validation
if p_schema ? 'uniqueItems' and (p_schema->>'uniqueItems')::boolean = true then
-- costruisce array per controllo unicità
for item in select jsonb_array_elements(p_data)
loop
if item = any(unique_check) then
error_list := error_list || format('path %s: array contains duplicate items', p_path);
exit;
end if;
unique_check := unique_check || item;
end loop;
end if;
-- items validation (p_schema per ogni elemento dell'array)
if p_schema ? 'items' then
items_schema := p_schema->'items';
item_index := 0;
for item_data in select jsonb_array_elements(p_data)
loop
current_path := format('%s[%s]', p_path, item_index);
-- valida ricorsivamente ogni elemento
temp_errors := common.__jsonb_schema__validate(item_data, items_schema, current_path);
error_list := error_list || temp_errors;
item_index := item_index + 1;
end loop;
end if;
return error_list;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__validate_array_constraints(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_array_constraints(p_data jsonb, p_schema jsonb, p_path text) IS '[PRIVATA] Valida i vincoli degli array: minItems, maxItems, uniqueItems, items.';
--
-- Name: __jsonb_schema__validate_conditional(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_conditional(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $$
declare
error_list text[] := array[]::text[];
temp_errors text[];
if_schema jsonb;
then_schema jsonb;
else_schema jsonb;
condition_valid boolean;
begin
-- se non c'è if, non c'è niente da validare
if not (p_schema ? 'if') then
return error_list;
end if;
if_schema := p_schema->'if';
then_schema := p_schema->'then';
else_schema := p_schema->'else';
-- valuta la condizione if
temp_errors := common.__jsonb_schema__validate(p_data, if_schema, p_path);
condition_valid := array_length(temp_errors, 1) is null;
-- applica then o else in base alla condizione
if condition_valid then
-- condizione vera: applica then se presente
if then_schema is not null then
temp_errors := common.__jsonb_schema__validate(p_data, then_schema, p_path);
error_list := error_list || temp_errors;
end if;
else
-- condizione falsa: applica else se presente
if else_schema is not null then
temp_errors := common.__jsonb_schema__validate(p_data, else_schema, p_path);
error_list := error_list || temp_errors;
end if;
end if;
return error_list;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__validate_conditional(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_conditional(p_data jsonb, p_schema jsonb, p_path text) IS '[PRIVATA] Valida gli schemi condizionali JSON schema: if/then/else.';
--
-- Name: __jsonb_schema__validate_enum(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_enum(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $$
declare
error_list text[] := array[]::text[];
begin
if not (p_schema->'enum' @> to_jsonb(p_data)) then
error_list := error_list || format('path %s: value %s not in enum %s',
p_path, p_data::text, (p_schema->'enum')::text);
end if;
if p_schema ? 'const' then
if not (p_schema->'const' @> to_jsonb(p_data)) then
error_list := error_list || format('path %s: value %s not equal to %s',
p_path, p_data::text, (p_schema->>'const'));
end if;
end if;
return error_list;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__validate_enum(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_enum(p_data jsonb, p_schema jsonb, p_path text) IS '[PRIVATA] Valida che il valore sia presente nell''array enum specificato nello p_schema.';
--
-- Name: __jsonb_schema__validate_format(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_format(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $_$
declare
format_type text;
data_text text;
error_list text[] := array[]::text[];
begin
-- solo stringhe possono avere format
if jsonb_typeof(p_data) != 'string' then
return error_list;
end if;
format_type := p_schema->>'format';
data_text := p_data #>> '{}'; -- estrae il valore stringa
case format_type
when 'date-time' then
-- RFC 3339 date-time format
if not (data_text ~ '^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|[+-][0-9]{2}:[0-9]{2})$') then
error_list := error_list || format('path %s: invalid date-time format "%s"', p_path, data_text);
else
-- verifica che sia una p_data valida
begin
perform data_text::timestamp with time zone;
exception
when others then
error_list := error_list || format('path %s: invalid date-time value "%s"', p_path, data_text);
end;
end if;
when 'date' then
-- YYYY-MM-DD format
if not (data_text ~ '^[0-9]{4}-[0-9]{2}-[0-9]{2}$') then
error_list := error_list || format('path %s: invalid date format "%s"', p_path, data_text);
else
begin
perform data_text::date;
exception
when others then
error_list := error_list || format('path %s: invalid date value "%s"', p_path, data_text);
end;
end if;
when 'time' then
-- HH:MM:SS format
if not (data_text ~ '^[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?$') then
error_list := error_list || format('path %s: invalid time format "%s"', p_path, data_text);
else
begin
perform data_text::time;
exception
when others then
error_list := error_list || format('path %s: invalid time value "%s"', p_path, data_text);
end;
end if;
when 'email' then
-- basic email validation
if not (data_text ~ '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$') then
error_list := error_list || format('path %s: invalid email format "%s"', p_path, data_text);
end if;
when 'uri' then
-- basic URI validation
if not (data_text ~ '^[a-zA-Z][a-zA-Z0-9+.-]*:') then
error_list := error_list || format('path %s: invalid uri format "%s"', p_path, data_text);
end if;
when 'uuid' then
-- UUID v4 format
if not (data_text ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$') then
error_list := error_list || format('path %s: invalid uuid format "%s"', p_path, data_text);
else
begin
perform data_text::uuid;
exception
when others then
error_list := error_list || format('path %s: invalid uuid value "%s"', p_path, data_text);
end;
end if;
when 'ipv4' then
-- IPv4 format
if not (data_text ~ '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$') then
error_list := error_list || format('path %s: invalid ipv4 format "%s"', p_path, data_text);
else
begin
perform data_text::inet;
exception
when others then
error_list := error_list || format('path %s: invalid ipv4 value "%s"', p_path, data_text);
end;
end if;
when 'ipv6' then
-- basic IPv6 validation (semplificata)
if not (data_text ~ '^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::1$|^::$') then
error_list := error_list || format('path %s: invalid ipv6 format "%s"', p_path, data_text);
end if;
else
-- format non supportato, ma non è un errore
null;
end case;
return error_list;
end;
$_$;
--
-- Name: FUNCTION __jsonb_schema__validate_format(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_format(p_data jsonb, p_schema jsonb, p_path text) IS 'Valida i formati stringa (date-time, date, time, email, uri, uuid, ipv4, ipv6) secondo le specifiche JSON p_schema.';
--
-- Name: __jsonb_schema__validate_logical(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_logical(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $$
declare
error_list text[] := array[]::text[];
temp_errors text[];
sub_schema jsonb;
valid_count integer;
any_of_valid boolean;
all_of_errors text[];
one_of_valid_count integer;
begin
-- anyOf: almeno uno degli schemi deve essere valido
if p_schema ? 'anyOf' then
any_of_valid := false;
for sub_schema in select jsonb_array_elements(p_schema->'anyOf')
loop
temp_errors := common.__jsonb_schema__validate(p_data, sub_schema, p_path);
if array_length(temp_errors, 1) is null then
any_of_valid := true;
exit; -- non appena uno è valido, esci
end if;
end loop;
if not any_of_valid then
error_list := error_list || format('path %s: does not match any schema in anyOf', p_path);
end if;
end if;
-- allOf: tutti gli schemi devono essere validi
if p_schema ? 'allOf' then
all_of_errors := array[]::text[];
for sub_schema in select jsonb_array_elements(p_schema->'allOf')
loop
temp_errors := common.__jsonb_schema__validate(p_data, sub_schema, p_path);
all_of_errors := all_of_errors || temp_errors;
end loop;
error_list := error_list || all_of_errors;
end if;
-- oneOf: esattamente uno p_schema deve essere valido
if p_schema ? 'oneOf' then
one_of_valid_count := 0;
for sub_schema in select jsonb_array_elements(p_schema->'oneOf')
loop
temp_errors := common.__jsonb_schema__validate(p_data, sub_schema, p_path);
if array_length(temp_errors, 1) is null then
one_of_valid_count := one_of_valid_count + 1;
end if;
end loop;
if one_of_valid_count = 0 then
error_list := error_list || format('path %s: does not match any schema in oneOf', p_path);
elsif one_of_valid_count > 1 then
error_list := error_list || format('path %s: matches %s schemas in oneOf, expected exactly 1',
p_path, one_of_valid_count);
end if;
end if;
-- not: lo p_schema NON deve essere valido
if p_schema ? 'not' then
temp_errors := common.__jsonb_schema__validate(p_data, p_schema->'not', p_path);
if array_length(temp_errors, 1) is null then
error_list := error_list || format('path %s: should not be valid against the "not" schema', p_path);
end if;
end if;
return error_list;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__validate_logical(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_logical(p_data jsonb, p_schema jsonb, p_path text) IS '[PRIVATA] Valida gli operatori logici JSON schema: anyOf, allOf, oneOf, not.';
--
-- Name: __jsonb_schema__validate_number_constraints(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_number_constraints(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $$
declare
data_number numeric;
minimum_val numeric;
maximum_val numeric;
exclusive_min numeric;
exclusive_max numeric;
multiple_of_val numeric;
error_list text[] := array[]::text[];
begin
-- solo numeri possono avere number constraints
if jsonb_typeof(p_data) not in ('number') then
return error_list;
end if;
data_number := (p_data #>> '{}')::numeric;
-- minimum validation
if p_schema ? 'minimum' then
minimum_val := (p_schema->>'minimum')::numeric;
if data_number < minimum_val then
error_list := error_list || format('path %s: value %s is less than minimum %s',
p_path, data_number, minimum_val);
end if;
end if;
-- maximum validation
if p_schema ? 'maximum' then
maximum_val := (p_schema->>'maximum')::numeric;
if data_number > maximum_val then
error_list := error_list || format('path %s: value %s exceeds maximum %s',
p_path, data_number, maximum_val);
end if;
end if;
-- exclusiveMinimum validation
if p_schema ? 'exclusiveMinimum' then
exclusive_min := (p_schema->>'exclusiveMinimum')::numeric;
if data_number <= exclusive_min then
error_list := error_list || format('path %s: value %s must be greater than exclusiveMinimum %s',
p_path, data_number, exclusive_min);
end if;
end if;
-- exclusiveMaximum validation
if p_schema ? 'exclusiveMaximum' then
exclusive_max := (p_schema->>'exclusiveMaximum')::numeric;
if data_number >= exclusive_max then
error_list := error_list || format('path %s: value %s must be less than exclusiveMaximum %s',
p_path, data_number, exclusive_max);
end if;
end if;
-- multipleOf validation
if p_schema ? 'multipleOf' then
multiple_of_val := (p_schema->>'multipleOf')::numeric;
if multiple_of_val > 0 and (data_number % multiple_of_val) != 0 then
error_list := error_list || format('path %s: value %s is not a multiple of %s',
p_path, data_number, multiple_of_val);
end if;
end if;
return error_list;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__validate_number_constraints(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_number_constraints(p_data jsonb, p_schema jsonb, p_path text) IS '[PRIVATA] Valida i vincoli numerici: minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf.';
--
-- Name: __jsonb_schema__validate_object_constraints(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_object_constraints(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $$
declare
properties_count integer;
min_properties integer;
max_properties integer;
error_list text[] := array[]::text[];
begin
-- solo oggetti possono avere object constraints
if jsonb_typeof(p_data) != 'object' then
return error_list;
end if;
-- conta il numero di proprietà
select count(*) into properties_count
from jsonb_object_keys(p_data);
-- minProperties validation
if p_schema ? 'minProperties' then
min_properties := (p_schema->>'minProperties')::integer;
if properties_count < min_properties then
error_list := error_list || format('path %s: object has %s properties, minimum required is %s',
p_path, properties_count, min_properties);
end if;
end if;
-- maxProperties validation
if p_schema ? 'maxProperties' then
max_properties := (p_schema->>'maxProperties')::integer;
if properties_count > max_properties then
error_list := error_list || format('path %s: object has %s properties, maximum allowed is %s',
p_path, properties_count, max_properties);
end if;
end if;
return error_list;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__validate_object_constraints(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_object_constraints(p_data jsonb, p_schema jsonb, p_path text) IS '[PRIVATA] Valida i vincoli degli oggetti: minProperties, maxProperties.';
--
-- Name: __jsonb_schema__validate_properties(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_properties(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $_$
declare
prop_name text;
prop_schema jsonb;
prop_data jsonb;
error_list text[] := array[]::text[];
temp_errors text[];
current_path text;
begin
-- itera su ogni proprietà definita nello p_schema
for prop_name, prop_schema in
select key, value from jsonb_each(p_schema->'properties')
loop
current_path := format('%s.%s', p_path, prop_name);
if p_data ? prop_name then
prop_data := p_data->prop_name;
-- gestisce $ref
if prop_schema ? '$ref' then
-- risolve il riferimento (implementazione semplificata)
declare
ref_path text := prop_schema->>'$ref';
definitions_schema jsonb;
begin
if ref_path like '#/definitions/%' then
ref_path := replace(ref_path, '#/definitions/', '');
definitions_schema := p_schema->'definitions'->ref_path;
if definitions_schema is not null then
prop_schema := definitions_schema;
end if;
end if;
end;
end if;
-- valida ricorsivamente
temp_errors := common.__jsonb_schema__validate(prop_data, prop_schema, current_path);
error_list := error_list || temp_errors;
end if;
end loop;
-- controlla additionalProperties
if p_schema ? 'additionalProperties' and (p_schema->'additionalProperties')::boolean = false then
for prop_name in
select key from jsonb_object_keys(p_data) key
where not (p_schema->'properties' ? key)
loop
error_list := error_list || format('path %s.%s: additional property not allowed',
p_path, prop_name);
end loop;
end if;
return error_list;
end;
$_$;
--
-- Name: FUNCTION __jsonb_schema__validate_properties(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_properties(p_data jsonb, p_schema jsonb, p_path text) IS 'Valida le proprietà di un oggetto JSONB contro lo schema. Gestisce validation ricorsiva, $ref, e additionalProperties.';
--
-- Name: __jsonb_schema__validate_required(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_required(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $$
declare
required_field text;
error_list text[] := array[]::text[];
begin
for required_field in
select jsonb_array_elements_text(p_schema->'required')
loop
if not (p_data ? required_field) then
error_list := error_list || format('path %s: missing required property "%s"',
p_path, required_field);
end if;
end loop;
return error_list;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__validate_required(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_required(p_data jsonb, p_schema jsonb, p_path text) IS 'Verifica che tutti i campi obbligatori specificati nell''array "required" dello schema siano presenti nel documento.';
--
-- Name: __jsonb_schema__validate_string_constraints(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_string_constraints(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $$
declare
data_text text;
data_length integer;
min_length integer;
max_length integer;
pattern_regex text;
error_list text[] := array[]::text[];
begin
-- solo stringhe possono avere string constraints
if jsonb_typeof(p_data) != 'string' then
return error_list;
end if;
data_text := p_data #>> '{}';
data_length := char_length(data_text);
-- minLength validation
if p_schema ? 'minLength' then
min_length := (p_schema->>'minLength')::integer;
if data_length < min_length then
error_list := error_list || format('path %s: string length %s is less than minLength %s',
p_path, data_length, min_length);
end if;
end if;
-- maxLength validation
if p_schema ? 'maxLength' then
max_length := (p_schema->>'maxLength')::integer;
if data_length > max_length then
error_list := error_list || format('path %s: string length %s exceeds maxLength %s',
p_path, data_length, max_length);
end if;
end if;
-- pattern validation
if p_schema ? 'pattern' then
pattern_regex := p_schema->>'pattern';
if not (data_text ~ pattern_regex) then
error_list := error_list
|| format('path %s: string "%s" does not match pattern "%s"'
, p_path, data_text, pattern_regex);
end if;
end if;
return error_list;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__validate_string_constraints(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_string_constraints(p_data jsonb, p_schema jsonb, p_path text) IS 'Valida i vincoli delle stringhe: minLength, maxLength, pattern.';
--
-- Name: __jsonb_schema__validate_type(jsonb, jsonb, text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__jsonb_schema__validate_type(p_data jsonb, p_schema jsonb, p_path text) RETURNS text[]
LANGUAGE plpgsql
AS $$
declare
expected_type jsonb;
actual_type text;
error_list text[] := array[]::text[];
begin
expected_type := p_schema->'type';
actual_type := jsonb_typeof(p_data);
if expected_type is null then
return error_list;
end if;
-- gestisce array di tipi (es. ["string", "null"])
if jsonb_typeof(expected_type) = 'array' then
if not (expected_type @> to_jsonb(actual_type)) then
error_list := error_list || format('path %s: expected one of %s, got %s',
p_path, expected_type::text, actual_type);
end if;
else
-- tipo singolo
if expected_type::text != format('"%s"', actual_type) then
error_list := error_list || format('path %s: expected %s, got %s',
p_path, expected_type::text, actual_type);
end if;
end if;
return error_list;
end;
$$;
--
-- Name: FUNCTION __jsonb_schema__validate_type(p_data jsonb, p_schema jsonb, p_path text); Type: COMMENT; Schema: common; Owner: -
--
COMMENT ON FUNCTION common.__jsonb_schema__validate_type(p_data jsonb, p_schema jsonb, p_path text) IS 'Valida il tipo di un valore JSONB contro lo schema. Supporta tipi singoli e array di tipi (es. ["string", "null"]).';
--
-- Name: __type__get_canonical_output_name(text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__type__get_canonical_output_name(internal_type text) RETURNS text
LANGUAGE plpgsql
AS $$
declare
t text := lower(trim(coalesce(internal_type, '')));
begin
-- regole per l'output finale
case t
-- datetime: versioni compatte per zoned
when 'timestamptz' then return 'timestamptz';
when 'timetz' then return 'timetz';
when 'timestamp' then return 'timestamp';
when 'date' then return 'date';
when 'time' then return 'time';
-- numerici: versioni estese
when 'int2' then return 'smallint';
when 'int4' then return 'integer';
when 'int8' then return 'bigint';
when 'float4' then return 'real';
when 'float8' then return 'double';
when 'numeric' then return 'numeric';
-- boolean: versione estesa
when 'bool' then return 'boolean';
-- text: char per lunghezza fissa, text per variabile
when 'varchar' then return 'text'; -- varchar è lunghezza variabile -> text
when 'char' then return 'char'; -- char è lunghezza fissa
when 'text' then return 'text';
-- altri tipi
when 'uuid' then return 'uuid';
when 'invalid' then return 'invalid';
else return t;
end case;
end;
$$;
--
-- Name: __type__normalize_name(text); Type: FUNCTION; Schema: common; Owner: -
--
CREATE FUNCTION common.__type__normalize_name(type_name text) RETURNS text
LANGUAGE plpgsql
AS $$
declare
t text := lower(trim(coalesce(type_name, '')));
begin
-- normalizza tutti gli alias ai nomi canonici
case t
when '' then return 'unknown';
-- datetime types
when 'timestamp with time zone' then return 'timestamptz';
when 'time with time zone' then return 'timetz';
-- numeric types
when 'smallint' then return 'int2';
when 'integer' then return 'int4';
when 'bigint' then return 'int8';
when 'real' then return 'float4';
when 'double precision' then return 'float8';
when 'number' then return 'numeric';
-- boolean
when 'boolean' then return 'bool';