private static void DeclareFieldArguments(
            IObjectFieldDescriptor fieldDescriptor,
            FieldDefinitionNode fieldDefinition)
        {
            foreach (InputValueDefinitionNode inputFieldDefinition in
                     fieldDefinition.Arguments)
            {
                fieldDescriptor.Argument(inputFieldDefinition.Name.Value,
                                         a =>
                {
                    IArgumentDescriptor descriptor = a
                                                     .Description(
                        inputFieldDefinition.Description?.Value)
                                                     .Type(inputFieldDefinition.Type)
                                                     .DefaultValue(inputFieldDefinition.DefaultValue)
                                                     .SyntaxNode(inputFieldDefinition);

                    foreach (DirectiveNode directive in
                             inputFieldDefinition.Directives)
                    {
                        descriptor.Directive(directive);
                    }
                });
            }
        }
        public static IArgumentDescriptor Validate <T>(
            this IArgumentDescriptor argumentDescriptor,
            Func <T, bool> func)
        {
            Action <IDirective, FieldNode, object> validator = (d, n, o) =>
            {
                bool isValid = false;
                if (o is T t)
                {
                    isValid = func(t);
                }

                if (!isValid)
                {
                    throw new QueryException(new ArgumentError(
                                                 "Argument is not valid.",
                                                 ((InputField)d.Source).Name,
                                                 n));
                }
            };

            return(argumentDescriptor.Directive(
                       new ArgumentValidationDirective {
                Validator = validator
            }));
        }
        /// <summary>
        /// Instructs FairyBread to add the given validator/s to the argument.
        /// </summary>
        public static IArgumentDescriptor ValidateWith(
            this IArgumentDescriptor descriptor,
            params Type[] validatorTypes)
        {
            descriptor.Extend().OnBeforeNaming((completionContext, argDef) =>
            {
                if (!argDef.ContextData.TryGetValue(WellKnownContextData.ExplicitValidatorTypes, out var explicitValidatorTypesRaw) ||
                    explicitValidatorTypesRaw is not List <Type> explicitValidatorTypes)
                {
                    argDef.ContextData[WellKnownContextData.ExplicitValidatorTypes]
                        = new List <Type>(validatorTypes.Distinct());
                    return;
                }

                foreach (var validatorType in validatorTypes)
                {
                    if (!explicitValidatorTypes.Contains(validatorType))
                    {
                        explicitValidatorTypes.Add(validatorType);
                    }
                }
            });

            return(descriptor);
        }
Esempio n. 4
0
        public static IArgumentDescriptor Validate <T>(
            this IArgumentDescriptor argumentDescriptor,
            Func <T, bool> func)
        {
            Action <IDirectiveContext, FieldNode, string, object> validator = (d, n, a, o) =>
            {
                bool isValid = false;
                if (o is T t)
                {
                    isValid = func(t);
                }

                if (!isValid)
                {
                    throw new QueryException(QueryError.CreateArgumentError(
                                                 "Argument is not valid.",
                                                 d.Path,
                                                 n,
                                                 a));
                }
            };

            return(argumentDescriptor.Directive(
                       new ArgumentValidationDirective {
                Validator = validator
            }));
        }
 public override void OnConfigure(
     IDescriptorContext context,
     IArgumentDescriptor descriptor,
     ParameterInfo parameterInfo)
 {
     descriptor.DefaultValue(DefaultValue);
 }
 public override void OnConfigure(
     IDescriptorContext context,
     IArgumentDescriptor descriptor,
     ParameterInfo parameter)
 {
     descriptor.ValidateRuleSets(_ruleSets);
 }
 public override void OnConfigure(
     IDescriptorContext context,
     IArgumentDescriptor descriptor,
     ParameterInfo parameter)
 {
     descriptor.ValidateProperties(_properties);
 }
 public override void OnConfigure(
     IDescriptorContext context,
     IArgumentDescriptor descriptor,
     ParameterInfo parameter)
 {
     descriptor.DontValidate();
 }
        public static IArgumentDescriptor Validate <T>(
            this IArgumentDescriptor argumentDescriptor,
            Func <T, bool> func)
        {
            Action <IDirectiveContext, FieldNode, string, object> validator =
                (d, n, a, o) =>
            {
                bool isValid = false;
                if (o is T t)
                {
                    isValid = func(t);
                }

                if (!isValid)
                {
                    throw new QueryException(
                              ErrorBuilder.New()
                              .SetMessage("Argument is not valid.")
                              .SetPath(d.Path)
                              .AddLocation(n)
                              .SetExtension("argument", a)
                              .Build());
                }
            };

            return(argumentDescriptor.Directive(
                       new ArgumentValidationDirective
            {
                Validator = validator
            }));
        }
        /// <summary>
        /// Skip object validation.
        /// </summary>
        public static IArgumentDescriptor SkipValidation(
            this IArgumentDescriptor descriptor)
        {
            descriptor.Extend().OnBeforeCreate(
                d => d.ContextData[ArgumentValidationKeys.Skip] = true);

            return(descriptor);
        }
        public static IArgumentDescriptor UseValidation(
            this IArgumentDescriptor descriptor)
        {
            descriptor
            .Extend()
            .OnBeforeCreate(definition => definition.ContextData.Add(ValidateAttribute.ValidateContextDataKey, true));

            return(descriptor);
        }
        /// <summary>
        /// Validate the specified rule sets only.
        /// By default, only unspecified "default" rule set is being validated.
        /// To validate all rule sets pass "*".
        /// </summary>
        public static IArgumentDescriptor ValidateRuleSets(
            this IArgumentDescriptor descriptor,
            params string[] ruleSets)
        {
            descriptor.Extend().OnBeforeCreate(
                d => d.ContextData[ArgumentValidationKeys.RuleSets] = ruleSets);

            return(descriptor);
        }
        /// <summary>
        /// Validate the specified properties only.
        /// This will inspect all rule sets in search for the specified property names.
        /// </summary>
        public static IArgumentDescriptor ValidateProperties(
            this IArgumentDescriptor descriptor,
            params string[] properties)
        {
            descriptor.Extend().OnBeforeCreate(
                d => d.ContextData[ArgumentValidationKeys.Properties] = properties);

            return(descriptor);
        }
        /// <summary>
        /// Instructs FairyBread to not run any validation for this argument.
        /// </summary>
        public static IArgumentDescriptor DontValidate(
            this IArgumentDescriptor descriptor)
        {
            descriptor.Extend().OnBeforeNaming((completionContext, argDef) =>
            {
                argDef.ContextData[WellKnownContextData.DontValidate] = true;
            });

            return(descriptor);
        }
Esempio n. 15
0
    public static IArgumentDescriptor Input(
        this IArgumentDescriptor descriptor,
        string argumentName = "input",
        string?typeName     = null)
    {
        ExtensionData contextData = descriptor.Extend().Definition.ContextData;

        contextData[InputContextData.Input] = new InputContextData(typeName, argumentName);
        return(descriptor);
    }
Esempio n. 16
0
        public override void OnConfigure(
            IDescriptorContext context,
            IArgumentDescriptor descriptor,
            ParameterInfo parameter)
        {
            if (parameter.GetCustomAttribute <ValidateAttribute>() is not ValidateAttribute attr)
            {
                return;
            }

            descriptor.ValidateWith(attr.ValidatorTypes);
        }
Esempio n. 17
0
        /// <summary>
        /// Configures argument for validation
        /// </summary>
        public static IArgumentDescriptor UseFluentValidation(
            this IArgumentDescriptor argumentDescriptor,
            Action <ArgumentValidationBuilder>?configure = null)
        {
            argumentDescriptor.Extend().OnBeforeCreate(argumentDefinition =>
            {
                var options = argumentDefinition.ContextData.GetOrCreateArgumentOptions();

                configure?.Invoke(new DefaultArgumentValidationBuilder(options));
            });

            return(argumentDescriptor);
        }
    public static IArgumentDescriptor ID(
        this IArgumentDescriptor descriptor,
        NameString typeName = default)
    {
        if (descriptor is null)
        {
            throw new ArgumentNullException(nameof(descriptor));
        }

        RelayIdFieldHelpers.ApplyIdToField(descriptor, typeName);

        return(descriptor);
    }
        public override void OnConfigure(
            IDescriptorContext context,
            IArgumentDescriptor descriptor,
            ParameterInfo parameter)
        {
            var fluentValidationAttributes = parameter.GetCustomAttributes <FluentValidationAttribute>();

            descriptor.UseFluentValidation(options =>
            {
                foreach (var fluentValidationAttribute in fluentValidationAttributes)
                {
                    fluentValidationAttribute.Configure(options);
                }
            });
        }
        public static IArgumentDescriptor ID(
            this IArgumentDescriptor descriptor,
            NameString typeName = default)
        {
            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            descriptor.Extend().OnBeforeCreate(RewriteInputFieldType);
            descriptor.Extend().OnBeforeCompletion(
                (c, d) => AddSerializerToInputField(c, d, typeName));

            return(descriptor);
        }
Esempio n. 21
0
    /// <summary>
    /// Specifies the type of an argument with GraphQL SDL type syntax.
    /// </summary>
    /// <param name="descriptor">
    /// The argument descriptor.
    /// </param>
    /// <param name="typeSyntax">
    /// The GraphQL SDL type syntax.
    /// </param>
    /// <returns>
    /// Returns the argument descriptor for configuration chaining.
    /// </returns>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="descriptor"/> is <c>null</c>.
    /// <paramref name="typeSyntax"/> is <c>null</c>.
    /// </exception>
    /// <exception cref="SyntaxException">
    /// The GraphQL SDL type syntax is invalid.
    /// </exception>
    public static IArgumentDescriptor Type(
        this IArgumentDescriptor descriptor,
        string typeSyntax)
    {
        if (descriptor is null)
        {
            throw new ArgumentNullException(nameof(descriptor));
        }

        if (typeSyntax is null)
        {
            throw new ArgumentNullException(nameof(typeSyntax));
        }

        return(descriptor.Type(Utf8GraphQLParser.Syntax.ParseTypeReference(typeSyntax)));
    }
Esempio n. 22
0
        private static void DeclareArguments(
            IDirectiveTypeDescriptor typeDescriptor,
            DirectiveDefinitionNode node)
        {
            foreach (InputValueDefinitionNode inputField in node.Arguments)
            {
                IArgumentDescriptor descriptor = typeDescriptor
                                                 .Argument(inputField.Name.Value)
                                                 .Description(inputField.Description?.Value)
                                                 .Type(inputField.Type)
                                                 .DefaultValue(inputField.DefaultValue)
                                                 .SyntaxNode(inputField);

                foreach (DirectiveNode directive in inputField.Directives)
                {
                    descriptor.Directive(directive);
                }
            }
        }
Esempio n. 23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Parameter"/> class.
 /// </summary>
 /// <param name="signature">
 /// The signature.
 /// </param>
 /// <param name="argument">
 /// The argument.
 /// </param>
 public Parameter(ISignature signature, IArgumentDescriptor argument)
 {
     this.Signature = signature;
     this.argument  = argument;
 }
Esempio n. 24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SerializableArgumentDescriptor"/> class.
 /// </summary>
 /// <param name="argument">
 /// The argument.
 /// </param>
 public SerializableArgumentDescriptor([NotNull] IArgumentDescriptor argument)
 {
     this.Name        = argument.Name;
     this.Description = argument.Description;
     this.Type        = new SerializableTypeDescriptor(argument.Type);
 }
Esempio n. 25
0
 /// <summary>
 /// Instructs FairyBread to add the given validator to the argument.
 /// </summary>
 public static IArgumentDescriptor ValidateWith <TValidator>(
     this IArgumentDescriptor descriptor)
     where TValidator : IValidator
 {
     return(descriptor.ValidateWith(typeof(TValidator)));
 }
Esempio n. 26
0
 public static IArgumentDescriptor Type <TInputType>(this IArgumentDescriptor descriptor, bool nullable) where TInputType : class, IInputType
 {
     return(nullable ? descriptor.Type <TInputType>() : descriptor.Type <NonNullType <TInputType> >());
 }
 public abstract void OnConfigure(IArgumentDescriptor descriptor);
 public override void OnConfigure(IArgumentDescriptor descriptor)
 {
     descriptor.DefaultValue(DefaultValue);
 }
 public abstract void OnConfigure(
     IDescriptorContext context,
     IArgumentDescriptor descriptor,
     ParameterInfo parameter);