Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.coursera.naptime.courier

import java.io.IOException
import java.time.Clock

import com.linkedin.data.ByteString
import com.linkedin.data.DataList
Expand All @@ -40,10 +39,6 @@ import com.linkedin.data.schema.RecordDataSchema
import com.linkedin.data.schema.StringDataSchema
import com.linkedin.data.schema.TyperefDataSchema
import com.linkedin.data.schema.UnionDataSchema
import com.linkedin.data.schema.validation.CoercionMode
import com.linkedin.data.schema.validation.RequiredMode
import com.linkedin.data.schema.validation.ValidateDataAgainstSchema
import com.linkedin.data.schema.validation.ValidationOptions
import com.linkedin.data.template.DataTemplate
import com.linkedin.data.template.DataTemplateUtil
import com.linkedin.data.template.RecordTemplate
Expand All @@ -57,6 +52,10 @@ import org.coursera.courier.templates.ScalaEnumTemplate
import org.coursera.courier.templates.ScalaEnumTemplateSymbol
import org.coursera.naptime.courier.CourierUtils._
import org.coursera.naptime.courier.Exceptions._
import org.coursera.naptime.courier.validation.CoercionMode
import org.coursera.naptime.courier.validation.RequiredMode
import org.coursera.naptime.courier.validation.ValidationOptions
import org.coursera.naptime.courier.validation.ValidateDataAgainstSchema
import play.api.libs.json.JsonValidationError
import play.api.libs.json.Format
import play.api.libs.json.IdxPathNode
Expand Down Expand Up @@ -856,10 +855,11 @@ object CourierFormats extends StrictLogging {
}
}

private[this] val validationOptions =
new ValidationOptions(RequiredMode.FIXUP_ABSENT_WITH_DEFAULT, CoercionMode.STRING_TO_PRIMITIVE)
private[this] val validationOptions = ValidationOptions(
requiredMode = RequiredMode.FIXUP_ABSENT_WITH_DEFAULT,
coercionMode = CoercionMode.STRING_TO_PRIMITIVE)

private[this] def validateAndFixUp(data: Any, schema: DataSchema): Unit = {
private[this] def validateAndFixUp(data: AnyRef, schema: DataSchema): Unit = {
val result = ValidateDataAgainstSchema.validate(data, schema, validationOptions)
if (!result.isValid) {
throw new DataValidationException(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import com.linkedin.data.codec.JacksonDataCodec
import com.linkedin.data.codec.TextDataCodec
import com.linkedin.data.schema.DataSchema
import com.linkedin.data.schema.TyperefDataSchema
import com.linkedin.data.schema.validation.ValidateDataAgainstSchema
import com.linkedin.data.schema.validation.ValidationOptions
import com.linkedin.data.schema.validation.ValidationResult
import com.linkedin.data.schema.validator.DataSchemaAnnotationValidator
import com.linkedin.data.template.DataTemplate
import com.linkedin.data.template.UnionTemplate
import org.coursera.courier.templates.DataTemplates.DataConversion
import org.coursera.courier.templates.DataValidationException
import org.coursera.naptime.courier.validation.ValidateDataAgainstSchema
import org.coursera.naptime.courier.validation.ValidationOptions
import org.coursera.pegasus.TypedDefinitionCodec

import scala.reflect.ClassTag
Expand Down Expand Up @@ -97,7 +97,7 @@ object CourierSerializer {
}
}

private[this] val recordValidationOptions = new ValidationOptions()
private[this] val recordValidationOptions = ValidationOptions()

class TemplateBuilder[T <: DataTemplate[_ <: AnyRef]](private val clazz: Class[T]) {
private[this] val companionInstance = companion(clazz)
Expand All @@ -122,6 +122,7 @@ object CourierSerializer {
schema,
recordValidationOptions,
annotationValidator)

if (!validationResult.isValid) {
Left(validationResult)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright (c) 2021 Coursera, Inc.

This file has been modified by Coursera, Inc. to loosen
validation of enums.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
Copyright (c) 2012 LinkedIn Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.coursera.naptime.courier.validation

import com.linkedin.data.schema.validation.{CoercionMode => PegasusCoercionMode}

sealed trait CoercionMode {
def toPegasus: PegasusCoercionMode
}

object CoercionMode {
case object NORMAL extends CoercionMode {
override def toPegasus: PegasusCoercionMode = PegasusCoercionMode.NORMAL
}
case object STRING_TO_PRIMITIVE extends CoercionMode {
override def toPegasus: PegasusCoercionMode = PegasusCoercionMode.STRING_TO_PRIMITIVE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright (c) 2021 Coursera, Inc.

This file has been modified by Coursera, Inc. to loosen
validation of enums.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
Copyright (c) 2012 LinkedIn Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.coursera.naptime.courier.validation

import com.linkedin.data.schema.validation.{RequiredMode => PegasusRequiredMode}

sealed trait RequiredMode {
def toPegasus: PegasusRequiredMode
}

object RequiredMode {
case object CAN_BE_ABSENT_IF_HAS_DEFAULT extends RequiredMode {
override def toPegasus: PegasusRequiredMode = PegasusRequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT
}
case object FIXUP_ABSENT_WITH_DEFAULT extends RequiredMode {
override def toPegasus: PegasusRequiredMode = PegasusRequiredMode.FIXUP_ABSENT_WITH_DEFAULT
}
}
Loading