/** * Obtain the set of types given the supplied name or iterable container of names. * * @param typeName the String or Symbol representation of a single type name, or an Iterable&?> list of names * @return the set of types that correspond to the supplied names; never null but possibly an empty set */ public static EnumSet<Type> typesWithNames( Object typeName ) { if (typeName instanceof List) { List<Type> result = new ArrayList<Type>(); Iterable<?> typeNames = (Iterable<?>)typeName; for (Object tname : typeNames) { if (tname == null) continue; String name = tname.toString(); Type type = byName(name); if (type != null) result.add(type); } if (!result.isEmpty()) { return EnumSet.copyOf(result); } } else if (typeName instanceof String || typeName instanceof Symbol) { String name = typeName.toString(); Type type = byName(name); return EnumSet.of(type); } return EnumSet.noneOf(Type.class); } }
case UNKNOWN: Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType);
case UNKNOWN: Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType);
case UNKNOWN: Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType);
if (value instanceof String) { Type type = JsonSchema.Type.byName((String)value); if (type == Type.ANY || type == Type.UNKNOWN) return; validators.add(new TypeValidator(type)); validator = create(schemaOrRef, parentPath.with("type")); } else if (obj instanceof String) { Type type = JsonSchema.Type.byName((String)obj); if (type == Type.ANY || type == Type.UNKNOWN) continue; validator = new TypeValidator(type);
case UNKNOWN: Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType);
if (value instanceof String) { Type type = JsonSchema.Type.byName((String)value); if (type == Type.ANY || type == Type.UNKNOWN) return; validators.add(new TypeValidator(type)); validator = create(schemaOrRef, parentPath.with("type")); } else if (obj instanceof String) { Type type = JsonSchema.Type.byName((String)obj); if (type == Type.ANY || type == Type.UNKNOWN) continue; validator = new TypeValidator(type);
case UNKNOWN: Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType);
Type actual = Type.typeFor(fieldValue); if (!type.isEquivalent(actual)) { Object converted = type.convertValueFrom(fieldValue, actual); Path pathToField = fieldName != null ? pathToDocument.with(fieldName) : pathToDocument; String reason = "Field value for '" + pathToField + "' expected to be of type " + type + " but was of type "
@Override public Number convert( Object value, Type type ) { if (value == null) return null; switch (type) { case STRING: String str = value.toString(); return JsonReader.parseNumber(str); // may return null case INTEGER: case NUMBER: return (Number)value; case BOOLEAN: // Convert a boolean to either '1' or '0' integer ... Boolean bool = (Boolean)value; return Boolean.TRUE.equals(bool) ? new Integer(1) : new Integer(0); case OBJECT: // this is a document, and we can't convert it to a number ... case UNION: case NULL: case ARRAY: return null; case ANY: // fall through case UNKNOWN: // Figure out the type ... Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType); } return null; } }
case UNKNOWN: Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType);
Type actual = Type.typeFor(fieldValue); if (!type.isEquivalent(actual)) { Object converted = type.convertValueFrom(fieldValue, actual); Path pathToField = fieldName != null ? pathToDocument.with(fieldName) : pathToDocument; String reason = "Field value for '" + pathToField + "' expected to be of type " + type + " but was of type "
@Override public Number convert( Object value, Type type ) { if (value == null) return null; switch (type) { case STRING: String str = value.toString(); return JsonReader.parseNumber(str); // may return null case INTEGER: case NUMBER: return (Number)value; case BOOLEAN: // Convert a boolean to either '1' or '0' integer ... Boolean bool = (Boolean)value; return Boolean.TRUE.equals(bool) ? new Integer(1) : new Integer(0); case OBJECT: // this is a document, and we can't convert it to a number ... case UNION: case NULL: case ARRAY: return null; case ANY: // fall through case UNKNOWN: // Figure out the type ... Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType); } return null; } }
@Override public Object convert( Object value, Type type ) { if (value == null) return null; switch (type) { case OBJECT: // this is a document return value; case STRING: case INTEGER: case NUMBER: case BOOLEAN: case UNION: case NULL: return null; case ARRAY: // The array can already be treated as a document ... return value instanceof Document ? value : null; case ANY: // fall through case UNKNOWN: // Figure out the type ... Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType); } return null; } }
@Override public Object convert( Object value, Type type ) { if (value == null) return null; switch (type) { case OBJECT: // this is a document return value; case STRING: case INTEGER: case NUMBER: case BOOLEAN: case UNION: case NULL: return null; case ARRAY: // The array can already be treated as a document ... return value instanceof Document ? value : null; case ANY: // fall through case UNKNOWN: // Figure out the type ... Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType); } return null; } }
/** * Obtain the set of types given the supplied name or iterable container of names. * * @param typeName the String or Symbol representation of a single type name, or an Iterable&?> list of names * @return the set of types that correspond to the supplied names; never null but possibly an empty set */ public static EnumSet<Type> typesWithNames( Object typeName ) { if (typeName instanceof List) { List<Type> result = new ArrayList<Type>(); Iterable<?> typeNames = (Iterable<?>)typeName; for (Object tname : typeNames) { if (tname == null) continue; String name = tname.toString(); Type type = byName(name); if (type != null) result.add(type); } if (!result.isEmpty()) { return EnumSet.copyOf(result); } } else if (typeName instanceof String || typeName instanceof Symbol) { String name = typeName.toString(); Type type = byName(name); return EnumSet.of(type); } return EnumSet.noneOf(Type.class); } }
@Override public String convert( Object value, Type type ) { if (value == null) return null; switch (type) { case STRING: return (String)value; case INTEGER: case NUMBER: case BOOLEAN: return value.toString(); case OBJECT: // this is a document, and we can't convert it to a string ... case UNION: case NULL: case ARRAY: return null; case ANY: // fall through case UNKNOWN: // Figure out the type ... Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType); } return null; } }
@Override public String convert( Object value, Type type ) { if (value == null) return null; switch (type) { case STRING: return (String)value; case INTEGER: case NUMBER: case BOOLEAN: return value.toString(); case OBJECT: // this is a document, and we can't convert it to a string ... case UNION: case NULL: case ARRAY: return null; case ANY: // fall through case UNKNOWN: // Figure out the type ... Type inferredType = Type.typeFor(value); if (inferredType == Type.UNKNOWN || inferredType == type) return null; return convert(value, inferredType); } return null; } }
@Override public void validate( Object fieldValue, String fieldName, Document parent, Path pathToParent, Problems problems, SchemaDocumentResolver resolver ) { Type type = Type.typeFor(fieldValue); if (type != Type.NULL) { if (disallowedTypes.contains(type)) { problems.recordError(pathToParent.with(fieldName), "The '" + fieldName + "' field on '" + pathToParent + "' contains a value '" + fieldValue + "' whose type '" + type + "' is disallowed."); } else { problems.recordSuccess(); } } }
@Override public void validate( Object fieldValue, String fieldName, Document parent, Path pathToParent, Problems problems, SchemaDocumentResolver resolver ) { Type type = Type.typeFor(fieldValue); if (type != Type.NULL) { if (disallowedTypes.contains(type)) { problems.recordError(pathToParent.with(fieldName), "The '" + fieldName + "' field on '" + pathToParent + "' contains a value '" + fieldValue + "' whose type '" + type + "' is disallowed."); } else { problems.recordSuccess(); } } }