Add minLength = 1 for quote in highlight

This commit is contained in:
Hongbo Wu
2023-03-10 15:40:31 +08:00
parent 2e197644d4
commit 45a857152b
5 changed files with 25 additions and 14 deletions

View File

@ -15,6 +15,7 @@ export const sanitizeDirectiveTransformer = (schema: GraphQLSchema) => {
}
const maxLength = sanitizeDirective.maxLength as number | undefined
const minLength = sanitizeDirective.minLength as number | undefined
const allowedTags = sanitizeDirective.allowedTags as string[] | undefined
const pattern = sanitizeDirective.pattern as string | undefined
@ -27,6 +28,7 @@ export const sanitizeDirectiveTransformer = (schema: GraphQLSchema) => {
fieldConfig.type.ofType,
allowedTags,
maxLength,
minLength,
pattern
)
)
@ -35,6 +37,7 @@ export const sanitizeDirectiveTransformer = (schema: GraphQLSchema) => {
fieldConfig.type,
allowedTags,
maxLength,
minLength,
pattern
)
} else {

View File

@ -4062,6 +4062,7 @@ export type ResolversParentTypes = {
export type SanitizeDirectiveArgs = {
allowedTags?: Maybe<Array<Maybe<Scalars['String']>>>;
maxLength?: Maybe<Scalars['Int']>;
minLength?: Maybe<Scalars['Int']>;
pattern?: Maybe<Scalars['String']>;
};

View File

@ -1,4 +1,4 @@
directive @sanitize(allowedTags: [String], maxLength: Int, pattern: String) on INPUT_FIELD_DEFINITION
directive @sanitize(allowedTags: [String], maxLength: Int, minLength: Int, pattern: String) on INPUT_FIELD_DEFINITION
type AddPopularReadError {
errorCodes: [AddPopularReadErrorCode!]!

View File

@ -9,6 +9,7 @@ export class SanitizedString extends GraphQLScalarType {
type: GraphQLScalarType,
allowedTags?: string[],
maxLength?: number,
minLength?: number,
pattern?: string
) {
super({
@ -25,11 +26,7 @@ export class SanitizedString extends GraphQLScalarType {
// invoked when a query is passed as a JSON object (for example, when Apollo Client makes a request
parseValue(value) {
if (maxLength && maxLength < value.length) {
throw new Error(
`Specified value cannot be longer than ${maxLength} characters`
)
}
checkLength(value)
if (pattern && !new RegExp(pattern).test(value)) {
throw new Error(`Specified value does not match pattern`)
}
@ -39,17 +36,26 @@ export class SanitizedString extends GraphQLScalarType {
// invoked when a query is passed as a string
parseLiteral(ast) {
const value = type.parseLiteral(ast, {})
if (maxLength && maxLength < value.length) {
throw new Error(
`Specified value cannot be longer than ${maxLength} characters`
)
}
checkLength(value)
if (pattern && !new RegExp(pattern).test(value)) {
throw new Error(`Specified value does not match pattern`)
}
return sanitize(value, { allowedTags: allowedTags || [] })
},
})
function checkLength(value: any) {
if (maxLength && maxLength < value.length) {
throw new Error(
`Specified value cannot be longer than ${maxLength} characters`
)
}
if (minLength && minLength > value.length) {
throw new Error(
`Specified value cannot be shorter than ${minLength} characters`
)
}
}
}
}

View File

@ -8,6 +8,7 @@ const schema = gql`
directive @sanitize(
allowedTags: [String]
maxLength: Int
minLength: Int
pattern: String
) on INPUT_FIELD_DEFINITION
@ -688,7 +689,7 @@ const schema = gql`
shortId: String!
articleId: ID!
patch: String!
quote: String! @sanitize(maxLength: 6000)
quote: String! @sanitize(maxLength: 6000, minLength: 1)
prefix: String @sanitize
suffix: String @sanitize
annotation: String @sanitize(maxLength: 4000)
@ -720,7 +721,7 @@ const schema = gql`
shortId: ID!
articleId: ID!
patch: String!
quote: String! @sanitize(maxLength: 6000)
quote: String! @sanitize(maxLength: 6000, minLength: 1)
prefix: String @sanitize
suffix: String @sanitize
annotation: String @sanitize(maxLength: 8000)
@ -752,7 +753,7 @@ const schema = gql`
highlightId: ID!
annotation: String @sanitize(maxLength: 4000)
sharedAt: Date
quote: String @sanitize(maxLength: 6000)
quote: String @sanitize(maxLength: 6000, minLength: 1)
}
type UpdateHighlightSuccess {