Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ValidationHelper.cs « SchemaObjectModel « EntityModel « Data « System « System.Data.Entity « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a9ea0c731406451027531b2fc3a94aac4f2c3947 (plain)
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
//---------------------------------------------------------------------
// <copyright file="ValidationHelper.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityModel.SchemaObjectModel;
using System.Data.Metadata.Edm;
using System.Data.Entity;
using System.Diagnostics;

namespace System.Data.EntityModel.SchemaObjectModel
{
    /// <summary>
    /// Helper methods used for Schema Object Model (validation) validation.
    /// </summary>
    internal static class ValidationHelper
    {
        /// <summary>
        /// Validates whether facets are declared correctly.
        /// </summary>
        /// <param name="element">Schema element being validated. Must not be null.</param>
        /// <param name="type">Resolved type (from declaration on the element). Possibly null.</param>
        /// <param name="typeUsageBuilder">TypeUsageBuilder for the current element. Must not be null.</param>
        internal static void ValidateFacets(SchemaElement element, SchemaType type, TypeUsageBuilder typeUsageBuilder)
        {
            Debug.Assert(element != null);
            Debug.Assert(typeUsageBuilder != null);

            if (type != null)
            {
                var schemaEnumType = type as SchemaEnumType;
                if (schemaEnumType != null)
                {
                    typeUsageBuilder.ValidateEnumFacets(schemaEnumType);
                }
                else if(!(type is ScalarType) && typeUsageBuilder.HasUserDefinedFacets)
                {
                    Debug.Assert(!(type is SchemaEnumType), "Note that enums should have already been handled.");

                    // Non-scalar type should not have Facets. 
                    element.AddError(ErrorCode.FacetOnNonScalarType, EdmSchemaErrorSeverity.Error, Strings.FacetsOnNonScalarType(type.FQName));
                }
            }
            else
            {
                if(typeUsageBuilder.HasUserDefinedFacets)
                {
                    // Type attribute not specified but facets exist.
                    element.AddError(ErrorCode.IncorrectlyPlacedFacet, EdmSchemaErrorSeverity.Error, Strings.FacetDeclarationRequiresTypeAttribute);
                }
            }
        }

        /// <summary>
        /// Validated whether a type is declared correctly. 
        /// </summary>
        /// <param name="element">Schema element being validated. Must not be null.</param>
        /// <param name="type">Resolved type (from declaration on the element). Possibly null.</param>
        /// <param name="typeSubElement">Child schema element. Possibly null.</param>
        /// <remarks>
        /// For some elements (e.g. ReturnType) we allow the type to be defined inline in an attribute on the element itself or 
        /// by using nested elements. These definitions are mutually exclusive.
        /// </remarks>
        internal static void ValidateTypeDeclaration(SchemaElement element, SchemaType type, SchemaElement typeSubElement)
        {
            Debug.Assert(element != null);

            if (type == null && typeSubElement == null)
            {
                //Type not declared as either attribute or subelement
                element.AddError(ErrorCode.TypeNotDeclared, EdmSchemaErrorSeverity.Error, Strings.TypeMustBeDeclared);
            }

            if (type != null && typeSubElement != null)
            {
                //Both attribute and sub-element declarations exist
                element.AddError(ErrorCode.TypeDeclaredAsAttributeAndElement, EdmSchemaErrorSeverity.Error, Strings.TypeDeclaredAsAttributeAndElement);
            }
        }

        /// <summary>
        /// Validate that reference type is an entity type.
        /// </summary>
        /// <param name="element">Schema element being validated. Must not be null.</param>
        /// <param name="type">Resolved type (from declaration on the element). Possibly null.</param>
        internal static void ValidateRefType(SchemaElement element, SchemaType type)
        {
            Debug.Assert(element != null);

            if (type != null && !(type is SchemaEntityType))
            {
                // Ref type refers to non entity type.
                element.AddError(ErrorCode.ReferenceToNonEntityType, EdmSchemaErrorSeverity.Error, Strings.ReferenceToNonEntityType(type.FQName));
            }
        }
    }
}